简体   繁体   中英

[objective-c]about NSMutableArray

ruby code(irb)

irb(main):001:0> ary = ["AAA", "BBB", "CCC"]
=> ["AAA", "BBB", "CCC"]
irb(main):003:0> ary.index("AAA")
=> 0
irb(main):004:0> ary.index("BBB")
=> 1
irb(main):005:0> ary.index("CCC")
=> 2
irb(main):006:0> ary.index("DDD")
=> nil

I want to do same in objective-c(NSMutableArray).

NSMutableArray *ary = [NSMutableArray arrayWithObjects:@"AAA",@"BBB",@"CCC",nil];
NSLog([ary description]);
NSInteger ndx = [ary indexOfObjectIdenticalTo:@"AAA"];
NSLog(@"%i",ndx);
ndx = [ary indexOfObjectIdenticalTo:@"BBB"];
NSLog(@"%i",ndx);
ndx = [ary indexOfObjectIdenticalTo:@"CCC"];
NSLog(@"%i",ndx);
ndx = [ary indexOfObjectIdenticalTo:@"DDD"];
NSLog(@"%i",ndx);

Cocoa methods are all pretty self explanatory, so skimming the headers can help you solve almost any problem.

EDIT: changed from NSArray to NSMutableArray, added note:

Note: the last method returns -1, not nil.

阅读Apple的NSArray和NSMutableArray文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM