public void IndexOfTest() { NSArray array = new NSArray(); array.Add(1); array.Add("test"); Assert.Equal(0, array.IndexOf(1)); Assert.Equal(1, array.IndexOf("test")); }
public void InsertTest() { NSArray array = new NSArray(); array.Add(0); array.Add(1); array.Add(2); array.Insert(1, "test"); Assert.Equal(4, array.Count); Assert.Equal("test", array[1].ToObject()); }
// ReSharper disable once InconsistentNaming public static NSObject?ToNSObject(object?obj) { if (obj == null) { return(null); } var type = obj.GetType(); if (type.IsPrimitive || type == typeof(string) || type == typeof(byte[])) { // NSObject can deal with this itself return(NSObject.Wrap(obj)); } if (IsSet(type)) { var nsSet = new NSSet(); foreach (var value in (IEnumerable)obj) { nsSet.AddObject(ToNSObject(value)); } return(nsSet); } if (IsDictionary(type)) { var nsDictionary = new NSDictionary(); foreach (DictionaryEntry kvp in (IDictionary)obj) { nsDictionary.Add((string)kvp.Key, ToNSObject(kvp.Value)); } return(nsDictionary); } if (IsEnumerable(type)) { var nsArray = new NSArray(); foreach (var value in (IEnumerable)obj) { nsArray.Add(ToNSObject(value)); } return(nsArray); } var dict = new NSDictionary(); foreach (var property in type.GetProperties(PropertyFlags)) { var name = property.Name; var dataMemberAttr = property.GetCustomAttribute <DataMemberAttribute>(); if (dataMemberAttr?.Name != null) { name = dataMemberAttr.Name; } dict.Add(name, ToNSObject(property.GetValue(obj))); } return(dict); }
public void GetStringArray_NSArray_ReturnsValue() { var array = new NSArray(); array.Add(new NSString("value1")); array.Add(new NSString("value2")); var dict = new NSDictionary(); dict.Add("key", array); Assert.Collection( dict.GetStringArray("key"), (v) => Assert.Equal("value1", v), (v) => Assert.Equal("value2", v)); }
public void AddAndContainsObjectTest() { NSArray array = new NSArray(); array.Add(1); Assert.True(array.Contains(1)); Assert.False(array.Contains(2)); }
public void RemoveTest() { NSArray array = new NSArray(); array.Add(0); Assert.False(array.Remove((object)1)); Assert.True(array.Remove((object)0)); Assert.Empty(array); }
public void EnumeratorTest() { NSArray array = new NSArray(); array.Add(0); array.Add(1); IEnumerator <NSObject> enumerator = array.GetEnumerator(); Assert.Null(enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(new NSNumber(0), enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(new NSNumber(1), enumerator.Current); Assert.False(enumerator.MoveNext()); }
public void RemoveTest() { NSArray array = new NSArray(); array.Add(0); Assert.False(array.Remove((object)1)); Assert.True(array.Remove((object)0)); Assert.Equal(0, array.Count); }
public void EnumeratorTest() { NSArray array = new NSArray(); array.Add(0); array.Add(1); var enumerator = array.GetEnumerator(); Assert.IsNull(enumerator.Current); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(new NSNumber(0), enumerator.Current); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(new NSNumber(1), enumerator.Current); Assert.IsFalse(enumerator.MoveNext()); }
// Recursively converts `NSMutable..` classes into corresponding ordinary ones. // Assume `current` comes from `ConvertNSClasses`, because keys can only be strings. public static NSObject ConvertMutableClasses(NSObject current) { switch (current) { case NSArray currentArray: for (int i = 0; i < currentArray.Count; i++) { currentArray[i] = ConvertMutableClasses(currentArray[i]); } return(currentArray); case NSDictionary currentDict: switch (ClassName(currentDict)) { case "NSMutableDictionary": var nsKeys = (NSArray)currentDict["NS.keys"]; var nsObjects1 = (NSArray)currentDict["NS.objects"]; var newDict1 = new NSDictionary(); for (int i = 0; i < nsKeys.Count; i++) { var key = (string)(NSString)nsKeys[i]; var value = ConvertMutableClasses(nsObjects1[i]); newDict1.Add(key, value); } return(newDict1); case "NSMutableArray": var nsObjects = (NSArray)currentDict["NS.objects"]; var newArray2 = new NSArray(); for (int i = 0; i < nsObjects.Count; i++) { newArray2.Add(ConvertMutableClasses(nsObjects[i])); } return(newArray2); default: var newDict = new NSDictionary(); foreach (var item in currentDict) { var key = item.Key; var value = ConvertMutableClasses(item.Value); newDict.Add(key, value); } return(newDict); } case NSSet currentSet: var newArray = currentSet.AllObjects().Select(o => ConvertMutableClasses(o)).ToArray(); return(new NSSet(NSSetSorted(currentSet), newArray)); default: return(current); } }
public static NSArray ParseArray(XmlNode node) { NSArray array = new NSArray(); int count = node.ChildNodes.Count; for (int i = 0; i < count; i++) { object result = Parse(node.ChildNodes[i]); if (result != null) { array.Add(result); } } return array; }
public NSArray ParseArchivedArray(NSDictionary archivedArray) { var objects = archivedArray.GetAs <NSArray>("NS.objects"); var resolvedArray = new NSArray(); foreach (var @object in objects) { resolvedArray.Add(ResolveObject(@object)); } return(resolvedArray); }
public void GetDataArray_ReturnsValue() { var entry = new byte[] { 1, 2, 3, 4 }; var array = new NSArray(); array.Add(new NSData(entry)); var dict = new NSDictionary(); dict.Add("key", array); var value = Assert.Single(dict.GetDataArray("key")); Assert.Equal(entry, value); }
/* * Packing function * Creates a collection of images, a rectangle object, and sends it through an engine instance, * which plots all images it to a power of 2 square. These are then drawn to a single spritesheet */ public static void Pack() { ///Creating an instance Rectangles, an collection of images from a file Rectangles rectangles = new Rectangles(); /// Calculates the area of all the images int totalAreaAllImages = 0; foreach (ImageInfo image in rectangles) { totalAreaAllImages += image.Width * image.Height; }//end foreach ///Calculate the closest power of 2 square that could enclose the images int side = 2; while (side < Math.Sqrt(totalAreaAllImages)) { side = side * 2; }//end while ///Creating an instance of engine, which plots the image objects to a position on the spritesheet Engine engine = new Engine(side, rectangles); /// Creating NSObjects to let us create a plist file NSDictionary locations; NSDictionary root = new NSDictionary(); NSArray imageArray = new NSArray(rectangles.fileCount); /// A bitmap to draw all of the images to Bitmap sheet = new Bitmap(engine.size, engine.size); /// Drawing images to bitmap sheet using (Graphics gfx = Graphics.FromImage(sheet)) { foreach (ImageInfo image in rectangles) { Write("."); //Writing image location data for plist file locations = new NSDictionary(); locations.Add("Width", image.Width); locations.Add("Height", image.Height); locations.Add("X", image.Position.X); locations.Add("Y", image.Position.Y); imageArray.Add(locations); //Drawing method gfx.DrawImage(image.Img, image.Position); } //end foreach } //end using graphics /// Creating plist file of image info on spritesheet root.Add("Frames", imageArray); PropertyListParser.SaveAsXml(root, new FileInfo(rectangles.outputPath + ".plist")); WriteLine("\nSaving:\n{0}", rectangles.outputPath); /// Saving spritesheet sheet.Save(rectangles.outputPath); WriteLine("What would you like to do now?"); }