{"id":20669,"date":"2024-04-07T12:32:58","date_gmt":"2024-04-07T04:32:58","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=20669"},"modified":"2024-04-07T12:32:58","modified_gmt":"2024-04-07T04:32:58","slug":"ios%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%93%e5%8c%85%e5%8f%91%e9%80%81%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/20669\/","title":{"rendered":"ios\u4e2d\u5982\u4f55\u6253\u5305\u53d1\u9001\u6570\u636e\uff1f"},"content":{"rendered":"

\u5728iOS\u5e94\u7528\u4e2d\uff0c\u6253\u5305\u5e76\u53d1\u9001\u6570\u636e\u901a\u5e38\u662f\u6307\u5c06\u67d0\u4e2a\u7279\u5b9a\u5bf9\u8c61\u5e8f\u5217\u5316\u540e\u53d1\u9001\u5230\u7f51\u7edc\u4e0a\u6216\u8005\u4fdd\u5b58\u5728\u672c\u5730\u78c1\u76d8\u4e0a\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u4f7f\u7528NSCoding\u534f\u8bae\u6765\u5b9e\u73b0\u3002NSCoding\u534f\u8bae\u662fFoios\u6253\u5305\u5931\u8d25<\/a>undation\u6846\u67b6\u4e2d\u5b9a\u4e49\u7684\u4e00\u79cd\u534f\u8bae\uff0c\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61\u3002<\/p>\n

\u5728iOS\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u6253\u5305\u5e76\u53d1\u9001\u5230\u7f51\u7edc\u4e0a\u6216\u8005\u672c\u5730\u78c1\u76d8\u4e0a\u3002\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u4e9b\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n

1. \u5f52\u6863\u548c\u89e3\u6863<\/p>\n

\u5f52\u6863\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u5e8f\u5217\u5316\u5e76\u5199\u5165\u5230\u6587\u4ef6ios\u6781\u5149\u63a8\u9001\u6253\u5305\u6536\u4e0d\u5230\u63a8\u9001<\/a>\u6216\u8005\u5185\u5b58\u4e2d\u7684\u8fc7\u7a0b\u3002\u89e3\u6863\u5219\u662f\u5c06\u5f52\u6863\u540e\u7684\u6570\u636e\u91cd\u65b0\u8fd8\u539f\u6210\u4e00\u4e2a\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002\u4f7f\u7528NSCoding\u534f\u8bae\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n

“`<\/p>\n

\/\/ \u5f52\u6863\u6570\u636e<\/p>\n

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:object];<\/p>\n

[data writeToFile:@”\/path\/to\/file” atomically:YES];<\/p>\n

\/\/ \u89e3\u6863\u6570\u636e<\/p>\n

NSData* data = [NSData dataWithContentsOfFile:@”\/path\/to\/file”];<\/p>\n

id object = [NSKeyedUnarchiver unarchiveObjectWithData:data];<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5b9e\u73b0NSCoding\u534f\u8bae\u4e2d\u7684`encodeWithCoder:`\u548c`initWithCoder:`\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n

“`<\/p>\n

– (void)encodeWithCoder:(NSCoder *)coder {<\/p>\n

[coder encodeObject:self.field1 forKey:@”field1″];<\/p>\n

[coder encodeObject:self.field2 forKey:@”field2″];<\/p>\n

\/\/ … encode other fields …<\/p>\n

}<\/p>\n

– (instancetype)initWithCoder:(NSCoder *)coder {<\/p>\n

self = [super init];<\/p>\n

if (self) {<\/p>\n

self.field1 = [coder decodeObjectForKey:@”field1″];<\/p>\n

self.field2 = [coder decodeObjectForKey:@”field2″];<\/p>\n

\/\/ … decode other fields …<\/p>\n

}<\/p>\n

return self;<\/p>\n

}<\/p>\n

“`<\/p>\n

2. JSON<\/p>\n

JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u670d\u52a1\u5668\u4f20\u8f93\u5230\u5ba2\u6237\u7aef\u3002\u5728iOS\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u8be5\u5b57\u7b26\u4e32\u53d1\u9001\u5230\u7f51\u7edc\u4e0a\u3002\u540c\u6837\u5730\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n

“`<\/p>\n

\/\/ \u5c06\u5bf9\u8c61\u8f6c\u6362\u6210JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/p>\n

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:nil];<\/p>\n

NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];<\/p>\n

\/\/ \u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5bf9\u8c61<\/p>\n

NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];<\/p>\n

id object = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u5bf9\u8c61\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u65b9\u6cd5`JSONObject`\u7528\u4e8e\u5c06\u8be5\u5bf9\u8c61\u8f6c\u6362\u6210\u4e00\u4e2aNSDictionary\u5bf9\u8c61\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u4e3a\u5bf9\u8c61\u5b9e\u73b0\u4e00\u4e2a\u521d\u59cb\u5316\u65b9\u6cd5`initWithJSON:`\u7528\u4e8e\u5c06NSDictionary\u5bf9\u8c61\u8f6c\u6362\u6210\u8be5\u5bf9\u8c61\u3002<\/p>\n

“`<\/p>\n

+ (instancetype)JSONObject:(NSDictionary *)dict {<\/p>\n

MyClass* obj = [[MyClass alloc] init];<\/p>\n

obj.field1 = dict[@”field1″];<\/p>\n

obj.field2 = dict[@”field2″];<\/p>\n

\/\/ … set other fields …<\/p>\n

return obj;<\/p>\n

}<\/p>\n

– (instancetype)initWithJSON:(NSDictionary *)dict {<\/p>\n

self = [super init];<\/p>\n

if (self) {<\/p>\n

self.field1 = dict[@”field1″];<\/p>\n

self.field2 = dict[@”field2″];<\/p>\n

\/\/ … set other fields …<\/p>\n

}<\/p>\n

return self;<\/p>\n

}<\/p>\n

“`<\/p>\n

3. Core Data<\/p>\n

Core Data\u662f\u4e00\u4e2a\u5904\u7406\u5bf9\u8c61\u56fe\u5f62\u7ba1\u7406\u548c\u6301\u4e45\u5316\u7684\u6846\u67b6\u3002\u5728iOS\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Core Data\u5c06\u4e00\u4e2a\u5bf9\u8c61\u4fdd\u5b58\u5728\u672c\u5730\u78c1\u76d8\u4e0a\uff0c\u5e76\u5728\u9700\u8981\u65f6\u68c0\u7d22\u8be5\u5bf9\u8c61\u3002<\/p>\n

“`<\/p>\n

\/\/ \u5c06\u5bf9\u8c61\u4fdd\u5b58\u5230Core Data\u4e2d<\/p>\n

NSManagedObjectContext* context = …;<\/p>\n

NSManagedObject* obj<\/p>\n

<\/figure>\n<\/p>\n

ect = [NSEntityDescription insertNewObjectForEntityForName:@”MyEntity” inManagedObjectContext:context];<\/p>\n

[object setValue:@”value1″ forKey:@”field1″];<\/p>\n

[object setValue:@42 forKey:@”field2″];<\/p>\n

\/\/ … set other fields …<\/p>\n

[context save:nil];<\/p>\n

\/\/ \u4eceCore Data\u4e2d\u68c0\u7d22\u5bf9\u8c61<\/p>\n

NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@”MyEntity”];<\/p>\n

request.predicate = …;<\/p>\n

NSArray* results = [context executeFetchRequest:request error:nil];<\/p>\n

for (NSManagedObject* object in results) {<\/p>\n

NSString* field1 = [object valueForKey:@”field1″];<\/p>\n

NSNumber* field2 = [object valueForKey:@”field2″];<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728iOS\u5e94\u7528\u4e2d\uff0c\u6253\u5305\u5e76\u53d1\u9001\u6570\u636e\u901a\u5e38\u662f\u6307\u5c06\u67d0\u4e2a\u7279\u5b9a\u5bf9\u8c61\u5e8f\u5217\u5316\u540e\u53d1\u9001\u5230\u7f51\u7edc\u4e0a\u6216\u8005\u4fdd\u5b58\u5728\u672c\u5730\u78c1\u76d8\u4e0a\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u4f7f\u7528NSCoding\u534f\u8bae\u6765\u5b9e\u73b0\u3002NSCoding\u534f\u8bae\u662fFoundation\u6846\u67b6\u4e2d\u5b9a\u4e49\u7684\u4e00\u79cd\u534f<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[22636,979,3147,22637,425],"topic":[],"class_list":["post-20669","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-flexios13","tag-979","tag-3147","tag-ios","tag-425"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/20669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=20669"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/20669\/revisions"}],"predecessor-version":[{"id":20747,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/20669\/revisions\/20747"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=20669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=20669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=20669"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=20669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}