iOS -copy-mutableCopy- NSHashTable -NSMapTable-Keychain

1、对于非容器对象(NSString)

不可变 (NSString)

copy :浅复制,指针指向

mutableCopy:深复制,生成可变字符串对象

可变(NSMutableString)

copy: 深复制,生成不可变字符串对象

mutableCopy :深复制,生成可变字符串对象

2、对于容器类对象 数组

不可变(NSArray)

copy :浅复制,指针指向

mutableCopy:深复制,生成可变数组对象(但,数组中的对象没有进行处理的话,数组的中的对象是浅复制)

可变(NSMutableArray)

copy :深复制,生成不可变数组对象

mutableCopy:深复制,生成不可变数组对象

test:

    NSString *str1 = @"123";
    NSLog(@"%p",str1);
    NSString *str2 = [str1 copy];
    NSLog(@"%p",str2);
    NSMutableString *str3 = [str1 mutableCopy];
    NSLog(@"%p",str3);
    
    NSMutableString *str4 = [[NSMutableString alloc]initWithString:@"123"];
    NSLog(@"%p",str4);
    NSString *str5 = [str4 copy];
    NSLog(@"%p",str5);
    NSMutableString *str6 = [str4 mutableCopy];
    NSLog(@"%p",str6);
    
    NSArray *array1 = [[NSArray alloc] initWithObjects:@3,@4, nil];
    NSLog(@"%p",array1);
    NSArray *array2 = [array1 copy];
    NSLog(@"%p",array2);
    NSMutableArray *array3 = [array1 mutableCopy];
    NSLog(@"%p",array3);
    
    NSMutableArray *array4 = [[NSMutableArray alloc]initWithObjects:@2,@3, nil];
    NSLog(@"%p",array4);
    NSArray *array5 = [array4 copy];
    NSLog(@"%p",array5);
    NSMutableArray *array6 = [array4 mutableCopy];
    NSLog(@"%p",array6);
2016-08-23 10:54:50.878 [1364:147504] 0x10409cc90
2016-08-23 10:54:53.636 [1364:147504] 0x10409cc90
2016-08-23 10:54:53.636 [1364:147504] 0x7fd602d1c330

2016-08-23 10:54:54.322 [1364:147504] 0x7fd602dae190
2016-08-23 10:54:54.323 [1364:147504] 0xa000000003332313
2016-08-23 10:54:54.323 [1364:147504] 0x7fd602e07420

2016-08-23 10:54:55.924 [1364:147504] 0x7fd602d00d60
2016-08-23 10:54:56.750 [1364:147504] 0x7fd602d00d60
2016-08-23 10:54:56.750 [1364:147504] 0x7fd602c11050

2016-08-23 10:54:56.751 [1364:147504] 0x7fd602c2bfc0
2016-08-23 10:54:56.751 [1364:147504] 0x7fd602c0a070
2016-08-23 10:54:56.751 [1364:147504] 0x7fd602c0a220

3、NSHashTable -NSMapTable

NSHashTable 和 -NSMapTable是iOS 6 后出现的容器类对象,他有一个特点就是,对加入其中的对象,可以设置其为弱引用,

而不是像数组等容器加入对象后默认对象引用计数会加1,强引用,有点类似(weak,assgin)的容器

- (instancetype)initWithOptions:(NSPointerFunctionsOptions)options capacity:(NSUInteger)initialCapacity NS_DESIGNATED_INITIALIZER;

typedef NS_OPTIONS(NSUInteger, NSPointerFunctionsOptions) {
    // Memory options are mutually exclusive
    
    // default is strong
    NSPointerFunctionsStrongMemory NS_ENUM_AVAILABLE(10_5, 6_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 NS_ENUM_AVAILABLE(10_5, 6_0) = (2UL << 0),
    NSPointerFunctionsMallocMemory NS_ENUM_AVAILABLE(10_5, 6_0) = (3UL << 0),       // free() will be called on removal, calloc on copyIn
    NSPointerFunctionsMachVirtualMemory NS_ENUM_AVAILABLE(10_5, 6_0) = (4UL << 0),
    NSPointerFunctionsWeakMemory NS_ENUM_AVAILABLE(10_8, 6_0) = (5UL << 0),         // uses weak read and write barriers appropriate for ARC or GC
    
    // Personalities are mutually exclusive
    // default is object.  As a special case, 'strong' memory used for Objects will do retain/release under non-GC
    NSPointerFunctionsObjectPersonality NS_ENUM_AVAILABLE(10_5, 6_0) = (0UL << 8),         // use -hash and -isEqual, object description
    NSPointerFunctionsOpaquePersonality NS_ENUM_AVAILABLE(10_5, 6_0) = (1UL << 8),         // use shifted pointer hash and direct equality
    NSPointerFunctionsObjectPointerPersonality NS_ENUM_AVAILABLE(10_5, 6_0) = (2UL << 8),  // use shifted pointer hash and direct equality, object description
    NSPointerFunctionsCStringPersonality NS_ENUM_AVAILABLE(10_5, 6_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 NS_ENUM_AVAILABLE(10_5, 6_0) = (4UL << 8),         // use a memory hash and memcmp (using size function you must set)
    NSPointerFunctionsIntegerPersonality NS_ENUM_AVAILABLE(10_5, 6_0) = (5UL << 8),        // use unshifted value as hash & equality

    NSPointerFunctionsCopyIn NS_ENUM_AVAILABLE(10_5, 6_0) = (1UL << 16),      // the memory acquire function will be asked to allocate and copy items on input
};

4、iOS 中的Keychain API

ios 中的keychian 应该是一个系统级的单例,在里面存的信息,安全的程度比较高,

不同的app可以通过相同的key,度取相关的信息,简单实现不同app之间的信息共享一种思路

原文地址:https://www.cnblogs.com/muzijun/p/5798422.html