3

Is it possible to access elements of an NSArray based on index and use them for comparison/operations?

Just like arrayName[ith element] in C/C++

sreejith.virgo
  • 333
  • 1
  • 4
  • 15

4 Answers4

13

Yes , you can access elements of an NSArray based on index .

NSArray *a=@[@"s1",@"s2",@"s3"];
NSLog(@"%@",a[1]);
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
7

Try this

arrayName[yourIndex];

or

[arrayName objectAtIndex:yourIndex];
Nathan
  • 11,938
  • 12
  • 55
  • 62
Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35
0

Try this way.

[arrayname objectAtindex:yourIndex]

user1673099
  • 3,293
  • 7
  • 26
  • 57
0

You can use 'Index Subscripting Literal' that Apple Provide on iOS 6 SDK later

NSArray *array = @[1, 2, 3];
NSLog(@"%@", array[0])
Kiattisak Anoochitarom
  • 2,157
  • 1
  • 20
  • 15
  • subscripting has nothing to do with the SDK. It's a compiler feature. – Gabriele Petronella Oct 07 '13 at 05:05
  • 1
    @GabrielePetronella That is half true. The syntax itself comes from the compiler, but the actual functions that implement it are part of the iOS 6 SDK (You only have to build with it, it will use some magic to insert itself into older versions at runtime) – borrrden Oct 07 '13 at 05:47
  • @borrden, fair enough. I guess it's a matter of wording. Subscripting literals are a compiler feature, but Apple indeed started taking advantage on them from iOS 6 on. – Gabriele Petronella Oct 07 '13 at 13:36