用法总结:NSArray,NSSet,NSDictionary NSArray 用于对象有序集合(数组) =================== NSArray ==================== Ordered collection of objects. Immutable(you cannot add or remove objects to it once it’s created) - (BOOL)containsObject:(id)anObject; // 当anObject出现在array中,则返回yes(实际是通过isEqual:方法来判断) - (unsigned)indexOfObject:(id)anObject; // 查找array中的anObject,并返回其最小索引值。没找到返回NSNotFound. - (void)makeObjectsPerformSelector:(SEL)aSelector; - (NSArray *)sortedArrayUsingSelector:(SEL)aSelector; self.dataArray = array; NSLog(@"self.dataArray count is:%d",[self.dataArray count]); NSLog(@"self.dataArray index 2 is:%@",[self.dataArray objectAtIndex:2]); 注:初始化一个数组时,经常使用initWithObject 和 initWithObjects。没有s的表示使用一个指针初始化NSArray,初始化后,这个NSArray的实例只有一个数据,所以在调用没有s的函数的时候是不能以nil结束的;但是有s的就不一样了,哪怕只有一个数据,或者没有数据,必须使用一个nil结束。 ------ 从一个数组拷贝数据到另一数组(可变数级) ------- //arrayWithArray: array1 = [NSArray arrayWithArray:array]; //Copy //id obj; NSLog(@"oldArray:%@",oldArray); //快速枚举 for(id obj in oldArray) //Deep copy //NSMutableArray *newArray = [[NSMutableArray alloc] init]; =================== NSMutableArray ==================== - (void)insertObject:(id)anObject atIndex:(int)index; //在索引index处插入anObject, 若index被占用,会把之后的object向后移。 - (void)removeObjectAtIndex:(int)index; //删除index处的对象,后面的对象依次向前移。 - (void)removeObject:(id)anObject; // 删除所有和anObject相等的对象,同样使用isEqual:作为相等比较方法. 注:我们不能把nil加到array中。但有时候我们真想给array加一个空的对象,可以使用NSNull来做这件事。如: [myArray addObject:[NSNull null]]; -----给数组分配容量----- -----在数组末尾添加对象----- -----删除数组中指定索引处对象----- ----- 数组枚举----- id thingie; id object; ----- NSValue(对任何对象进行包装)----- ----★使用NSMutableArray要防止内存泄露★------ [objectsArray addObject:p1]; //同样做数组替换时 //执行清空数组 由此可知,每次执行上面的数组操作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。 =================== NSSet ====================
- (int)count;
- (BOOL)containsObject:(id)anObject; - (id)anyObject; - (void)makeObjectsPerformSelector:(SEL)aSelector; - (id)member:(id)anObject; // uses isEqual: and returns a matching object (if any) =================== NSMutableSet ==================== Mutable version of NSSet. 可修改的集合。主要用于集合运算(并集,交集,差集) + (NSMutableSet *)set;
- (void)addObject:(id)anObject;
- (void)removeObject:(id)anObject;
- (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 Important methods: + dictionaryWithObjectsAndKeys: (id)firstObject, ...;
- (int)count; // 返回有多少对key-value
- (id)objectForKey:(id)key; //返回和key相关联的value。 如果没有和key相关联的value,则返回nil。 - (NSArray *)allKeys; // 返回一个包含所有key的array - (NSArray *)allValues;
- NSEnumerator *)keyEnumerator; //这个方法是从一个 dictionary 中得到一个 key 的迭代器.
Enumerators 也就是 iterators 或 enumerations.我们可以使用它来一步一步迭代出集合中的所 有成员. 下面是我们可能使用它来列 举所有的 key- ‐vaule 对 for (NSString *s in e) { NSLog(@"key is %@, value is %@", s, [myDict objectForKey:s]); } 注:NSArray 也有一个类似的方法得到 array 的成员迭代 器 : objectEnumerator
-----创建字典 -----
//- (id) initWithObjectsAndKeys; //NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil]; ================= NSMutableDictionary ================== Mutable version of NSDictionary.
NSMutableDictionary是NSDictionary的子类。创建后,允许用户添加和删除key和value。
+ (NSMutableDictionary *)dictionary; // 创建一个空的dictionary
- (void)
setObject:(id)anObject
forKey:(id)key; // 使用key和anObject组成一条记录添加到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); |
NSArray,NSSet,NSDictionary
最新推荐文章于 2024-07-11 23:45:43 发布
用法总结:NSArray,NSSet,NSDictionary |