serendi21
serendi21

Reputation: 1

is this code wrong?

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

Answers (3)

Amin Negm-Awad
Amin Negm-Awad

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

Sulthan
Sulthan

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

brianpseudo
brianpseudo

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:

  • While you could technically do an alloc and then the init of an NSObject on two separate lines, universally you would do them together as above on line 1.
  • The second line uses an class method to instantiate the NSMutableArray. Under the hood (typically), those types of methods will do the alloc & init and return an object. But in (slightly) more advanced situations, they may return nil, meaning an object couldn't be created. For example, you want to instantiate an AVCaptureDevice but one doesn't exist.
  • You can't create an NSArray and assign it to an NSMutableArray, but you can go the other way--NSMutableArray is a subclass of NSArray.

Upvotes: 1

Related Questions