iOS中数组的使用(NSArray,NSSet,NSDictionary)

来源:互联网 发布:用plsql导入excel数据 编辑:程序博客网 时间:2024/05/18 02:15

line-height:24px">NSArray用于对象有序集合(数组)
NSSet用于对象无序集合(集合)
NSDictionary用于键值映射(字典)
  以上三种集合类是不可变的(一旦初始化后,就不能改变)
  以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类)
NSMutableArray
NSMutableSet
NSMutableDictionary

  注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
===================NSArray====================
Orderedcollection of objects.Immutable(you cannot add or remove objects to it onceit’s created)
Importantmethods:
+(id)arrayWithObjects:(id)firstObject, ...;//nilterminated
-(int)count;                         
//得到array中的对象个数
-(id)objectAtIndex:(int)index; //得到索引为i的对象
-(BOOL)containsObject:(id)anyObject;  //anyObject出现在array中,则返回yes(实际是通过isEqual:方法来判断)
-(unsigned)indexOfObject:(id)anyObject; 
//查找array中的anObject,并返回其最小索引值。没找到返回NSNotFound.
-(void)makeObjectsPerformSelector:(SEL)aSelector;

- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
-(id)lastObject;               
//得到array中最后一个对象。如果array中没有任何对象存在,则返回nil
  注:类方法arrayWithObjects可以创建anautoreleasedNSArrayof the items.例如
