1.//NSMutableArray 继承自array 的可变数组
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1];// 这个数字代表了数组初始化有n个结构体的内存,若不够,再申请 n 个结构体内存.
例子
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];
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1];
2.addObject // 给可变数组添加元素
- (void)addObject:(id)object //Adds a given object to the receiver
例子
[array addObject:p];
[array addObject:p1];
[array addObject:p2];
[array addObject:p];
NSLog(@"%@", array);
3.insertObject //在可变数组中在某个下标位置添加元素
- (void)insertObject:(NSManagedObject *)object // Registers an object to be inserted in the receiver’s persistent store the next time changes are saved.
例子
[array insertObject:p1 atIndex:3];
NSLog(@"%@", array);
4.removeLastObject //删除最后一个
- (void)removeLastObject //Removes the object with the highest-valued index in the array
例子
[array removeLastObject];
NSLog(@"%@", array);
5.removeObject // 删除该数组所有得 p (元素)
virtual void removeObject( const OSMetaClassBase *anObject); //Removes an object from the set.
例子
[array removeObject:p];
NSLog(@"%@", array);
6. removeObjectAtIndex // 根据下标删除某个元素
- (void)removeObjectAtIndex:(NSUInteger)idx //Removes a the object at the specified index from the mutable ordered set.
例子
[array removeObjectAtIndex:1];
NSLog(@"%@", array);
for (int i = 0; i < [array count]; i++) {
[array removeObjectAtIndex:i];
i--;//数组是动态的
}
NSLog(@"%@", array);
7.replaceObjectAtIndex // 用某个对象替换给定下标的对象
- (void)replaceObjectAtIndex:(NSUInteger)idx
withObject:(id)object
//Replaces the object at the specified index with the new object.
例子
[array replaceObjectAtIndex:1 withObject:p];
NSLog(@"%@", array);
8.exchangeObjectAtIndex // //给定两个下标 交换
- (void)exchangeObjectAtIndex:(NSUInteger)idx1
withObjectAtIndex:(NSUInteger)idx2
//Exchanges the objects in the array at given indices.
例子
[array exchangeObjectAtIndex:1 withObjectAtIndex:3];
NSLog(@"%@", array);
9.isKindOfClass // 是否是某个类
- (BOOL)isKindOfClass:(Class)aClass //Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)
例子
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, @"111", p1, @"222", p2, @"333", nil];
// NSLog(@"%@", array);
//
for (int i = 0; i < [array count]; i++) {
NSString * obj = [array objectAtIndex:i];
idnewStr = [obj stringByAppendingString:@"aaaa"];// 崩溃 !
NSLog(@"%@", obj);
}
for (int i = 0; i < [array count]; i++) {
id obj = [array objectAtIndex:i];
if ([obj isKindOfClass:[NSString class]]) {
NSString * newStr = [obj stringByAppendingString:@"aaaa"];
NSLog(@"%@", newStr);
}
NSLog(@"%@", obj);
}
10.//numberWithInt 把基本型转位对象
+ (NSNumber *)numberWithInt:(int)value //Creates and returns an NSNumber object containing a given value, treating it as a signed int.
例子
int n = 5;
NSNumber * number = [NSNumber numberWithInt:n];
NSArray * array = [NSArray arrayWithObjects:number, [NSNumber numberWithInt:8], nil];
NSLog(@"%@", array);
11.intValue //对象转为基本型
@property(readonly) int intValue // The integer value of the string. (read-only)
例子
int sum = [[array firstObject] intValue] + [[array lastObject] intValue];
NSLog(@"%d", sum);