概述三种集合类来收集cocoa对象(NSObject对象): NSArray 用于对象有序集合(相当于是数组) NSSet 用于对象无序集合 NSDictionary用于键值映射 以上三种集合类是不可变的(一旦初始化后,就不能改变) 以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类): NSMutableArray NSMutableSet NSMutableDictionary
三种集合类来收集cocoa对象(NSObject对象):
NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)
以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary
注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int,float,double,BOol等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
NSArray
Ordered collection of objects. Immutable. You cannot add or remove objects to it once it’s created.
important methods:
+ (ID)arrayWithObjects:(ID)firstObject,...; // nil terminated
- (int)count;
- (ID)objectAtIndex:(int)index; // Nsstring *s1=[[myarray objectAtIndex: 0];
- (voID)makeObjectsPerformSelector:(SEL)aSelector;
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (ID)lastObject; // returns nil if there are no objects in the array (convenIEnt)
注:
类方法arrayWithObjects 可以创建an autoreleased NSArray of the items.例如
@implementation MyObject
- (NSArray *)coolCats {
return [NSArray arrayWithObjects:@“Steve”,@“Ankush”,@“Sean”,nil];
}
@end
Other convenIEnt 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;
[array release];
//- (unsigned) Count;数组所包含对象个数;
NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
//- (ID) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
NSLog(@"self.dataArray cound 2:%@",[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
//ID obj;
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(int i = 0; i < [oldArray count]; i++)
{
obj = [[oldArray objectAtIndex:i] copy];
[newArray addobject: obj];
}
//
NSLog(@"newArray:%@",newArray);
[newArray release];
//快速枚举
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",nil];
NSLog(@"oldArray:%@",oldArray);
for(ID obj in oldArray)
{
[newArray addobject: obj];
}
//
NSLog(@"newArray:%@",newArray);
[newArray release];
//Deep copy
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",oldArray);
newArray = (NSMutableArray*)CFPropertyListCreateDeepcopy(kcfAllocatorDefault,(CFPropertyListRef)oldArray,kcfPropertyListMutableContainers);
NSLog(@"newArray:%@",newArray);
[newArray release];
//copy and sort
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
NSMutableArray
总结
以上是内存溢出为你收集整理的ios学习--用法总结:NSArray,NSSet,NSDictionary全部内容,希望文章能够帮你解决ios学习--用法总结:NSArray,NSSet,NSDictionary所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)