1.initWithObjects //
例子
NSArray * array = [[NSArray alloc] initWithObjects:@"A", @"B",@"C" ,@"D" ,@"E" , nil];
NSLog(@"%@", array);
//arrayWithObjects 是initWithObjects 的便利构造器
NSArray * array = [NSArray arrayWithObjects:@"11", @"22", @"33", nil];
NSLog(@"%@", array);
2. //objectAtIndex //根据下标 得到数组中某个元素
- (id)objectAtIndex:(NSUInteger)index // Returns the object at the specified index of the set.
例子
NSString * str = [array objectAtIndex:1];
NSLog(@"%@", str);
3. firstObject //@property(nonatomic, readonly) id firstObject . // 得到数组中第一个值
lastObject // @property(nonatomic, readonly) id lastObject .// 得到数组中最后一个值
例子
NSLog(@"%@", [array firstObject]);// 得到数组第一个元素
NSLog(@"%@", [array lastObject]);// 得到数组最后一个元素
4.containsObject //判断 某个字符串在数组中是否存在
virtual bool containsObject( const OSMetaClassBase *anObject) const; //Checks the set for the presence of an object.
例子
BOOL b = [array containsObject:p];
NSLog(@"%@", b ? @"YES" : @"NO");
5.indexOfObject //得到数组中某个元素的下标
- (NSUInteger)indexOfObject:(id)anObject // Returns the lowest index whose corresponding array value is equal to a given object.
例子
Person * p = [[Person alloc] initWithName:@"fu" sex:@"nan" age:30];
Person * p1 = [[Person alloc] initWithName:@"lu" sex:@"nan" age:30];
Person * p2 = [[Person alloc] initWithName:@"shou" sex:@"nan" age:30];
NSArray * array = [NSArray arrayWithObjects:p, p1, p2, nil];
NSUInteger n1 = [array indexOfObject:p1];
NSLog(@"%lu", n1);
6.isEqualToArray 判断两个数组是否相等
- (BOOL)isEqualToArray:(NSArray *)otherArray //Compares the receiving array to another array.
BOOL bb = [array isEqualToArray:array];
NSLog(@"%@", bb ? @"YES" : @"NO");