{"id":8,"date":"2016-11-23T22:49:40","date_gmt":"2016-11-23T14:49:40","guid":{"rendered":"http:\/\/blog.fly-kite.com\/?p=8"},"modified":"2016-11-23T23:43:05","modified_gmt":"2016-11-23T15:43:05","slug":"ios%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e4%b8%ad%e8%8b%b1%e6%96%87%e6%b7%b7%e5%90%88%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%ad%97%e8%8a%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/flykite.me\/?p=8","title":{"rendered":"iOS\u5982\u4f55\u7edf\u8ba1\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u5b57\u7b26\u4e32\u5b57\u8282\u6570"},"content":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u670d\u52a1\u7aef\u4f7f\u7528\u4e86varchar\u6765\u4fdd\u5b58\u5b57\u7b26\u4e32\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u76f4\u63a5\u7528\u5b57\u7b26\u4e32\u7684.length\u5c5e\u6027\u83b7\u5f97\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u8bdd\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b57\u7b26\u4e32\u5728\u670d\u52a1\u7aef\u6570\u636e\u5e93\u4e2d\u88ab\u622a\u65ad\u3002\u6bd4\u5982varchar(256)\u53ef\u4ee5\u5b58\u50a8128\u4e2a\u6c49\u5b57\u6216\u8005256\u4e2a\u82f1\u6587\u5b57\u6bcd\uff0c\u800c\u4f60\u4e0a\u4f20\u7684\u5b57\u7b26\u4e32\u8d85\u8fc7\u4e86128\u4e2a\u6c49\u5b57\u7684\u957f\u5ea6\u3002\u4f46\u662f\u6211\u4eec\u4e5f\u4e0d\u80fd\u76f4\u63a5\u5224\u65ad\u5b57\u7b26\u4e32\u7684length\u662f\u5426\u5c0f\u4e8e\u7b49\u4e8e128\uff0c\u56e0\u4e3a\u5f53\u5b57\u7b26\u4e32\u4e3a\u82f1\u6587\u65f6\u6700\u591a\u662f\u53ef\u4ee5\u8f93\u5165256\u4e2a\u82f1\u6587\u5b57\u6bcd\u7684\u3002<\/p>\n<p>\u56e0\u6b64\u5176\u5b9e\u6211\u4eec\u60f3\u8981\u7684\u662f\u201c\u4e00\u4e2a\u6c49\u5b57\u6216\u8005\u4e24\u4e2a\u82f1\u6587\u5b57\u6bcd\u8ba1\u957f\u5ea6\u4e3a1\u201d\uff0c\u5728\u65b0\u6d6a\u5fae\u535a\u7684APP\u7684\u53d1\u5e03\u5fae\u535a\u754c\u9762\u91cc\u5c31\u662f\u8fd9\u6837\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u8981\u5982\u4f55\u5b9e\u73b0\u8fd9\u6837\u7684\u6548\u679c\u5462\uff1f\u5148\u4e0a\u4ee3\u7801~<\/p>\n<blockquote><p>NSString *text = @&#8221;\u4f60\u597dHello World!&#8221;;<br \/>\nNSInteger length = [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];<br \/>\nlength -= (length &#8211; text.length) \/ 2;<br \/>\nlength = (length +1) \/ 2;<br \/>\nNSLog(@&#8221;%ld&#8221;, (long)length);<\/p><\/blockquote>\n<p>\u6b64\u5904\u8f93\u51fa\u7ed3\u679c\u4e3a8\uff0c\u5982\u679c\u76f4\u63a5\u4f7f\u7528<code>text.length<\/code>\u7684\u8bdd\u5f97\u5230\u7684\u4f1a\u662f14\u3002<\/p>\n<p>\u8fd9\u91cc\u5229\u7528\u4e86<code>NSString<\/code>\u7684<code>lengthOfBytesUsingEncoding:<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6839\u636e\u4f60\u4f20\u5165\u7684\u53c2\u6570\uff08\u4e5f\u5c31\u662f\u5b57\u7b26\u7f16\u7801\uff09\u6765\u8ba1\u7b97\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>\u5728UTF8\u7f16\u7801\u4e2d\uff0c\u4e00\u4e2a\u82f1\u6587\u5b57\u6bcd\u5360\u75281\u4e2a\u5b57\u8282\uff0c\u4e00\u4e2a\u6c49\u5b57\u4f1a\u5360\u75283\u4e2a\u5b57\u8282\uff0c\u4e0e\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u6709\u4e9b\u504f\u5dee\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u8ba1\u7b97\u6765\u5f97\u5230\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002<\/p>\n<h1>\u5570\u55e6\u7684\u89e3\u91ca\uff08\u65b9\u4fbf\u5c0f\u767d\u7406\u89e3\uff09<\/h1>\n<p>\u9996\u5148\u662f<code>length -= (length - text.length) \/ 2;<\/code>\u8fd9\u4e00\u884c\uff0c\u7b80\u5355\u70b9\u89e3\u91ca\u5c31\u662f@&#8221;\u54c8ab&#8221;\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684length\u4e3a3\uff0c\u800c\u901a\u8fc7<code>lengthOfBytesUsingEncoding:<\/code>\u65b9\u6cd5\u5f97\u5230\u7684\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7528UTF8\u7f16\u7801\u6240\u5360\u5185\u5b58\u4e3a5\u4e2a\u5b57\u8282\uff0c\u51cf\u53bblength\u4e4b\u540e\u5f97\u52302\uff0c\u6b64\u65f6\u7ed3\u679c\u5c31\u662f\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u7684\u6c49\u5b57\u6240\u5360\u5185\u5b58\u7684\u5927\u5c0f\u4e86~\uff08@&#8221;\u4f60\u597dabc&#8221;\u7ecf\u8fc7\u8fd9\u9053\u8ba1\u7b97\u540e\u5f97\u52304\uff0c@&#8221;\u54c8\u54c8\u54c81234&#8243;\u7ecf\u8fc7\u8ba1\u7b97\u540e\u5f97\u52306\uff09\u3002<br \/>\n\u8fd9\u65f6\u5019\u7528\u8fd9\u4e2a\u7ed3\u679c\u9664\u4ee52\u81ea\u7136\u5c31\u5f97\u5230\u4e86\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u7684\u6c49\u5b57\u4e2a\u6570\u4e86\uff0cUTF8\u7f16\u7801\u91cc\u6c49\u5b57\u53603\u4e2a\u5b57\u8282\uff0c\u4f46\u662f\u6211\u4eec\u60f3\u8981\u8ba1\u7b97\u7684\u662f\u4e00\u4e2a\u6c49\u5b57\u53602\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u518d\u7528\u6574\u4f53\u7684UTF8\u7f16\u7801\u6240\u5360\u5185\u5b58\u5927\u5c0f\u51cf\u53bb\u6c49\u5b57\u4e2a\u6570\u5c31\u5f97\u5230\u4e86\u201c\u4e00\u4e2a\u6c49\u5b57\u53602\u4e2a\u5b57\u8282\uff0c\u4e00\u4e2a\u82f1\u6587\u5b57\u6bcd\u53601\u4e2a\u5b57\u8282\u201d\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4f46\u662f\u6211\u4eec\u60f3\u8981\u7684\u662f\u201c\u4e00\u4e2a\u6c49\u5b57\u6216\u8005\u4e24\u4e2a\u82f1\u6587\u5b57\u6bcd\u8ba1\u957f\u5ea6\u4e3a1\u201d\u8fd9\u6837\u7684\u6548\u679c\uff0c\u6240\u4ee5\u7528\u4e0a\u8fb9\u7684\u7ed3\u679c+1\u518d\u9664\u4ee5\u4e8c\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u7ec8\u6548\u679c\u5566\u3002<br \/>\n\u4e3a\u4ec0\u4e48\u8981+1\u800c\u4e0d\u662f\u76f4\u63a5\u9664\u4ee5\u4e8c\u5462\uff1f\u56e0\u4e3a\u5f53length\u4e3a\u5076\u6570\u65f6\u9664\u4ee5\u4e8c\u7684\u786e\u662f\u5f97\u5230\u4e86\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u5f53length\u4e3a\u5947\u6570\u65f6\u5c31\u4f1a\u51fa\u9519\uff0c\u6bd4\u5982\u5f53\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e3a@&#8221;\u54c8a&#8221;\u65f6\uff0c\u7ecf\u8fc7\u524d\u9762\u7684\u8ba1\u7b97\u540e\u5f97\u5230\u7684\u7ed3\u679c\u662f3\uff0c\u76f4\u63a5\u9664\u4ee5\u4e8c\u7684\u7ed3\u679c\u662f1\uff08\u5982\u679c\u8fd9\u90fd\u4e0d\u660e\u767d\u662f\u4e3a\u5565\u7684\u8bdd\u4f60\u9700\u8981\u53bb\u6076\u8865\u4e00\u4e0b\u59ff\u52bf\u4e86\uff09\uff0c\u800c\u5148+1\u518d\u9664\u4ee5\u4e8c\u4e4b\u540e\u5f97\u5230\u7684\u5c31\u662f\u6b63\u786e\u7684\u4e86\u3002\u800c\u4e14\u4e0d\u4f1a\u5f71\u54cdlength\u4e3a\u5076\u6570\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u670d\u52a1\u7aef\u4f7f\u7528\u4e86varchar\u6765\u4fdd\u5b58\u5b57\u7b26\u4e32\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u76f4\u63a5\u7528\u5b57\u7b26\u4e32\u7684.length\u5c5e\u6027\u83b7\u5f97 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":1,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":10,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/10"}],"wp:attachment":[{"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}