1.NSArray数组
NSArray是一个有序的静态数组,子类NSMutableArray创建的是动态数组。可以用下标取值。
创建方法
字面值创建,NSArray字面值创建不可以存nil,如类方法arrayWithObjects接收到nil时判定列表结束
//由编译器产生底层创建代码,调用arrayWithObjects:count:方法。
NSArray *array = @[someObject, @"Hello, World!", @42];
等同于
id objects[] = { someObject, @"Hello, World!", @42 };
NSUInteger count = sizeof(objects) / sizeof(id);
NSArray *array = [NSArray arrayWithObjects:objects
count:count];
常用类方法
+ array //用于子类NSMutableArray创建
+ (instancetype)arrayWithArray:(NSArray<ObjectType> *)array;
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...;
常用实例变量方法
- (NSUInteger) count; //获取数组大小
- (ObjectType)objectAtIndex:(NSUInteger)index; //返回索引处对象
- (BOOL)containsObject:(ObjectType)anObject; //查找是否有该对象
- (NSEnumerator<ObjectType> *)objectEnumerator; //枚举器方法
枚举与快速枚举
//枚举器
NSEnumerator *em = [array objectEnumerator];
while (id inde = [em nextObject]){
do someting with inde;
}
//快速枚举
for (NSString * st in array)
{
do something with st;
}
更多内容参考NSArray
2.NSDictionary字典
同样地,NSDictionary是静态不可变的词典,而其子类NSMutableDictionary可变
创建方法
字面值创建
NSDictionary *dictionary = @{
@"anObject" : someObject,
@"helloString" : @"Hello, World!",
@"magicNumber" : @42,
@"aValue" : someValue
};
等同于
id objects[] = { someObject, @"Hello, World!", @42, someValue };
id keys[] = { @"anObject", @"helloString", @"magicNumber", @"aValue" };
NSUInteger count = sizeof(objects) / sizeof(id);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys
count:count];
常用类方法
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects
forKeys:(NSArray<id<NSCopying>> *)keys;
常用实例变量方法
- (ObjectType)objectForKey:(KeyType)aKey;
- (void)setObject:(ObjectType)anObject
forKey:(id<NSCopying>)aKey; //for NSMutableDictionary
- (void)removeObjectForKey:(KeyType)aKey; //for NSMutableDictionary
更多见
3.NSSet
NSSet的内容是无序的,其主要作用是查看某一特定对象是否存在。
常用类方法
+ (instancetype)setWithObjects:(ObjectType)firstObj, ...;
+ (instancetype)setWithArray:(NSArray<ObjectType> *)array;
常用实例变量用法
- (BOOL)containsObject:(ObjectType)anObject;
- (void)addObject:(ObjectType)object; //for NSMutableSet
- (void)removeObject:(ObjectType)object; //for NSMutableSet
更多见
网友评论