我们的生活有这么多的障碍,真他妈的有意思,这种逻辑就叫做黑色幽默。

Objective-C 2.0学习笔记之NSMutableArray

#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int i;
	NSArray *row=[NSArray arrayWithObjects:@"duoduoluo",@"theboy",@"longmao",nil];
	NSMutableArray *array;//定义一个可变数组
	array=[NSMutableArray arrayWithCapacity:3];//定义数组长度
	[array addObjectsFromArray:row];//增加
	[array removeObjectAtIndex:1];//删除
	for(i=0;i<[array count];i++)
	{
		NSLog(@"%@",[array objectAtIndex:i]);
    }
	[pool drain];
    return 0;
}

output:

[Session started at 2010-08-21 16:20:58 +0800.]
2010-08-21 16:20:58.395 nsstring[688:903] duoduoluo
2010-08-21 16:20:58.400 nsstring[688:903] longmao

The Debugger has exited with status 0.

NSArray:(immutable)

在创建的时候,就包含了所有对象.你不能增加或是删除其中任何一个对象.这种特定称为: immutable.

1. – (unsigned)count 得到array中的对象个数.

2. -(id)objectAtIndex:(unsigned)i 得到索引为i的对象.如果i值超过了array对象数量,在程序运行到这里会产生错误.

3. -(id)lastObject 得到最后一个对象.如果NSArray中没有任何对象存在,返回nil.

4. -(BOOL)containsObject:(id)anObject 当anObject出现在NSArray中,则返回YES.

5. -(unsigned)indexOfObject:(id)anObject查找NSArray中是否存在anObject, 并返回最小的索引值.

NSMutableArray:

NSMutableArray继承NSArray,扩展了增加,删除对象的功能. 可以使用NSArray的mutableCopy方法来复制得到一个可修改的NSMutableArray对象.

1. – (void)addObject:(id)anObject 在reciever最后添加anObject. 添加nil是非法的.

2. – (void)addObjectsFromArray:(NSArray *)otherArray 在reciever最后,把otherArray中的对象都依次添加进去.

3. – (void)insertObject:(id)anObject atIndex:(unsigned)index 在索引index处插入对象anObject. 如果index被占用,会把之后的object向后移动. index不能大于所包含对象个数,并且anObject不能为空.

4. – (void)removeAllObjects 清空array.

5. – (void)removeObject:(id)anObject 删除所有和anObject相等的对象.

6. – (void)removeObjectAtIndex:(unsigned)index 删除索引为index的对象.后面的对象依次往前移.如果index越界,将会产生错误.

theboy @ 八月 21, 2010 at 16:25 下午

Have 3 reply

TrackBack URL

Leave a reply