@implementationMyObject
-(NSArray *)coolCats{
return[NSArray arrayWithObjects@“Steve”, @“Ankush”, @“Sean”, nil];
}
@end
Otherconvenient create with methods (all return autoreleased objects):
[NSString stringWithFormat@“Meaning of %@ is %d”, @“life”, 42];
[NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
[NSArray arrayWithContentsOfFile:(NSString *)path];
-----创建数组-----
NSArray*array = [[NSArray alloc]
initWithObjects@One,@Two,@Three,@Four,nil];
self.dataArray= array;
[arrayrelease];
NSLog(@self.dataArraycount is:%d,[self.dataArray count]);
NSLog(@self.dataArrayindex 2 is:%@,[self.dataArray objectAtIndex:2]);
------从一个数组拷贝数据到另一数组(可变数级)-------
//arrayWithArray:
NSArray*array1 = [[NSArray alloc]
init];

NSMutableArray *MutableArray =[[NSMutableArray alloc]
init];
NSArray*array = [NSArray arrayWithObjects@a,@b,@c,nil];
NSLog(@array:%@,array);
MutableArray= [NSMutableArray arrayWithArray:array];
NSLog(@MutableArray:%@,MutableArray);
array1= [NSArray arrayWithArray:array];
NSLog(@array1:%@,array1);
//Copy
//idobj;
NSMutableArray*newArray = [[NSMutableArray alloc]
init];
NSArray*oldArray = [NSArray arrayWithObjects@a,@b,@c,@d,@e,@f,@g,@h,nil];
NSLog(@oldArray:%@,oldArray);
for(inti = 0; i  [oldArray count]; i++){  
obj= [[oldArray objectAtIndex:i]
copy];
[newArrayaddObject: obj];
}
NSLog(@newArray:%@,newArray);
[newArrayrelease];
//快速枚举
NSMutableArray*newArray = [[NSMutableArray alloc]
init];
NSArray*oldArray = [NSArray arrayWithObjects:
@a,@b,@c,@d,@e,@f,@g,@h,nil]; 
NSLog(@oldArray:%@,oldArray);
for(idobj in oldArray)
{
[newArrayaddObject: obj];
}
NSLog(@newArray:%@,newArray);
[newArrayrelease]; 
//Deepcopy
NSMutableArray*newArray = [[NSMutableArray alloc]
init];
NSArray*oldArray = [NSArray arrayWithObjects:
@a,@b,@c,@d,@e,@f,@g,@h,nil]; 
NSLog(@oldArray:%@,oldArray); 
newArray= (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault,(CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
NSLog(@newArray:%@,newArray);
[newArrayrelease]; 
===================NSMutableArray====================
Mutableversion of NSArray.
-(void)addObject:(id)anyObject;//array最后添加anObject,添加nil是非法的.
-(void)addObjectsFromArray:(NSArray *)otherArray;//array最后把otherArray中的对象依次添加进去。
-(void)insertObject:(id)anyObject atIndex:(int)index;//在索引index处插入anyObject,index被占用,会把之后的object向后移。
-(void)removeObjectAtIndex:(int)index;  //删除index处的对象,后面的对象依次向前移。
-(void)removeObject:(id)anyObject; //删除所有和anObject相等的对象,同样使用isEqual:作为相等比较方法.
-(void)removeAllObjects;
  注:我们不能把nil加到array中。但有时候我们真想给array加一个空的对象,可以使用NSNull来做这件事。如:
[myArray addObject:[NSNull null]];
-----给数组分配容量-----
//NSArray*array;
array= [NSMutableArray arrayWithCapacity:20];
-----在数组末尾添加对象-----
//-(void) addObject: (id) anyObject;
//NSMutableArray*array = [NSMutableArray arrayWithObjects@One,@Two,@Three,nil];
[arrayaddObject@Four];
NSLog(@array:%@,array);
-----删除数组中指定索引处对象-----
//-(void)removeObjectAtIndex: (unsigned) index; 
//NSMutableArray*array = [NSMutableArray arrayWithObjects@One,@Two,@Three,nil];
[arrayremoveObjectAtIndex:1];
NSLog(@array:%@,array);
-----数组枚举-----
//1-(NSEnumerator *)objectEnumerator; //从前向后
NSMutableArray*array = [NSMutableArray arrayWithObjects@One,@Two,@Three,nil];
NSEnumerator*enumerator;
enumerator= [array objectEnumerator];
idthingie;
while(thingie = [enumerator nextObject]) {
NSLog(@thingie:%@,thingie);
}
//2-(NSEnumerator *)reverseObjectEnumerator; //从后向前
NSMutableArray*array = [NSMutableArray arrayWithObjects@One,@Two,@Three,nil];
NSEnumerator*enumerator;
enumerator= [array reverseObjectEnumerator];
idobject;
while(object = [enumerator nextObject]) {
NSLog(@object:%@,object);
}
//3、快速枚举
NSMutableArray*array = [NSMutableArray arrayWithObjectsOne,@Two,@Three,nil];
for(NSString*string in array){
NSLog(@string:%@,string);
}
-----NSValue(对任何对象进行包装)-----
//NSRect放入NSArray
NSMutableArray*array = [[NSMutableArray alloc]
init];
NSValue*value;
CGRectrect = CGRectMake(0, 0, 320, 480); 
value= [NSValue valueWithBytes:rect objCType@encode(CGRect)];
[arrayaddObject:value];
NSLog(@array:%@,array);
//Array提取
value= [array objectAtIndex:0];
[valuegetValue:rect];
NSLog(@value:%@,value);
----使用NSMutableArray要防止内存泄露------
NSObject*p1 = [[NSObject alloc]
init];
NSObject*p2 = [[NSObject alloc]
init];
NSMutableArray*objectsArray = [[NSMutableArray alloc]
init];
[objectsArrayaddObject:p1];
NSLog(@p1count:%d, [p1 retainCount]);//输出 2,也就是执行追加对象后,对象的计数器也被加1
//[p1release];
//NSLog(@p1count:%d, [p1 retainCount]);
//同样做数组替换时
[objectsArrayreplaceObjectAtIndex:0 withObject:p2];
NSLog(@p2count:%d, [p2 retainCount]);//输出 2,同样也是2
NSLog(@p1count:%d, [p1 retainCount]);//输出 1,对象p1仍然存在
//[p2release];
//NSLog(@p2count:%d, [p2 retainCount]);
//执行清空数组
[objectsArrayremoveAllObjects];
NSLog(@p2count:%d, [p2 retainCount]);//输出 1,对象p2仍然存在
//[p2release];
  由此可知,每次执行上面的数组操作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。
NSSet
Unorderedcollection of objects.
Immutable.You cannot add or remove objects to it once it’s created.
Importantmethods:
+setWithObjects:(id)firstObj, ...; //nilterminated
-(int)count;
-(BOOL)containsObject:(id)anObject;
-(id)anyObject;
-(void)makeObjectsPerformSelector:(SEL)aSelector;
-(id)member:(id)anObject; // uses isEqual: and returns amatching object (if any)
NSMutableSet
Mutableversion of NSSet.
+(NSMutableSet *)set;
-(void)addObject:(id)anyObject;
-(void)removeObject:(id)anyObject;
-(void)removeAllObjects;
-(void)unionSet:(NSSet *)otherSet;
-(void)minusSet:(NSSet *)otherSet;
-(void)intersectSet:(NSSet *)otherSet;
NSDictionary
  一个dictionary就是key-value对的集合。key是字符串(NSString),value是对象指针。
key在整个dictionary是唯一的。通过方法objectForKey:来获得对应于某个key的一个或多个value(值)。
NSDictionary在创建时,其中所有的key和对应的value都存在了。你可以访问其内容,但不可以修改。
dictionary使用hash表来实现,所以查找速度很快。
-(NSUInteger)hash  - (BOOL)isEqual:(NSObject *)obj
Importantmethods:
+dictionaryWithObjectsAndKeys: (id)firstObject, ...;
-(int)count;// 返回有多少对key-value
-(id)objectForKey:(id)key; //返回和key相关联的value
  如果没有和key相关联的value,则返回nil
-(NSArray *)allKeys;    // 返回一个包含所有keyarray
-(NSArray *)allValues;
-NSEnumerator *)keyEnumerator;  //这个方法是从一个 dictionary中得到一个key的迭代器.
Enumerators也就是iterators enumerations.我们可以使用它来一步一步迭代出集合中的所有成员.   
  下面是我们可能使用它来列举所有的 key-vaule 
NSEnumerator*e = [myDict keyEnumerator];
for(NSString *s in e) { 
NSLog(@keyis %@, value is %@, s, [myDict objectForKey:s]);
}
  注:NSArray也有一个类似的方法得到 array的成员迭代  : objectEnumerator
-----创建字典-----
-(id) initWithObjectsAndKeys;
NSDictionary*dictionary = [[NSDictionary alloc]
initWithObjectsAndKeys@One,@1,@Two,@2,@Three,@3,nil];
NSString*string = [dictionary objectForKey@One];
NSLog(@string:%@,string);
NSLog(@dictionary:%@,dictionary);
[dictionaryrelease];
NSMutableDictionary
Mutableversion of NSDictionary.
NSMutableDictionaryNSDictionary的子类。创建后,允许用户添加和删除keyvalue
+(NSMutableDictionary *)dictionary;         //创建一个空的dictionary
-(void)setObject:(id)anyObjectforKey:(id)key;  //使用keyanObject组成一条记录添加到dictionary
//如果key已存在,则会先移除旧的key-value,然后再添加新的key-value
-(void)removeObjectForKey:(id)key;      
//dictionary中删除一条记录,key以及和它对应的value
-(void)removeAllObjects;
-(void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
-----创建可变字典-----
//创建
NSMutableDictionary*dictionary = [NSMutableDictionary dictionary];
//添加字典
[dictionary setObject@One forKey@1];
[dictionary setObject@Two forKey@2];
[dictionary setObject@Three forKey@3];
[dictionary setObject@Four forKey@4];
NSLog(@dictionary:%@,dictionary);
//删除指定的字典
[dictionary removeObjectForKey@3];
NSLog(@dictionary:%@,dictionary);


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 工商注册后大股东不注资怎么办 公司不给去办理变更股东信息怎么办 滴滴车主注册没有自己的车型怎么办 代办用虚假地址注册的公司怎么办? 写字楼注册公司租户不租了怎么办 租户没把公司迁出我该怎么办 租户不肯把户口迁出了业主怎么办 同片区个体户营业场所搬迁怎么办 个体领发票的本丢了怎么办 领房产证发票和合同丢了怎么办 税务登记证5年没有办怎么办 二证合一后税务登记证怎么办? 微信漂流瓶打招呼对方收不到怎么办 添加不上徽信好友的微信帐号怎么办 手机号码不用了微信密码忘了怎么办 别人用我的手机号码注册微信怎么办 电脑此网站的安全证书有问题怎么办 起诉离婚开庭时被告不到场怎么办 商标35类被别人注册了怎么办 商标被注销后被别人注册怎么办 电商35类商标被抢注怎么办 血小板太低怎么办可以吃水果吗? 微信好友删除了只记得昵称怎么办 优酷会员1080p很卡怎么办 电脑最下面的任务栏不显示怎么办 ea账号保留的问题忘了怎么办 微博超级话题账号被屏蔽怎么办 梦幻西游手机将军令号码换了怎么办 文档的内容被锁定无法修改怎么办 用户没有权限访问u盘时该怎么办? 电脑找不到ip地址连不上网怎么办 商标提前被别人注册微博昵称怎么办 扫码注册显示手机号被占用怎么办 vivo手机屏锁密码忘了怎么办 手机号注册微博手机号不用了怎么办 微博更换手机号被别人注册过怎么办 怎么有个qq注册验证码怎么办 12306手机丢了密码忘了怎么办 教师考试注册时邮箱填写错误怎么办 计算机二级注册时邮箱填错了怎么办 注册时执业范围填错了怎么办