{"id":650,"date":"2018-02-24T16:03:28","date_gmt":"2018-02-24T08:03:28","guid":{"rendered":"http:\/\/60.205.208.74\/?p=650"},"modified":"2018-02-24T16:03:28","modified_gmt":"2018-02-24T08:03:28","slug":"ios-%e6%95%b0%e6%8d%ae%e6%8c%81%e4%b9%85%e5%8c%96%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=650","title":{"rendered":"ios \u6570\u636e\u6301\u4e45\u5316\u7684\u4e09\u79cd\u65b9\u5f0f"},"content":{"rendered":"<span itemprop=\"description\"><p>IOS\u5e38\u7528\u7684\u4e09\u79cd\u672c\u5730\u6570\u636e\u6301\u4e45\u5316\u65b9\u5f0f:<\/p>\n<p>1\uff1a\u5c5e\u6027\u5e8f\u5217\u5316\u5217\u8868 \u5b58\u50a8\u5230\u6587\u4ef6 plist\uff1b<\/p>\n<p>2\uff1a\u5f52\u6863\u5e8f\u5217\u5316\uff0c\u5bf9\u6a21\u578b\u6570\u636e\u8fdb\u884c\u5f52\u6863\uff1b<\/p>\n<p>3\uff1a\u672c\u5730\u6570\u636e\u5e93\u5b58\u50a8\uff0c\u5d4c\u5165\u5f0fsqlite\uff1b<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e09\u79cd\u65b9\u5f0f\u90fd\u662f\u628a\u6570\u636e\u5b58\u50a8\u5230\u672c\u5730\u6587\u4ef6\u91cc\u8fb9\uff0c\u53ea\u662f\u5b9e\u73b0\u65b9\u5f0f\u548c\u4f7f\u7528\u7684\u573a\u666f\u4e0d\u540c\u800c\u5df2\uff0c\u590d\u6742\u7a0b\u5ea6\u4ece\u4e0a\u5230\u4e0b\u589e\u52a0\u3002<\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u5c31\u662f\u4f7f\u7528NSUserDefaults\uff0c\u5b83\u5df2\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u5c40\u8bbe\u7f6e\uff0c\u5176\u5b9esettings.bundle\u91cc\u8fb9\u7684\u6570\u636e\u5c31\u662f\u5b58\u5728NSUserDefaults\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e3b\u8981\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>\u65b9\u5f0f\u4e00\uff1aplist<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"Objective-C\">\/\/\u5148\u83b7\u53d6plist\u6587\u4ef6\u8def\u5f84\nNSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);\n\tNSString *documentDirectory = [path objectAtIndex:0];\n\tfilePath = [documentDirectory stringByAppendingPathComponent:@\"xxx.plist\"];\n\/\/\u628a\u6570\u636e\u5199\u5165plist\nNSArray *arr = [[NSArray allocl] initWithObjects:A,B,C,nil];\n[arr writeToFile:self.filePath atomically:YES];\n\n\/\/\u4eceplist\u53d6\u6570\u636e\nNSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u65b9\u5f0f\u4e8c\uff1aarchiver<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"Objective-C\">\/\/\u521b\u5efa\u7ee7\u627fNSCoding,NSCopying\u534f\u8bae\u7684\u6570\u636e\u6a21\u578bUser\uff0c\u5fc5\u987b\u5b9e\u73b0\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u6cd5\uff1a\n@synthesize passport;\n@synthesize password;\n@synthesize nickname;\n@synthesize remark;\n\n- (void)encodeWithCoder:(NSCoder *)encoder{\n\t[encoder encodeObject:passport forKey:kPassportKey];\n\t[encoder encodeObject:password forKey:kPasswordKey];\n\t[encoder encodeObject:nickname forKey:kNicknameKey];\n\t[encoder encodeObject:remark forKey:kRemarkKey];\n}\n\n- (id)initWithCoder:(NSCoder *)decoder{\n\tif (self=[super init]) {\n\t\tself.passport = [decoder decodeObjectForKey:kPassportKey];\n\t\tself.password = [decoder decodeObjectForKey:kPasswordKey];\n\t\tself.nickname = [decoder decodeObjectForKey:kNicknameKey];\n\t\tself.remark = [decoder decodeObjectForKey:kRemarkKey];\n\t}\n\treturn self;\n}\n\n- (id)copyWithZone:(NSZone *)zone{\n\tUser *copy = [[[self class] allocWithZone:zone] init];\n\tpassport = [self.passport copy];\n\tpassword = [self.password copy];\n\tnickname = [self.nickname copy];\n\tremark = [self.remark copy];\n\treturn copy;\n}\n\n\/\/\u83b7\u53d6\u5f52\u6863\u6587\u4ef6\n- (NSString *)dataFilePath{\n\tNSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);\n\treturn [(NSString *)[arr objectAtIndex:0] stringByAppendingPathComponent:@\"archiver\"];\n}\n\n\/\/\u521d\u59cb\u5316\u6570\u636e\u6a21\u578b\u5e76\u5199\u5165\u5f52\u6863\u6587\u4ef6\nUser *user = [[User alloc] init];\n\tuser.passport = @\"alec030711\";\n\tuser.password = @\"abcd\";\n\tuser.nickname = @\"alecchyi\";\n\tuser.remark = @\"fuck\";\n\t\n\tNSMutableData *data = [[NSMutableData alloc] init];\n\tNSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];\n\t[archiver encodeObject:user forKey:@\"kData\"];\n\t[archiver finishEncoding];\n\t[data writeToFile:[self dataFilePath] atomically:YES];\n\t[archiver release];\n\t[data release];\n\t[user release];\n\n\/\/\u4ece\u5f52\u6863\u6587\u4ef6\u8bfb\u6570\u636e\nNSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];\n\tNSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];\n\tUser *user = [unArchiver decodeObjectForKey:@\"kData\"];\n\t[unArchiver finishDecoding];\n\n\t[data release];\n\t[unArchiver release];\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u65b9\u5f0f\u4e09\uff1asqlite<\/p>\n<pre class=\"Objective-c\">\tsqlite3 *database;\n\t\/\/\u6253\u5f00\u6570\u636e\u5e93\uff0c\u6ca1\u6709\u5219\u521b\u5efa\n\tif (sqlite3_open([\u6570\u636e\u5e93\u6587\u4ef6\u8def\u5f84 UTF8String], &amp;database) != SQLITE_OK) {\n\t\tsqlite3_close(database);\n\t}\n\t\/\/\u521b\u5efa\u8868\n\tchar *errorStr;\n\tNSString *createTable = @\"drop table if exists users;create table if not exists users (id integer primary key,nickname text);\";\n\tif (sqlite3_exec(database,[createTable UTF8String],NULL,NULL,&amp;errorStr)!=SQLITE_OK) {\n\t\tsqlite3_close(database);\n\t}\n\t\/\/\u63d2\u5165\u6570\u636e\n\tfor (int x=1; x&lt;5; x++) {\n\t\tNSString *insert = [[NSString alloc] initWithFormat:@\"insert or replace into users (id,nickname) values (%d,'ruby%d')\",x,x];\n\t\tif (sqlite3_exec(database,[insert UTF8String],NULL,NULL,&amp;errorStr)!=SQLITE_OK) {\n\t\t\tsqlite3_free(database);\n\t\t}\n\t\t[insert release];\n\t}\n\t\/\/\u67e5\u8be2\u8868\u6570\u636e\n\tNSString *query = @\"select nickname,id from users\";\n\tsqlite3_stmt *statememt;\n\tif (sqlite3_prepare_v2(database,[query UTF8String],-1,&amp;statememt,nil)==SQLITE_OK) {\n\t\twhile (sqlite3_step(statememt)==SQLITE_ROW) {\n\t\t\tchar *rowData = (char *)sqlite3_column_text(statememt,0);\n\t\t\tNSString *nickname = [[NSString alloc] initWithUTF8String:rowData];\n\t\t\t[nickname release];\n\t\t}\n\t}\n\tsqlite3_finalize(statememt);\n\t\/\/\u5173\u95ed\u6570\u636e\u5e93\n\tsqlite3_close(database);\n\t\n\t\/\/\u5173\u952e\u5c31\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u548c\u51e0\u4e2a\u8bed\u53e5\uff1asqlite3_open, sqlite3_exec,sqlite3_prepare_v2  sqlite3_step sqlite_finalize,sqlite3_close\u3002<\/pre>\n<p>\u65b9\u5f0f\u4e09\u9700\u8981\u5f15\u5165libsqlite3.lib\uff0c\u7531\u4e8esqlite\u6570\u636e\u5e93\u662fC\u5b9e\u73b0\u7684\uff0c\u5b58\u50a8\u4e5f\u662fC\u5f62\u5f0f\u5b58\u50a8\uff0c\u6240\u4ee5\u5728\u53d6\u51fa\u6570\u636e\u65f6\u9700\u8981\u7528UTF8String\u8f6c\u6362\u6210objecitve-c\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32<\/p>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">IOS\u5e38\u7528\u7684\u4e09\u79cd\u672c\u5730\u6570\u636e\u6301\u4e45\u5316\u65b9\u5f0f: 1\uff1a\u5c5e\u6027\u5e8f\u5217\u5316\u5217\u8868 \u5b58\u50a8\u5230\u6587\u4ef6 plist\uff1b 2\uff1a\u5f52&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=650\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26,12],"tags":[74,93,116,138,139],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","category-blog","tag-ios","tag-mobile","tag-sqlite","tag-138","tag-139","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}