iOS making a 2D array using NSMutable array - objective-c

I want to make a 2D array like this
[[1,2,3],[94,22],[2,4,6],[1,3,5,6]]
What is the best way to do this for iOS using NSMutable arrays

You cannot create a static 2D array with differently sized rows.
Perhaps you can use NSArrays instead of C arrays to achieve this.
edit:
This is tedious, but you can try:
NSArray *array1 = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:2],[NSNumber numberWithInt:1],[NSNumber numberWithInt:3],nil];
And so on for each array, then
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:array1];

Neither C nor Objective-C truly support 2D arrays. Instead, in either language, you can create an array of arrays. Doing this in C gives you the same effect as a 2D array of ints with 2 rows and 3 columns, or vice versa:
int foo[2][3];
You can do something similar in Objective-C, but since you can't create objects statically, and you can't fix the size of a mutable array, and NSMutableArray holds object pointers rather than ints, it's not exactly the same:
NSMutableArray *foo = [NSMutableArray array];
for (short i = 0; i < 2; i++)
[foo addObject:[NSMutableArray array]];

NSArray *array = #[#[#1, #2, #3],
#[#94, #22],
#[#2, #4, #6],
#[#1, #3, #5, #6]];
NSMutableArray *arrayM = [[NSMutableArray alloc] init];
for(NSArray *nextArray in array)
arrayM addObject:[[NSMutableArray alloc] initWithArray:nextArray];

Related

Most efficient way to create an NSMutableArray from NSArray

Which of these, if either, is more efficient?
NSMutableArray *array = [NSMutableArray arrayWithArray:#[#1, #2]];
or
NSMutableArray *array = [#[#1, #2] mutableCopy];
Or are these the same internally?
The two options you provide both first create an NSArray and then create an NSMutableArray from the NSArray so there is essentially no difference.
There is a third option that would be ever so slightly better in this case:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#1, #2, nil];
This doesn't create an intermediate NSArray like your other two options.
While using ARC there is no difference in your example. But if you would use NSArray variable instead of static #[#1, #2], in case of this array being nil the first would return you empty array and the second one will return you nil.

Create a dynamic array

I am new. It's a basic question i guess.
Suppose I need to have a dynamic array to store several objects from my Own class,like classA. I ve no idea about how to wrap these classA-objects and put 'em into the array, maybe NSMutableArray. thx alot.
#interface classA
{
int x;
int y;
}
...
classA *a,*b,*c;
Initialize your array with :
NSMutableArray *myArray = [[NSMutableArray alloc] init];
And then add an object with :
[myArray addObject:a];
[myArray addObject:b];
And so on
You can find it all explained here : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
If you need your array to be mutable,
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:2];
[arr addObject:a];
[arr addObject:b];
will suffice. Objective-C, like Smalltalk, uses dynamic typing. You can just add the objects.
If you do not need to mutate your array,
NSArray *arr = #[a, b];

Array of doubles objective c

Is there something like NSMutableArray that will take doubles directly without putting them inside the #""?
You can only put objects into an NSMutableArray. But you can wrap your doubles in NSNumber like so:
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:[NSNumber numberWithDouble:0.12345]];
[array addObject:[NSNumber numberWithDouble:5.43210]];
You can only insert objects into an NSMutableArray. Luckily, there is a class, NSNumber, that is used to wrap Objective-C primitives as objects. You can use the doubleValue method to get your primitive value back.
For example:
NSMutableArray *numArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithDouble:1.1f], nil];
double num = [[numArray objectAtIndex:0]doubleValue];

Arrays of NSObjects in Objective-C

I've created an object, and now I'm trying to create an array full of these objects. I've tried a few different things with no success.
How can I do this?
You can do it one of two ways, with NSArray or NSMutableArray.
id obj1, obj2, obj3;
// This creates a static array
NSArray *array = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];
// This creates a dynamic array
NSMutableArray *mutarray = [NSMutableArray array];
[mutarray addObject:obj1];
[mutarray addObject:obj2];
[mutarray addObject:obj3];
NSMutableArray * arrayOfFoos = [NSMutableArray array];
for (int i = 0; i < 100; ++i) {
Foo * f = [[Foo alloc] init];
[arrayOfFoos addObject:f];
[f release];
}
You can use an NSArray, take a look at Apple's documentation.
If you wanna add them incrementally consider using a mutable collection like an NSMutableArray (here in the doc)

NSArray filled with bool

I am trying to create an NSArray of bool values. How many I do this please?
NSArray *array = [[NSArray alloc] init];
array[0] = YES;
this does not work for me.
Thanks
NSArrays are not c-arrays. You cant access the values of an NSArray with array[foo];
But you can use c type arrays inside objective-C without problems.
The Objective-C approach would be:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithBool:YES]];
//or
[array addObject:#(NO)];
...
BOOL b = [[array objectAtIndex:0] boolValue];
....
[array release];
EDIT: New versions of clang, the now standard compiler for objective-c, understand Object subscripting. When you use a new version of clang you will be able to use array[0] = #YES
Seems like you've confused c array with objc NSArray. NSArray is more like a list in Java, into which you can add objects, but not values like NSInteger, BOOL, double etc. If you wish to store such values in an NSArray, you first need to create a mutable array:
NSMutableArray* array = [[NSMutableArray alloc] init];
And then add proper object to it (in this case we'll use NSNumber to store your BOOL value):
[array addObject:[NSNumber numberWithBool:yourBoolValue]];
And that's pretty much it! If you wish to access the bool value, just call:
BOOL yourBoolValue = [[array objectAtIndex:0] boolValue];
Cheers,
Pawel
Use [NSNumber numberWithBool: YES] to get an object you can put in the collection.