How to insert items into NSArray object in C# (Monotouch)? I don't find appropriate method to do so? In Objective-C side, there is a constructor called "initWithObjects" but I don't find this on C# side.
pom
Sorry to answer your question with a question, but are you sure you want to create an NSArray?
I ask because MonoTouch does some work behind the scenes so that you don't have to deal with NSArray.
If you go to the MonoTouch API Design page and do an in-page search for "NSArray", you'll find this:
Instead of dealing with NSString and NSArray the runtime instead exposes these as C# strings and strongly typed arrays throughout the API.
In other words, unless you have a specific reason to, you don't have to mess with NSArray.
But, if you do have a reason, or if it's your preference, then Jason's answer is correct:
NSArray someArray = NSArray.FromNSObjects(thingOne, thingTwo, thingThree);
The only thing I'd add is that, if you're an Objective-C dev and giving MonoTouch a try, you're used to this:
NSArray *someArray = [NSArray arrayWithObjects:obj1, obj2, obj3, nil];
The difference is the "nil" at the end of the Objective-C version. MonoTouch's NSArray doesn't have this same requirement. You just pass it the objects you want it to contain, and it happily does so. No terminating null required :)
Hope this helps...
I'm not using Monotouch - but if you're looking to manipulate an NSArray, you're going to want to use it's cousin, NSMutableArray.
It looks like this static method is what you want:
public static NSArray FromNSObjects (params NSObject[] items);
Used like this:
NSArray arr = NSArray.FromNSObjects(obj1, obj2, obj3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With