资源描述:
《ios中数组的使用(nsarraynssetnsdictionary)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、我的命运我做主http://www.goodprogrammer.org/IOS中数组的使用(NSArray,NSSet,NSDictionary)line-height:24px">NSArray用于对象有序集合(数组) NSSet用于对象无序集合(集合) NSDictionary用于键值映射(字典) 以上三种集合类是不可变的(一旦初始化后,就不能改变) 以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类): NSMutableArray NSMutableSet NSMutableDi
2、ctionary 注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int,float,double,BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。 ===================NSArray==================== Orderedcollectionofobjects.Immutable(youcannotaddorremoveobjectstoitoncei
3、t’screated) Importantmethods: +(id)arrayWithObjects:(id)firstObject,...;//nilterminated -(int)count; //得到array中的对象个数 -(id)objectAtIndex:(int)index;//得到索引为i的对象 -(BOOL)containsObject:(id)anObject; //当anObject出现在array中,则返回yes(实际是通过isEqual:方法来
4、判断) -(unsigned)indexOfObject:(id)anObject; //查找array中的anObject,并返回其最小索引值。没找到返回NSNotFound. -(void)makeObjectsPerformSelector:(SEL)aSelector;-(NSArray*)sortedArrayUsingSelector:(SEL)aSelector; -(id)lastObject; //得到array中最后一个对象。如果array中没有任何对象存在,则返回ni
5、l 注: 类方法arrayWithObjects可以创建anautoreleasedNSArrayoftheitems.例如 @implementationMyObject -(NSArray*)coolCats{ return[NSArrayarrayWithObjects:@“Steve”,@“Ankush”,@“Sean”,nil]; } @end Otherconvenientcreatewithmethods(allreturnautoreleasedobjects): [NSStringstr
6、ingWithFormat:@“Meaningof%@is%d”,@“life”,42]; [NSDictionarydictionaryWithObjectsAndKeys:ankush,@“TA”,janestudent,@“Student”,nil]; [NSArrayarrayWithContentsOfFile:(NSString*)path]; -----创建数组----- NSArray*array=[[NSArrayalloc]最低7000就业,不就业不交学费我的命运我做主http://www.go
7、odprogrammer.org/ initWithObjects:@One,@Two,@Three,@Four,nil]; self.dataArray=array; [arrayrelease]; NSLog(@self.dataArraycountis:%d,[self.dataArraycount]); NSLog(@self.dataArrayindex2is:%@,[self.dataArrayobjectAtIndex:2]); ------从一个数组拷贝数据到另一数组(可变数级)-------
8、 //arrayWithArray: NSArray*array1=[[NSArrayalloc] init];NSMutableArray*MutableArray=[[NSMutableArrayalloc] init]; NSArray*array=[NSArrayarrayWithObj