NSDictionary/NSMutableArray淺析
我們?cè)谑褂肗SDictionary/NSMutableArray時(shí),通常會(huì)使用NSString對(duì)象作為key考蕾,因?yàn)閗ey必須遵循NSCopying協(xié)議,見(jiàn)NSMutableArray中的方法:
- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey;
在NSDictionary/NSMutableArray對(duì)象中船庇,aKey對(duì)象被copy一份后存入懈糯,anObject對(duì)象則被強(qiáng)引用。來(lái)看一段代碼:
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
{
NSString *aKey = @"akey";
NSObject *aObject = [[NSObject alloc] init];
[aDictionary setObject:aObject forKey:aKey];
}
NSLog(@"dictionary: %@", aDictionary);
打印日志:
dictionary: {
akey = "<NSObject: 0x60400001d3b0>";
}
本來(lái)作用域結(jié)束后搔扁,aKey變量指向的NSString對(duì)象(簡(jiǎn)稱(chēng)aKey對(duì)象)和aObject變量指向的NSObject對(duì)象(簡(jiǎn)稱(chēng)aObject對(duì)象)應(yīng)該被自動(dòng)釋放,但是aDictionary變量指向的NSMutableDictionary對(duì)象(簡(jiǎn)稱(chēng)aDictionary對(duì)象)持有一份aObject對(duì)象的強(qiáng)引用蟋字,所以打印日志時(shí)阁谆,aDictionary對(duì)象不為空。
現(xiàn)在有一個(gè)Teacher類(lèi)表示班主任信息愉老,包含姓名name屬性和年齡age屬性,另有一個(gè)Student類(lèi)表示學(xué)生信息剖效,也包含姓名name屬性和年齡age屬性嫉入。那么一個(gè)班包含一個(gè)班主任(Teacher對(duì)象)和n個(gè)學(xué)生(Student數(shù)組),為了統(tǒng)計(jì)一個(gè)班的信息璧尸,需要把班主任和學(xué)生的信息及對(duì)應(yīng)關(guān)系保存下來(lái)咒林。
// Teacher.h
@interface Teacher : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
// Teacher.m
@implementation Teacher
@end
// Student.h
@interface Student : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
// Student.m
@implementation Student
@end
// ViewController.m
{
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
Teacher *teacher = [[Teacher alloc] init];
teacher.name = @"teacher";
teacher.age = 30;
NSMutableArray *aArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 3; i++) {
Student *student = [[Student alloc] init];
student.name = [NSString stringWithFormat:@"student%d", i];
student.age = i;
[aArray addObject:student];
}
[aDictionary setObject:aArray forKey:teacher.name];
NSLog(@"%@", aDictionary);
}
打印日志:
dictionary:{
teacher = (
"<Student: 0x60000003aa00>",
"<Student: 0x60000003a9c0>",
"<Student: 0x60000003aa80>"
);
}
這里將班主任的姓名作為key,這樣會(huì)損失其他信息爷光。如果想要將teacher對(duì)象作為key垫竞,則需要讓Teacher類(lèi)遵循NSCopying協(xié)議,而且NSDictionary/NSMutable使用hash表來(lái)實(shí)現(xiàn)key和value之間的映射和存儲(chǔ),所以作為key值的類(lèi)型必須重寫(xiě)hash和isEqual:兩個(gè)方法欢瞪,其中hash方法計(jì)算該對(duì)象的hash值活烙,hash值決定該對(duì)象在hash表中存儲(chǔ)的位置,isEqual方法通過(guò)hash值來(lái)定位對(duì)象在hash表中的位置遣鼓。具體代碼如下:
// Teacher.h
@interface Teacher : NSObject<NSCopying>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
// Teacher.m
@implementation Teacher
- (id)copyWithZone:(NSZone *)zone
{
Teacher *teacher = [[Teacher allocWithZone:zone] init];
teacher.name = self.name;
teacher.age = self.age;
return teacher;
}
- (BOOL)isEqual:(id)object
{
// 比較hash值是否相等
return [self hash] == [object hash];
}
- (NSUInteger)hash
{
// 調(diào)用父類(lèi)的hash方法啸盏,也可以自定義
return [super hash];
}
打印日志:
dictionary:{
"<Teacher: 0x600000027940>" = (
"<Student: 0x60c00022fa20>",
"<Student: 0x60c00022fa00>",
"<Student: 0x60c00022f960>"
);
}
NSMapTable淺析
NSMapTable繼承自NSObject,自iOS6.0開(kāi)始使用骑祟,NSMapTable是可變的回懦。
NS_CLASS_AVAILABLE(10_5, 6_0)
@interface NSMapTable<KeyType, ObjectType> : NSObject <NSCopying, NSCoding, NSFastEnumeration>
NSMapTable有兩個(gè)指定初始化方法和一個(gè)便捷初始化方法:
// 指定初始化方法
- (instancetype)initWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions capacity:(NSUInteger)initialCapacity NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithKeyPointerFunctions:(NSPointerFunctions *)keyFunctions valuePointerFunctions:(NSPointerFunctions *)valueFunctions capacity:(NSUInteger)initialCapacity NS_DESIGNATED_INITIALIZER;
// 便捷初始化方法
+ (NSMapTable<KeyType, ObjectType> *)mapTableWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions;
初始化方法方法中有兩個(gè)參數(shù)keyOptions和valueOptions,都是NSPointerFunctionsOptions類(lèi)型次企,NSPointerFunctionsOptions是一個(gè)枚舉類(lèi)型怯晕,
typedef NS_OPTIONS(NSUInteger, NSPointerFunctionsOptions) {
// Memory options are mutually exclusive
// default is strong
NSPointerFunctionsStrongMemory API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (0UL << 0), // use strong write-barrier to backing store; use GC memory on copyIn
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || TARGET_OS_WIN32
NSPointerFunctionsZeroingWeakMemory NS_ENUM_DEPRECATED_MAC(10_5, 10_8) = (1UL << 0), // deprecated; uses GC weak read and write barriers, and dangling pointer behavior otherwise
#endif
NSPointerFunctionsOpaqueMemory API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (2UL << 0),
NSPointerFunctionsMallocMemory API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (3UL << 0), // free() will be called on removal, calloc on copyIn
NSPointerFunctionsMachVirtualMemory API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (4UL << 0),
NSPointerFunctionsWeakMemory API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)) = (5UL << 0), // uses weak read and write barriers appropriate for ARC
// Personalities are mutually exclusive
// default is object. As a special case, 'strong' memory used for Objects will do retain/release under non-GC
NSPointerFunctionsObjectPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (0UL << 8), // use -hash and -isEqual, object description
NSPointerFunctionsOpaquePersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (1UL << 8), // use shifted pointer hash and direct equality
NSPointerFunctionsObjectPointerPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (2UL << 8), // use shifted pointer hash and direct equality, object description
NSPointerFunctionsCStringPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (3UL << 8), // use a string hash and strcmp, description assumes UTF-8 contents; recommended for UTF-8 (or ASCII, which is a subset) only cstrings
NSPointerFunctionsStructPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (4UL << 8), // use a memory hash and memcmp (using size function you must set)
NSPointerFunctionsIntegerPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (5UL << 8), // use unshifted value as hash & equality
NSPointerFunctionsCopyIn API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = (1UL << 16), // the memory acquire function will be asked to allocate and copy items on input
};
常用的枚舉值及對(duì)應(yīng)的含義如下:
- NSPointerFunctionsStrongMemory: 強(qiáng)引用存儲(chǔ)對(duì)象
- NSPointerFunctionsWeakMemory: 弱引用存儲(chǔ)對(duì)象
- NSPointerFunctionsCopyIn:copy存儲(chǔ)對(duì)象
就是說(shuō),如果NSMapTable的初始化方法為:
NSMapTable *aMapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn valueOptions:NSPointerFunctionsStrongMemory capacity:0];
或
NSMapTable *aMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsCopyIn valueOptions:NSPointerFunctionsStrongMemory];
那么就等同于NSMutableDictionay的初始化方法:
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
或
NSMutableDictionary *aDictionary = [NSMutableDictionary dictionary];
若初始方法修改為
NSMapTable *aMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsStrongMemory];
即對(duì)key值進(jìn)行弱引用缸棵,就可以不用讓Teacher類(lèi)遵循NSCopying協(xié)議和重新跟hash有關(guān)的兩個(gè)方法舟茶,代碼如下:
// Teacher.h
@interface Teacher : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
// Teacher.m
@implementation Teacher
@end
// ViewController.m
Teacher *teacher = [[Teacher alloc] init];
teacher.name = @"teacher";
teacher.age = 30;NSMutableArray *aArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < 3; i++) {
Student *student = [[Student alloc] init];
student.name = [NSString stringWithFormat:@"student%d", i];
student.age = i;
[aArray addObject:student];
}
NSMapTable *aMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsStrongMemory];
[aMapTable setObject:aArray forKey:teacher];
NSLog(@"%@", aMapTable);
打印日志:
NSMapTable {
[10] <Teacher: 0x604000038a40> -> (
"<Student: 0x60400003c640>",
"<Student: 0x60400003c660>",
"<Student: 0x60400003c620>"
)
}
這樣的方法可以快速的將NSObject對(duì)象作為key存入到“字典”中。
由于NSDictionary/NSMutableArray會(huì)強(qiáng)引用value蛉谜,使得value的引用計(jì)數(shù)+1稚晚,加入不希望怎么做,可以用NSMapTable來(lái)實(shí)現(xiàn)型诚。
NSMapTable *aMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory];
{
NSObject *keyObject = [[NSObject alloc] init];
NSObject *valueObject = [[NSObject alloc] init];
[aMapTable setObject:valueObject forKey:keyObject];
NSLog(@"NSMapTable:%@", aMapTable);
}
NSLog(@"NSMapTable:%@", aMapTable);
打印日志:
NSMapTable:NSMapTable {
[6] <NSObject: 0x60c00000c690> -> <NSObject: 0x60c00000c730>
}
NSMapTable:NSMapTable {
}
第一個(gè)NSLog打印出了key-value值客燕,等到object對(duì)象指向的NSObject對(duì)象超出作用域,釋放該對(duì)象狰贯,由于aMapTable弱引用object對(duì)象也搓,aMapTable的中的key-value值會(huì)被安全的刪除,第二個(gè)NSLog打印出的值為空涵紊。
NSMapTable與NSDictionary/NSMutableDictionary對(duì)比
- NSDcitionary有一個(gè)可變類(lèi)型NSMutableDictionary傍妒,NSMapTable沒(méi)有可變類(lèi)型,它本身就是可變的;
- NSDcitionary/NSMutableDictionary中對(duì)于key和value的內(nèi)存管理方法唯一摸柄,即對(duì)key進(jìn)行copy颤练,對(duì)value進(jìn)行強(qiáng)引用,而NSMapTable沒(méi)有限制驱负;
- NSDcitionary中對(duì)key值進(jìn)行copy嗦玖,不可改變,通常用字符串作為key值跃脊,只是key->object的映射宇挫,而NSMapTable的key是可變的對(duì)象,既可以實(shí)現(xiàn)key->object的映射酪术,又可以實(shí)現(xiàn)object->object的映射器瘪。
遺留的問(wèn)題
筆者才疏學(xué)淺,對(duì)NSMapTable與NSDictionary的內(nèi)部結(jié)構(gòu)了解不是很深,不清楚key-value是通過(guò)怎么樣的方式綁定起來(lái)的橡疼,假如看到這篇文章的朋友有所了解援所,希望可以指點(diǎn)一二。