{"id":457,"date":"2014-11-13T09:40:20","date_gmt":"2014-11-13T01:40:20","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=457"},"modified":"2016-07-15T17:32:02","modified_gmt":"2016-07-15T09:32:02","slug":"ios%e5%88%9b%e5%bb%ba%e5%8d%95%e4%be%8b%e7%9a%84%e4%b8%a4%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/11\/13\/094020457.html","title":{"rendered":"iOS\u521b\u5efa\u5355\u4f8b\u7684\u4e24\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p><strong>1\u3001\u82f9\u679c\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u5199\u6cd5<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">static AccountManager *DefaultManager = nil;  \n   \n+ (AccountManager *)defaultManager {  \n    if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init];  \n    return DefaultManager;  \n}<\/pre>\n<p><strong>2\u3001iOS4\u4e4b\u540e\u6709\u4e86\u53e6\u5916\u4e00\u79cd\u5199\u6cd5\uff1a<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">+ (AccountManager *)sharedManager  \n{  \n        static AccountManager *sharedAccountManagerInstance = nil;  \n        static dispatch_once_t predicate;  \n        dispatch_once(&predicate, ^{  \n                sharedAccountManagerInstance = [[self alloc] init];   \n        });  \n    return sharedAccountManagerInstance;  \n}<\/pre>\n<p><strong>3\u3001\u8be5\u5199\u6cd5\u6765\u81ea objcolumnist\uff0c\u6587\u4e2d\u63d0\u5230\uff0c\u8be5\u5199\u6cd5\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u6027\uff1a<\/strong><!--more--><\/p>\n<p>1. \u7ebf\u7a0b\u5b89\u5168\u3002<br \/>\n2. \u6ee1\u8db3\u9759\u6001\u5206\u6790\u5668\u7684\u8981\u6c42\u3002<br \/>\n3. \u517c\u5bb9\u4e86ARC<\/p>\n<p>\u7136\u540e\u6211\u8fd8\u6709\u70b9\u597d\u5947\u7684\u662fdispatch_once,\u8fd9\u4e2a\u51fd\u6570\uff0c\u6ca1\u89c1\u8fc7\u554a\u3002<br \/>\n\u4e8e\u662f\u5c31\u5230\u5b98\u65b9\u7684\u6587\u6863\u91cc\u627e\u627e\u770b\uff0c\u662f\u600e\u4e48\u8bf4\u7684\u3002<br \/>\n\u4e0b\u9762\u662f\u5b98\u65b9\u6587\u6863\u4ecb\u7ecd\uff1a<\/p>\n<p>dispatch_once<br \/>\nExecutes a block object once and only once for the lifetime of an application.<br \/>\n  void dispatch_once(<br \/>\n    dispatch_once_t *predicate,<br \/>\n    dispatch_block_t block);<br \/>\n<strong>Parameters<\/strong><br \/>\npredicate<br \/>\nA pointer to a dispatch_once_t structure that is used to test whether the block has completed or not.<br \/>\nblock<br \/>\nThe block object to execute once.<br \/>\n<strong>Discussion<\/strong><br \/>\nThis function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.<br \/>\nIf called simultaneously from multiple threads, this function waits synchronously until the block has completed.<br \/>\nThe predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined.<br \/>\nAvailability<br \/>\nAvailable in iOS 4.0 and later.<br \/>\nDeclared In<br \/>\ndispatch\/once.h<\/p>\n<p>\u6211\u4eec\u770b\u5230\uff0c\u8be5\u65b9\u6cd5\u7684\u4f5c\u7528\u5c31\u662f\u6267\u884c\u4e14\u5728\u6574\u4e2a\u7a0b\u5e8f\u7684\u58f0\u660e\u5468\u671f\u4e2d\uff0c\u4ec5\u6267\u884c\u4e00\u6b21\u67d0\u4e00\u4e2ablock\u5bf9\u8c61\u3002\u7b80\u76f4\u5c31\u662f\u4e3a\u5355\u4f8b\u800c\u751f\u7684\u561b\u3002\u800c\u4e14\uff0c\u6709\u4e9b\u6211\u4eec\u9700\u8981\u5728\u7a0b\u5e8f\u5f00\u5934\u521d\u59cb\u5316\u7684\u52a8\u4f5c\uff0c\u5982\u679c\u4e3a\u4e86\u4fdd\u8bc1\u5176\uff0c\u4ec5\u6267\u884c\u4e00\u6b21\uff0c\u4e5f\u53ef\u4ee5\u653e\u5230\u8fd9\u4e2adispatch_once\u6765\u6267\u884c\u3002<br \/>\n\u7136\u540e\u6211\u4eec\u770b\u5230\u5b83\u9700\u8981\u4e00\u4e2a\u65ad\u8a00\u6765\u786e\u5b9a\u8fd9\u4e2a\u4ee3\u7801\u5757\u662f\u5426\u6267\u884c\uff0c\u8fd9\u4e2a\u65ad\u8a00\u7684\u6307\u9488\u8981\u4fdd\u5b58\u8d77\u6765\uff0c\u76f8\u5bf9\u4e8e\u7b2c\u4e00\u79cd\u65b9\u6cd5\u800c\u8a00\uff0c\u8fd8\u9700\u8981\u591a\u4fdd\u5b58\u4e00\u4e2a\u6307\u9488\u3002<\/p>\n<p>\u65b9\u6cd5\u7b80\u4ecb\u4e2d\u5c31\u8bf4\u7684\u5f88\u6e05\u695a\u4e86\uff1a\u5bf9\u4e8e\u5728\u5e94\u7528\u4e2d\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5316\u4e00\u4e2a\u5168\u5c40\u7684\u6570\u636e\u5bf9\u8c61\uff08\u5355\u4f8b\u6a21\u5f0f\uff09\uff0c\u8fd9\u4e2a\u51fd\u6570\u5f88\u6709\u7528\u3002<br \/>\n\u5982\u679c\u540c\u65f6\u5728\u591a\u7ebf\u7a0b\u4e2d\u8c03\u7528\u5b83\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c06\u7b49\u5f85\u540c\u6b65\u7b49\u5f85\uff0c\u76f4\u81f3\u8be5block\u8c03\u7528\u7ed3\u675f\u3002<br \/>\n\u8fd9\u4e2a\u65ad\u8a00\u7684\u6307\u9488\u5fc5\u987b\u8981\u5168\u5c40\u5316\u7684\u4fdd\u5b58\uff0c\u6216\u8005\u653e\u5728\u9759\u6001\u533a\u5185\u3002\u4f7f\u7528\u5b58\u653e\u5728\u81ea\u52a8\u5206\u914d\u533a\u57df\u6216\u8005\u52a8\u6001\u533a\u57df\u7684\u65ad\u8a00\uff0cdispatch_once\u6267\u884c\u7684\u7ed3\u679c\u662f\u4e0d\u53ef\u9884\u77e5\u7684\u3002<\/p>\n<p><strong>4\u3001\u9879\u76ee\u4e2d\u5e38\u89c1\u7684\u5199\u6cd5\uff0c\u91cd\u5199allocWithZone:\u65b9\u6cd5<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">+ (id)allocWithZone:(struct _NSZone *)zone\n{\n    static SADownload *instance = nil;\n    static dispatch_once_t predicate;\n    dispatch_once(&predicate, ^{\n        instance = [super allocWithZone:zone];\n    });\n    return instance;\n}<\/pre>\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<p>1.\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5728\u521b\u5efa\u5355\u4f8b\u6216\u8005\u67d0\u4e9b\u521d\u59cb\u5316\u52a8\u4f5c\u65f6\u4f7f\u7528\uff0c\u4ee5\u4fdd\u8bc1\u5176\u552f\u4e00\u6027\u3002<br \/>\n2.\u8be5\u65b9\u6cd5\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u8bf7\u653e\u5fc3\u5927\u80c6\u7684\u5728\u5b50\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u3002\uff08\u524d\u63d0\u662f\u4f60\u7684dispatch_once_t *predicate\u5bf9\u8c61\u5fc5\u987b\u662f\u5168\u5c40\u6216\u8005\u9759\u6001\u5bf9\u8c61\u3002\u8fd9\u4e00\u70b9\u5f88\u91cd\u8981\uff0c\u5982\u679c\u4e0d\u80fd\u4fdd\u8bc1\u8fd9\u4e00\u70b9\uff0c\u4e5f\u5c31\u4e0d\u80fd\u4fdd\u8bc1\u8be5\u65b9\u6cd5\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\u3002\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u82f9\u679c\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u5199\u6cd5 static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } 2\u3001iOS4\u4e4b\u540e\u6709\u4e86\u53e6\u5916\u4e00\u79cd\u5199\u6cd5\uff1a + (AccountManager *)sharedManager { static AccountManager *sharedAccountManagerInstance = nil; static dispatch_once_t predicate; dispatch_once(&#038;predicate, ^{ sharedAccountManagerInstance = [[self alloc] init]; }); return sharedAccountManagerInstance; } 3\u3001\u8be5\u5199\u6cd5\u6765\u81ea objcolumnist\uff0c\u6587\u4e2d\u63d0\u5230\uff0c\u8be5\u5199\u6cd5\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u6027\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20],"class_list":["post-457","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=457"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/457\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}