{"id":971,"date":"2021-03-15T10:54:48","date_gmt":"2021-03-15T02:54:48","guid":{"rendered":"http:\/\/www.mustenaka.cn\/?p=971"},"modified":"2021-03-15T15:03:00","modified_gmt":"2021-03-15T07:03:00","slug":"oc-nsstring-introducensstring","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2021\/03\/15\/oc-nsstring-introducensstring\/","title":{"rendered":"OC &#8211; NSString \u5b57\u7b26\u4e32\u7c7b\u8be6\u89e3"},"content":{"rendered":"<p>Objective-C \u7684 NSString\u662f\u8be5\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u6a21\u5757\uff0c\u5c31\u5b57\u7b26\u4e32\u800c\u8a00C\u8bed\u8a00\u4e5f\u6709\u81ea\u5df1\u7684\u5b57\u7b26\u4e32\u6a21\u5757\u5c01\u88c5\uff0c\u4f46NSString\u4ee5\u5bf9\u8c61\u7684\u65b9\u5f0f\u5c01\u88c5\uff0c\u800c\u4e14\u652f\u6301\u5404\u79cd\u7f16\u7801\uff0c\u7b97\u662f\u4e00\u4e2a\u5f3a\u5316\u7248\u672c\u7684\u5b57\u7b26\u4e32\u6a21\u5757\u4e86\uff0c\u4f46\u662f\u548c\u539f\u59cbC\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u76f8\u6bd4\uff0c\u901f\u5ea6\u6162\u4e8610\u500d\u6709\u4f59\u3002<\/p>\n<p>OC\u4e2dNSString\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u53ef\u4ee5\u5982\u4e0b\u3002\u5947\u602a\u7684\u662f\uff0c\u767e\u5ea6\u4e0a\u8bf4\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u521b\u5efa\u901f\u5ea6\u66f4\u5feb\uff0c\u7136\u800c\u6211\u901a\u8fc71e5\uff0c1e6\uff0c1e7\u4e09\u6b21\u8fed\u4ee3\u6d4b\u8bd5\u53d1\u73b0\u76f4\u63a5\u5f15\u7528\u7684\u521b\u5efa\u65b9\u6cd5\u66f4\u5feb\uff0c\u5f53\u7136\u4e86\u8fd9\u4e24\u79cd\u90fd\u8fdc\u8fdc\u4e0d\u5982C\u8bed\u8a00\u7248\u672cchar *str\u76f4\u63a5\u65b9\u6cd5\u7a7f\u4ef6\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" null=\"\">NSString *str = @\"balabalabala\";\r\nNSString *Str = [[NSString alloc] initWithString:@\"balabalabala\"];<\/code><\/pre>\n<h5>1.\u64cd\u4f5cUnicode\u7f16\u7801\u5b57\u7b26\u4e32<\/h5>\n<p>NSString\u4e2d\u7684\u6c49\u5b57\u90fd\u662f\u7528Unicode\u8868\u793a\uff0c\uff0c\u4e0b\u9762\u6458\u6284\u4e86\u4e00\u4e9b\u4e0d\u9519\u7684API\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662flength\u8fd4\u56de\u7f16\u7801\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u5bf9\u4e8eC\u8bed\u8a00\u4e2d\u5982\u679c\u8f93\u5165\u7684\u662f\u4e2d\u6587\u4ea4\u7ed9\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u8be5\u5b57\u7b26\u4e32\u7684length\u4f1adouble\uff0c\u56e0\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5360\u7528\u4e24\u4e2achar\uff0c\u800c\u4e14\u4e5f\u76f8\u5f53\u4e0d\u597d\u8fdb\u884c\u4e00\u4e9breversed order\u8fd9\u7c7b\u578b\u7684\u64cd\u4f5c\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" objectivec=\"\">-(id) initWithUTF8String: (const char *) bytes\r\n\/*\u4ee5\u4eceNULL\u7ed3\u675f\u7684UTF8\u7f16\u7801\u7684C\u5b57\u7b26\u4e32\u4e2d\u590d\u5236\u4fe1\u606f\uff0c\u5e76\u4e14\u521d\u59cb\u5316\u63a5\u53d7\u8005*\/\r\n\r\n-(__strong const char *)UTF8String\r\n\/*\u8fd4\u56de\u7f16\u7801\u4e3aUTF\uff0c\u4ee5NULL\u7ed3\u5c3e\u7684C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u6307\u9488\u3002\u57fa\u4e8e\u5f15\u7528\u8ba1\u6570\u7684\u5185\u5b58\u7ba1\u7406\u6a21\u5f0f\u4e0b\uff0c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4f1a\u5728\u6d88\u606f\u63a5\u6536\u72ec\u4eab\u88ab\u91ca\u653e\u7684\u540c\u4e8b\u88ab\u91ca\u653e\u6389\uff0c\u5982\u679c\u60f3\u5728\u5bf9\u8c61\u88ab\u91ca\u653e\u4e4b\u540e\u4e5f\u80fd\u4f7f\u7528\uff0c\u5c31\u9700\u8981\u989d\u5916\u590d\u5236\u4e00\u4efd\u3002GC\u7684\u5185\u5b58\u7ba1\u7406\u6a21\u5f0f\u4e0b\uff0c\u56e0\u4e3a\u8fd4\u56de\u7684\u6307\u9488\u662f\u5f3a\u6307\u9488\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88abGC\u56de\u6536*\/\r\n\r\n-(NSUInteger) length\r\n\/*\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2dUnicode\u7f16\u7801\u7684\u5b57\u7b26\u4e2a\u6570\u3002\u548cC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u4e0d\u540c\uff0c\u4e0d\u80fd\u7528\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u503c\u6765\u8ba1\u7b97\u9700\u8981\u7684\u5b57\u8282\u6570\u548c\u8868\u793a\u65f6\u9700\u8981\u7684 \u957f\u5ea6 *\/\r\n\r\n\r\n\/\/\u2026\u2026and more<\/code><\/pre>\n<h5>2.\u7f16\u7801\u98ce\u683c\u8f6c\u6362<\/h5>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7f16\u7801\u8f6c\u6362\u7684API\uff0c\u6ce8\u610fascii\u7684\u5b9a\u4e49\u6709\u7684\u65f6\u5019\u662f\u7528\u662f\u65e7\u7684char\u65b9\u6cd5\uff087bits\uff09\uff0c\u800c\u6709\u65f6\u5019\u5219\u662f\u65b0\u7248\u7684unsigned char\u65b9\u6cd5(8bits)\uff0c\u5173\u4e8eUTF\uff0c\u4e5f\u67098bit\u548c16bit\u7248\u672c\u7684\u533a\u5206\uff0c\u8bf7\u6ce8\u610f\u7ec6\u8282\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"224\"><span>NSASCIIStringEncoding<\/span><\/td>\n<td width=\"224\"><span>7<\/span><span>\u4f4d\u7248\u7684<\/span><span>ASCII<\/span><span>\u7f16\u7801\uff08\u4f4e<\/span><span>7<\/span><span>\u4f4d\u8868\u793a\u4e0d\u540c\u7684<\/span><span>ASCII<\/span><span>\u5b57\u7b26\uff0c\u5b57\u8282\u7684\u6700\u9ad8\u4f4d\u56fa\u5b9a\u4e3a<\/span><span>0<\/span><span>\uff09<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSNEXTSTEPStringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u7248\u7684<\/span><span>ASCII<\/span><span>\u7f16\u7801\uff08<\/span><span>NEXTSTEP<\/span><span>\u6269\u5c55\uff09<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSUnicodeStringEncoding<\/span><\/td>\n<td width=\"224\"><span>\u6b63\u89c4\u7684<\/span><span>Unicode<\/span><span>\u7f16\u7801\uff08<\/span><span>UTF-16<\/span><span>\uff09<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSUTF8StringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u7684<\/span><span>Unicode<\/span><span>\u5b57\u7b26\u4e32\uff08<\/span><span>UTF-8<\/span><span>\uff09<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSNonLossyASCIIStringEncoding<\/span><\/td>\n<td width=\"224\"><span>7<\/span><span>\u4f4d\u7684\u65e0\u635f<\/span><span>Unicode<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSISOLatin1StringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u7684<\/span><span>ISO Latin 1<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSISOLatin2StringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u7684<\/span><span>ISO Latin 2<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSMacOSRomanStringEncoding<\/span><\/td>\n<td width=\"224\"><span>Mac OS<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSWindowsCP1252StringEncoding Windows<\/span><\/td>\n<td width=\"224\"><span>Windows Latin1<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSWindowsCP1250StringEncoding Windows<\/span><\/td>\n<td width=\"224\"><span>Windows Latin2<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSSymbolStringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d<\/span><span> Adobe Symbol<\/span><span>\u7f16\u7801\u5411\u91cf<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSJapaneseEUCStringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u65e5\u8bed<\/span><span>EUC<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSShiftJISStringEncoding<\/span><\/td>\n<td width=\"224\"><span>8<\/span><span>\u4f4d\u65e5\u8bed<\/span><span>Shift-JIS<\/span><span>\u7f16\u7801<\/span><\/td>\n<\/tr>\n<tr>\n<td width=\"224\"><span>NSISO2022JPStringEncoding<\/span><\/td>\n<td width=\"224\"><span>ISO 2022<\/span><span>\u7535\u5b50\u90ae\u4ef6\u7528\u65e5\u8bed\u7f16\u7801<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3.\u751f\u6210\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/h5>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662fUnicode\u548cascii\u7801\u5360\u636e\u7a7a\u95f4\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u6bd4\u5982\u6211\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\uff0c\u5206\u522b\u6d4b\u8bd5\u4e00\u4e0b\u6b63\u786e\u548c\u9519\u8bef\u7684\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" objectivec=\"\">int main(int argc, const char * argv[]) {\r\n    NSString *str = @\"\u600e\u4e48\u54ea\u91cc\u90fd\u6709\u7838\u603b\u7684\uff0cWD\uff01\";\r\n    NSLog(@\"%@\",str);\r\n    unsigned long cnt = [str length];\r\n    for(unsigned int i = 0; i &lt; cnt; i++){\r\n        unichar c = [str characterAtIndex:i];   \/\/\u6b63\u786e\u65b9\u6cd5\r\n        \/\/char c = [str characterAtIndex:i];    \/\/\u9519\u8bef\u65b9\u6cd5\r\n        \/*\r\n         \u4e2d\u6587\u51fa\u9519\u539f\u56e0\u731c\u6d4b\uff0c\u539f\u672cNSString\u91c7\u7528Unicode\u683c\u5f0f16bit\uff0c\u800cC\u8bed\u8a00\u7684char\u91c7\u75287bit\u683c\u5f0f\r\n         \u76f4\u63a5[str characterAtIndex]\u8fd4\u56deUnicode\u683c\u5f0f\u7684\u4e00\u4e2a\u5b57\u7b26\uff0c\u5360\u636e16bit\uff0c\u5982 5000 1000 0000 0005\r\n         \u800cchar\u53ea\u63a5\u53d78bit\u4f4d\uff0c\u53d1\u751f\u4e86\u622a\u65ad \u5982\u4e0a\u6570\u636e\u5c31\u53ea\u63a5\u53d7\u4e86 0000 0005\r\n         \u800cUnicode\u5728\u4f4e\u4f4d\u517c\u5bb9ASCII\u7801\uff0c\u56e0\u6b64\u82f1\u6587\u7684\u8bfb\u53d6\u662f\u6b63\u5e38\u7684\r\n         \u89e3\u51b3\u601d\u60f3\uff1a\u4eceOC\u7684\u6807\u51c6\u5e93\u4e2d\u627e\u5230\u4e00\u4e2a\u80fd\u591f\u5355\u72ec\u63d0\u53d6UTF8\u683c\u5f0f\u5355\u4e2a\u5b57\u7b26\u7684\u5bb9\u5668\uff0c\u5229\u7528\u5b83\u8fdb\u884c\u8f93\u51fa\r\n         \u89e3\u51b3\u65b9\u6cd5\uff1a\u7528unichar\u4ee3\u66ffchar\uff0c\u7528%C\u4ee3\u66ff%c\r\n         *\/\r\n        NSLog(@\"\u5b57\u7b26\u4e32\u7b2c %d \u4f4d \u4e3a %C\",i,c);       \/\/\u6b63\u786e\u65b9\u6cd5\r\n        \/\/NSLog(@\"\u5b57\u7b26\u4e32\u7b2c %d \u4f4d \u4e3a %c\",i,c);     \/\/\u9519\u8bef\u65b9\u6cd5\r\n    }\r\n    return 0;\r\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5e38\u89c1\u7684\u683c\u5f0f\u800c\u8a00\uff0c\u53ef\u4ee5\u53c2\u8003\u5982\u4e0b\u5185\u5bb9\uff0c\u91cd\u70b9\u6ce8\u610f\u5982\u4e0b\u4e24\u4e2a\u70b9<\/p>\n<ol>\n<li>%@\u5bf9\u5e94\u7684\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u5f53\u8f93\u51fa%@\u65f6\uff08\u5217\u5982\uff1aNSLog(@&#8221;%@&#8221;, test);\uff09\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8c03\u7528\u4e86test\u7684description\u65b9\u6cd5\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u50a8\u5b58\u5230%@\u4e2d\u3002description\u65b9\u6cd5\u662fNSObject\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u4efb\u4f55\u5bf9\u8c61\u90fd\u53ef\u4ee5\u8c03\u7528\u5b83\u3002description\u8fd4\u56de\u7684\u5185\u5bb9\u548c\u7c7b\u7684\u5b9e\u73b0\u76f8\u5173\u3002<\/li>\n<li>C\u8bed\u8a00\u7684printf()\u4e2d\uff0c\u7f16\u8bd1\u5668\u652f\u6301\u53c2\u6570\u7c7b\u578b\u7684\u81ea\u52a8\u8f6c\u6362\uff0c\u5217\u5982\u4f60\u53ef\u4ee5\u5427int\u5b9a\u4e49\u7684\u6570\u5b57\u7528%.f\u8f93\u51fa\uff0c\u53cd\u4e4b\u7528%f\u8f93\u5165\u7684\u6570\u5b57\u4e5f\u53ef\u4ee5\u4ea4\u7ed9\u4e00\u4e2a\u6574\u578b\u3002\u800cOC\u7684NSString\u5219\u4e0d\u652f\u6301\u8fd9\u79cd\u53c2\u6570\u7c7b\u578b\u7684\u81ea\u52a8\u8f6c\u6362\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u4fdd\u8bc1\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u548c\u6240\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p>NSLog\u5e38\u7528\u7684\u6807\u8bb0\u7b26\uff0cNSLog\u548cNSString\u662f\u7ed3\u5408\u6700\u5bc6\u5207\u7684\u4e24\u4e2a\u7c7b\u4e86\uff0c\u6ce8\u610f%@\u8868\u793a<strong>\u5bf9\u8c61<\/strong>\uff0c\u7528NSString\u751f\u6210\u7684\u5b57\u7b26\u4e32\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5e94\u8be5\u4f7f\u7528%@\u800c\u4e0d\u80fd\u50cfc\u8bed\u8a00\u5b57\u7b26\u4e32\u90a3\u6837%s.<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" objectivec=\"\">\/*\r\n========== NSLog\u4f7f\u7528 ==========\r\n    \/\/    %@        \u5bf9\u8c61\r\n    \/\/    %d, %i    \u6574\u6570\r\n    \/\/    %u          \u65e0\u7b26\u6574\u578b\r\n    \/\/    %f           \u6d6e\u70b9\/\u53cc\u5b57\r\n    \/\/    %x, %X      \u4e8c\u8fdb\u5236\u6574\u6570\r\n    \/\/    %o          \u516b\u8fdb\u5236\u6574\u6570\r\n    \/\/    %zu        size_t\r\n    \/\/    %p          \u6307\u9488\r\n    \/\/    %e          \u6d6e\u70b9\/\u53cc\u5b57 \uff08\u79d1\u5b66\u8ba1\u7b97\uff09\r\n    \/\/    %g          \u6d6e\u70b9\/\u53cc\u5b57\r\n    \/\/    %s C      \u5b57\u7b26\u4e32\r\n    \/\/    %.*s        Pascal\u5b57\u7b26\u4e32\r\n    \/\/    %c           \u5b57\u7b26\r\n    \/\/    %C          unichar\r\n    \/\/    %lld        64\u4f4d\u957f\u6574\u6570\uff08long long\uff09\r\n    \/\/    %llu        \u65e0\u7b2664\u4f4d\u957f\u6574\u6570\r\n    \/\/    %Lf         64\u4f4d\u53cc\u5b57\r\n*\/<\/code><\/pre>\n<p>\u7279\u522b\u6ce8\u610f\u4e00\u4e0b\u8fd9\u4e2aAPI<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" objectivec=\"\">-(id) initWithFormat:(NSString *) format, ...\r\n\/*\r\n\u6839\u636eformat\u4e2d\u6307\u5b9a\u7684\u683c\u5f0f\u4e32\u6765\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6765\u521d\u59cb\u5316\u6d88\u606f\u63a5\u53d7\u8005\u3002format\u4e2d\u53c2\u6570\u7528\u9017\u53f7\u5206\u9694\uff0c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e0d\u80fd\u4e3a\u7a7a\u3002\r\n\u4fbf\u5229\u6784\u9020\u5668\uff1astringWithFormat:\r\n*\/<\/code><\/pre>\n<p>\u6ce8\uff1a\u548cC\u8bed\u8a00\u4e00\u6837\uff0cOC\u4e5f\u652f\u6301\u53ef\u53d8\u53c2\u6570\uff0c\u7528&#8230;\u8868\u793a\uff0c\u8be6\u60c5\u53ef\u4ee5\u770b\u8fd9\u4e2ablog\uff1a<a href=\"http:\/\/c.biancheng.net\/view\/344.html\">http:\/\/c.biancheng.net\/view\/344.html<\/a><\/p>\n<p>\u5176\u4ed6\u529f\u80fd\u8bf8\u5982\uff1aNSString\u7684\u6bd4\u8f83\uff0c\u8ffd\u52a0\u622a\u53d6\uff0c\u68c0\u7d22\u548c\u7f6e\u6362\uff0c\u5927\u5c0f\u5199\u5904\u7406\uff0c\u6570\u503c\u8f6c\u6362\uff0c\u6587\u4ef6\u8def\u5f84\u5904\u7406\uff08&#8221;Foundation\/NSPathUtilities.h&#8221;\uff09\u7b49\u5185\u5bb9\u53ef\u4ee5\u81ea\u5df1\u7ffb\u9605API\uff0cOC\u7684\u4e00\u4e2a\u98ce\u683c\u70b9\u5c31\u662f\u6c47\u603b\u7684\u597d\uff0c\u6309\u7167\u5927\u529f\u80fd\u6a21\u5757\u5408\u5e76<\/p>\n<h5>n.\u6587\u4ef6\u8f93\u5165\u8f93\u51fa<\/h5>\n<p>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff0c\u4e5f\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\u3002\u53e6\u5916\uff0c\u4e0b\u9762\u8fd9\u4e9b\u540c\u6587\u4ef6\u76f8\u5173\u7684\u65b9\u6cd5\u90fd\u5e26\u6709\u4e00\u4e2aerror\u53c2\u6570\uff0c\u5e76\u628a\u53d1\u751f\u9519\u8bef\u65f6\u7684\u4fe1\u606f\u5199\u5165\u5230error\u4e2d\u3002error\u4e0d\u80fd\u4e3aNULL\uff0c\u5426\u5219\u5c31\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u6709\u5982\u4e0b\u4e24\u4e2aAPI\u5e38\u7528\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" objectivec=\"\">-(id) initWithContentsOfFile : (NSString *) path\r\n                               encoding : (NSStringEncoding) enc\r\n                                      error : (NSError **) error\r\n\/*\r\n\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6path\u4e2d\u7684\u5185\u5bb9\u6765\u521d\u59cb\u5316\u4e00\u4e2aNSString\uff0c\u6587\u4ef6\u7684\u7f16\u7801\u4e3aenc\u3002\u8bfb\u53d6\u6587\u4ef6\u5931\u8d25\u7684\u65f6\u5019\u4f1a\u91ca\u653e\u8c03\u7528\u8005\uff0c\u5e76\u5728\u8fd4\u56denil\u7684\u540c\u4e8b\u5c06\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u8bbe\u5b9a\u5230error\u3002\r\n\u4fbf\u5229\u6784\u9020\u5668\uff1astringWithContentsOfFile : encoing : error:\r\n*\/\r\n\r\n-(id) initWithContentsOfFile : (NSString *) path\r\n                      usedEncoding : (NSStringEncoding) enc\r\n                                      error : (NSError **) error\r\n\/*\r\n\u548c\u4e0a\u4e00\u4e2a\u65b9\u6cd5\u4e00\u6837\uff0c\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6path\u4e2d\u7684\u5185\u5bb9\u6765\u521d\u59cb\u5316\u4e00\u4e2aNSString\u3002\u4e0d\u540c\u7684\u5730\u65b9\u5728\u4e8e\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u81ea\u52a8\u5224\u522b\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u5e76\u901a\u8fc7enc\u8fd4\u56de\u3002\u6587\u4ef6\u7684\u7f16\u7801\u662f\u901a\u8fc7\u6587\u4ef6\u7684\u5185\u5bb9\u548c\u6587\u4ef6\u7684\u6269\u5c55\u5c5e\u6027(extended attribute)\u6765\u5224\u65ad\u7684\u3002\r\n\u66f4\u591a\u8bf7\u53c2\u8003\u547d\u4ee4xattr\r\n*\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Objective-C \u7684 NSString\u662f\u8be5\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u6a21\u5757\uff0c\u5c31\u5b57\u7b26\u4e32\u800c\u8a00C\u8bed\u8a00\u4e5f\u6709\u81ea\u5df1\u7684\u5b57\u7b26\u4e32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,88,43],"tags":[87,89],"class_list":["post-971","post","type-post","status-publish","format-standard","hentry","category-macos","category-objective-c","category-programming-language","tag-macos","tag-objective-c"],"views":1637,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/comments?post=971"}],"version-history":[{"count":10,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/971\/revisions"}],"predecessor-version":[{"id":982,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/971\/revisions\/982"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}