Reputation: 1
I'm about to make iPhone apps. and I faced a problem.
NSArray *array2 = [NSArray alloc];
[array2 initWithObjects:@"a",@"b",@"c",@"d",nil];
NSMutableArray *arr3 = [NSArray ArrayWithObjects:@"e",@"f",@"g",nil];
NSUInteger uInt1 = [array2 count];
uInt1 = [arr3 count];
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array2];
NSLog(@"count : %d", [mutableArray count]);
Is this code wrong? I think I'm familiar with c language. but it is hard to handle with c-objective class like NS family....
It is my first time to iPhone programming..
Upvotes: 0
Views: 95
Reputation: 16650
A. NSArray
Creation
NSArray *array2 = [NSArray alloc];
[array2 initWithObjects:@"a",@"b",@"c",@"d",nil];
Here is a real bug: You have to respect that init…
methods have a return value:
NSArray *array2 = [NSArray alloc];
array2 = [array2 initWithObjects:@"a",@"b",@"c",@"d",nil];
But as mentioned by sulthan, you usually do it in one statement.
However, you can forget about what I said, because you can do want you want to do easier with array literals:
NSArray *array2 = @[@"a",@"b",@"c",@"d"];
B. NSMutableArray
Creation
NSMutableArray *arr3 = [NSArray ArrayWithObjects:@"e",@"f",@"g",nil];
There is a bug, the compiler should warn you about: You create an instance of NSArray
and assign it to a object reference to NSMutableArray
, what is a subclass. Options:
NSMutableArray *arr3 = [NSMutableArray ArrayWithObjects:@"e",@"f",@"g",nil];
or
NSMutableArray *arr3 = [@"e",@"f",@"g" mutableCopy];
C. Logging
NSLog(@"count : %d", [mutableArray count]);
There is a bug, the compiler should warn you about: -count
returns a value of type NSUInteger
. This can be an int or a long, depending on the architecture. Since clang does format string checking, %d
is the wrong placeholder for longs. The usual way to handle that is:
NSLog(@"count : %ld", (long)[mutableArray count]);
Upvotes: 0
Reputation: 130072
NSArray *array2 = [NSArray alloc];
[array2 initWithObjects:@"a",@"b",@"c",@"d",nil];
You should assign the result of initWithObjects:
to array2
, usually you don't even split alloc
from init...
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",nil];
or using arrayWithObjects:
method:
NSArray *array2 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",nil];
or using literal syntax
NSArray *array2 = @[@"a",@"b",@"c",@"d"];
NSMutableArray *arr3 = [NSArray ArrayWithObjects:@"e",@"f",@"g",nil];
You cannot assign an immutable array NSArray
to a mutable one NSMutableArray
, also the case of ArrayWithObjects
is wrong. Correct is:
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"e",@"f",@"g",nil];
Nothing wrong here:
NSUInteger uInt1 = [array2 count];
uInt1 = [arr3 count];
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array2];
NSLog(@"count : %d", [mutableArray count]);
%d
shouldn't be really used with a NSUInteger
. You will get compiler warning on 64bit machines.
Print the number as object instead:
NSLog(@"count : %@", @([mutableArray count]));
Upvotes: 0
Reputation: 11
Depends on what you are trying to do, but syntactically it should be something more like this:
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",nil];
NSMutableArray * array2 = [NSMutableArray arrayWithObjects:@"e",@"f",@"g",nil];
NSUInteger count1 = [array1 count];
NSUInteger count2 = [array2 count];
NSMutableArray * array3 = [[NSMutableArray alloc] initWithArray:array2];
Couple of notes:
Upvotes: 1