Tuesday, April 1, 2014

Create an array


1
  NSArray *array = [NSArray arrayWithObjects:@"One", [NSNumber numberWithInt:1], nil];

Or use literal array expression:-

1
  NSArray *array =@[ @"One", [NSNumber numberWithInt:1] ];

Use NSNumber to add boolean or integer values to an array, or prefix a boolean value with ‘@':-

1
  NSArray *array = @[ [NSNumber numberWithInt:1], [NSNumber numberWithBool:YES], @NO ];

Otherwise we will get an error like this:-

     Collection element of type ‘NSInteger’ (aka ‘int’) is not an Objective-C object

Use NSNull to add a nil equivalent value to an array:-

1
  NSArray *array = @[ @"a", [NSNull null], @"b" ];


Refs
http://stackoverflow.com/questions/1602572/how-to-create-an-array-of-strings-in-objective-c-for-iphone
http://clang.llvm.org/docs/ObjectiveCLiterals.html
http://stackoverflow.com/questions/1309535/why-does-nsarray-arraywithobjects-require-a-terminating-nil
http://stackoverflow.com/questions/2057910/how-to-add-nil-to-nsmutablearray

No comments:

Post a Comment