{"id":6283,"date":"2024-07-10T14:35:17","date_gmt":"2024-07-10T06:35:17","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6283"},"modified":"2024-07-10T14:35:17","modified_gmt":"2024-07-10T06:35:17","slug":"objective-c-%e4%b8%ad%e7%9a%84%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6283","title":{"rendered":"Objective-C \u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u975e\u539f\u521b\u3001Gemini \u95ee\u7b54\u751f\u6210<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objective-C \u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7528\u4e8e\u5f00\u53d1 macOS\u3001iOS\u3001watchOS \u548c tvOS \u5e94\u7528\u7a0b\u5e8f\u3002\u5e76\u53d1\u7f16\u7a0b\u662f Objective-C \u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4e3b\u9898\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Objective-C \u5e76\u53d1\u7f16\u7a0b\u9762\u8bd5\u9898\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. \u89e3\u91ca\u5e76\u53d1\u7f16\u7a0b\u548c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. \u63cf\u8ff0\u51e0\u79cd\u5e38\u89c1\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. \u89e3\u91ca Objective-C \u4e2d\u7684\u9501\u673a\u5236\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u9501\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. \u63cf\u8ff0 GCD\uff08Grand Central Dispatch\uff09\u6846\u67b6\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528 GCD \u6765\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5. \u89e3\u91ca NSOperation \u548c NSOperationQueue \u7c7b\u7684\u7528\u6cd5\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6. \u63cf\u8ff0\u5982\u4f55\u5728 Objective-C \u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>7. \u89e3\u91ca\u6b7b\u9501\u7684\u6982\u5ff5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u907f\u514d\u6b7b\u9501\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>8. \u63cf\u8ff0\u5982\u4f55\u5728 Objective-C \u4e2d\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>9. \u89e3\u91ca\u5982\u4f55\u5728 Objective-C \u4e2d\u5904\u7406\u5e76\u884c\u4efb\u52a1\u7684\u5b8c\u6210\u60c5\u51b5\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>10. \u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728 Objective-C \u4e2d\u4f7f\u7528\u5e76\u53d1\u7f16\u7a0b\u6765\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9488\u5bf9\u7279\u5b9a\u9762\u8bd5\u9898\u7684\u66f4\u8be6\u7ec6\u89e3\u7b54\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. \u89e3\u91ca\u5e76\u53d1\u7f16\u7a0b\u548c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u53d1\u7f16\u7a0b\u662f\u6307\u5141\u8bb8\u591a\u4e2a\u4efb\u52a1\u6216\u7a0b\u5e8f<strong>\u540c\u65f6<\/strong>\u6267\u884c\uff0c\u800c<strong>\u4e0d<\/strong>\u8981\u6c42\u5b83\u4eec<strong>\u5fc5\u987b<\/strong>\u540c\u65f6\u6267\u884c\u3002\u5e76\u53d1\u7f16\u7a0b\u7684\u91cd\u70b9\u662f\u63d0\u9ad8\u8d44\u6e90\u5229\u7528\u7387\uff0c\u4f8b\u5982\u8ba9\u591a\u4e2a\u4efb\u52a1\u5171\u4eab CPU \u65f6\u95f4\u6216\u5176\u4ed6\u8d44\u6e90\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7a0b\u5e8f<strong>\u540c\u65f6<\/strong>\u6267\u884c\u591a\u4e2a<strong>\u7ebf\u7a0b<\/strong>\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u72ec\u7acb\u8def\u5f84\uff0c\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684\u4efb\u52a1\u6808\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u91cd\u70b9\u662f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u6548\u7387\uff0c\u4f8b\u5982\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7b80\u800c\u8a00\u4e4b\uff0c\u5e76\u53d1\u7f16\u7a0b\u662f\u5b8f\u89c2\u6982\u5ff5\uff0c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u5fae\u89c2\u5b9e\u73b0\u3002<\/strong> \u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u5229\u7528\u591a\u7ebf\u7a0b\u7b49\u6280\u672f\u6765\u5b9e\u73b0\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6280\u672f\uff0c\u4f8b\u5982\u8fdb\u7a0b\u3001\u534f\u7a0b\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. \u63cf\u8ff0\u51e0\u79cd\u5e38\u89c1\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e9b\u5e38\u89c1\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u6838\u5904\u7406\u5668\uff1a<\/strong> \u73b0\u4ee3 CPU \u901a\u5e38\u5177\u6709\u591a\u4e2a\u6838\u5fc3\uff0c\u6bcf\u4e2a\u6838\u5fc3\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u65f6\u95f4\u7247\u8f6e\u8f6c\uff1a<\/strong> \u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5728\u5404\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5206\u914d CPU \u65f6\u95f4\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u80fd\u83b7\u5f97\u8fd0\u884c\u673a\u4f1a\u3002<\/li>\n\n\n\n<li><strong>\u9501\uff1a<\/strong> \u9501\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u8d44\u6e90\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u3002<\/li>\n\n\n\n<li><strong>\u4fe1\u53f7\u91cf\uff1a<\/strong> \u4fe1\u53f7\u91cf\u7528\u4e8e\u63a7\u5236\u8d44\u6e90\u7684\u8bbf\u95ee\u6570\u91cf\uff0c\u4f8b\u5982\u9650\u5236\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570\u3002<\/li>\n\n\n\n<li><strong>\u6d88\u606f\u4f20\u9012\uff1a<\/strong> \u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u6d88\u606f\u4f20\u9012\u6765\u8fdb\u884c\u901a\u4fe1\u548c\u534f\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. \u89e3\u91ca Objective-C \u4e2d\u7684\u9501\u673a\u5236\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u9501\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objective-C \u63d0\u4f9b\u591a\u79cd\u9501\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e92\u65a5\u9501 (Mutex)\uff1a<\/strong> \u4e92\u65a5\u9501\u786e\u4fdd\u4e00\u6b21\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u8bfb\u5199\u9501 (ReadWriteLock)\uff1a<\/strong> \u8bfb\u5199\u9501\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\u5171\u4eab\u8d44\u6e90\uff0c\u4f46\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5199\u5165\u5171\u4eab\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u6761\u4ef6\u9501 (ConditionLock)\uff1a<\/strong> \u6761\u4ef6\u9501\u5141\u8bb8\u7ebf\u7a0b\u7b49\u5f85\u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e92\u65a5\u9501\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objective-C<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@interface MyClass : NSObject {\n  NSMutableString *_sharedString;\n  NSLock *_lock;\n}\n\n@property (nonatomic, strong) NSMutableString *sharedString;\n\n- (instancetype)init;\n\n- (void)appendString:(NSString *)string;\n\n@end\n\n@implementation MyClass\n\n- (instancetype)init {\n  if (self = &#91;super init]) {\n    _sharedString = &#91;&#91;NSMutableString alloc] init];\n    _lock = &#91;&#91;NSLock alloc] init];\n  }\n  return self;\n}\n\n- (void)appendString:(NSString *)string {\n  &#91;_lock lock];\n  &#91;_sharedString appendString:string];\n  &#91;_lock unlock];\n}\n\n@end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. \u63cf\u8ff0 GCD\uff08Grand Central Dispatch\uff09\u6846\u67b6\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528 GCD \u6765\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GCD \u662f Apple \u63d0\u4f9b\u7684\u7528\u4e8e\u7b80\u5316\u5e76\u53d1\u7f16\u7a0b\u7684\u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957 API \u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u5404\u79cd\u5e76\u53d1\u7f16\u7a0b\u539f\u8bed\uff0c\u4f8b\u5982\u961f\u5217\u3001\u7ec4\u3001\u6805\u680f\u548c\u8c03\u5ea6\u5757\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 GCD \u521b\u5efa\u5e76\u8fd0\u884c\u5e76\u884c\u4efb\u52a1\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objective-C<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);\ndispatch_async(queue, ^{\n  NSLog(@\"Task 1\");\n});\n\ndispatch_async(queue, ^{\n\n4. \u63cf\u8ff0 GCD\uff08Grand Central Dispatch\uff09\u6846\u67b6\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528 GCD \u6765\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u3002\n\nGCD\uff08Grand Central Dispatch\uff09\u662f Apple \u63d0\u4f9b\u7684\u7528\u4e8e\u7b80\u5316\u5e76\u53d1\u7f16\u7a0b\u7684\u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957 API \u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u5404\u79cd\u5e76\u53d1\u7f16\u7a0b\u539f\u8bed\uff0c\u4f8b\u5982\u961f\u5217\u3001\u7ec4\u3001\u6805\u680f\u548c\u8c03\u5ea6\u5757\u3002\n\nGCD \u7684\u6838\u5fc3\u6982\u5ff5\u662f \u961f\u5217\uff08queue\uff09\u3002\u961f\u5217\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u4efb\u52a1\u7684 FIFO\uff08\u5148\u8fdb\u5148\u51fa\uff09\u7ed3\u6784\u3002GCD \u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u7684\u961f\u5217\uff0c\u4f8b\u5982\uff1a\n\n\u5e76\u884c\u961f\u5217\uff08concurrent queue\uff09\uff1a \u5141\u8bb8\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\n\u4e32\u884c\u961f\u5217\uff08serial queue\uff09\uff1a \u4e00\u6b21\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\u4efb\u52a1\u3002\n\u4e3b\u961f\u5217\uff08main queue\uff09\uff1a \u7528\u4e8e\u66f4\u65b0 UI \u754c\u9762\u3002\n\u9664\u4e86\u961f\u5217\u4e4b\u5916\uff0cGCD \u8fd8\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u91cd\u8981\u7684\u6982\u5ff5\uff1a\n\n\u7ec4\uff08group\uff09\uff1a \u53ef\u4ee5\u5c06\u591a\u4e2a\u4efb\u52a1\u7ec4\u5408\u6210\u4e00\u4e2a\u7ec4\uff0c\u5e76\u534f\u8c03\u5b83\u4eec\u7684\u6267\u884c\u3002\n\u6805\u680f\uff08barrier\uff09\uff1a \u53ef\u4ee5\u786e\u4fdd\u7ec4\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u90fd\u5b8c\u6210\u6267\u884c\u540e\uff0c\u518d\u6267\u884c\u6805\u680f\u540e\u9762\u7684\u4efb\u52a1\u3002\n\u8c03\u5ea6\u5757\uff08dispatch block\uff09\uff1a \u5c01\u88c5\u8981\u6267\u884c\u7684\u4efb\u52a1\u7684\u4ee3\u7801\u5757\u3002\n\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 GCD \u521b\u5efa\u5e76\u8fd0\u884c\u5e76\u884c\u4efb\u52a1\u7684\u793a\u4f8b\uff1a\n\nObjective-C\ndispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);\ndispatch_async(queue, ^{\n  NSLog(@\"Task 1\");\n});\n\ndispatch_async(queue, ^{\n  NSLog(@\"Task 2\");\n});\n\u8bf7\u8c28\u614e\u4f7f\u7528\u4ee3\u7801\u3002\ncontent_copy\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cdispatch_get_global_queue \u51fd\u6570\u83b7\u53d6\u4e86\u4e00\u4e2a\u5e76\u884c\u961f\u5217\uff0cdispatch_async \u51fd\u6570\u5c06\u4e24\u4e2a\u4efb\u52a1\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u3002\u8fd9\u4e24\u4e2a\u4efb\u52a1\u5c06\u88ab\u5e76\u884c\u6267\u884c\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u3002\n\n5. \u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u3002\n\n\u7ebf\u7a0b\u5b89\u5168\u662f\u6307\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u88ab\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u800c\u4e0d\u4f1a\u5bfc\u81f4\u6570\u636e\u635f\u574f\u6216\u7ade\u4e89\u6761\u4ef6\u3002\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u6280\u672f\uff1a\n\n\u540c\u6b65\u539f\u8bed\uff1a \u4f7f\u7528\u9501\u3001\u4fe1\u53f7\u91cf\u6216\u4e92\u65a5\u9501\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u6570\u636e\u7ade\u4e89\u3002\n\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff1a \u5c3d\u53ef\u80fd\u4f7f\u7528\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u88ab\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u3002\n\u539f\u5b50\u64cd\u4f5c\uff1a \u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\uff0c\u4f8b\u5982 atomicIncrement \u6216 atomicSetValue\uff0c\u6765\u786e\u4fdd\u5bf9\u5171\u4eab\u53d8\u91cf\u7684\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u7684\u3002\n\u7ebf\u7a0b\u5c40\u90e8\u6570\u636e\uff1a \u4f7f\u7528\u7ebf\u7a0b\u5c40\u90e8\u6570\u636e\u6765\u5b58\u50a8\u6bcf\u4e2a\u7ebf\u7a0b\u7279\u6709\u7684\u6570\u636e\uff0c\u907f\u514d\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\n6. \u89e3\u91ca\u6b7b\u9501\u7684\u6982\u5ff5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u907f\u514d\u6b7b\u9501\u3002\n\n\u6b7b\u9501\u662f\u4e00\u79cd\u60c5\u51b5\uff0c\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\uff0c\u5bfc\u81f4\u5b83\u4eec\u90fd\u6c38\u4e45\u963b\u585e\u3002\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u51c6\u5219\uff1a\n\n\u907f\u514d\u5faa\u73af\u4f9d\u8d56\uff1a \u83b7\u53d6\u8d44\u6e90\u65f6\uff0c\u5e94\u6309\u4e00\u81f4\u7684\u987a\u5e8f\u83b7\u53d6\uff0c\u4ee5\u9632\u6b62\u5faa\u73af\u7b49\u5f85\u3002\n\u5c3d\u91cf\u7f29\u77ed\u9501\u7684\u6301\u6709\u65f6\u95f4\uff1a \u53ea\u8981\u5b8c\u6210\u5bf9\u8d44\u6e90\u7684\u64cd\u4f5c\uff0c\u5c31\u7acb\u5373\u91ca\u653e\u9501\u3002\n\u8c28\u614e\u4f7f\u7528\u5d4c\u5957\u9501\uff1a \u4f7f\u7528\u5d4c\u5957\u9501\u65f6\uff0c\u5e94\u5148\u83b7\u53d6\u5185\u5c42\u9501\uff0c\u540e\u91ca\u653e\u5916\u5c42\u9501\u3002\n7. \u63cf\u8ff0\u5982\u4f55\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u3002\n\n\u5f02\u6b65\u7f16\u7a0b\u662f\u6307\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4efb\u52a1\u3002\u8fd9\u4f7f\u5f97 UI \u754c\u9762\u53ef\u4ee5\u4fdd\u6301\u54cd\u5e94\uff0c\u5373\u4f7f\u5728\u540e\u53f0\u6267\u884c\u8017\u65f6\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5f02\u6b65\u7f16\u7a0b\u65b9\u6cd5\uff1a\n\nGCD \u961f\u5217\uff1a \u4f7f\u7528 GCD \u961f\u5217\u5c06\u4efb\u52a1\u5206\u53d1\u5230\u540e\u53f0\u7ebf\u7a0b\u3002\nNSOperation \u548c NSOperationQueue\uff1a \u4f7f\u7528 NSOperation \u548c NSOperationQueue \u6765\u7ba1\u7406\u548c\u6267\u884c\u5f02\u6b65\u4efb\u52a1\u3002\n\u59d4\u6258\uff1a \u4f7f\u7528\u59d4\u6258\u5c06\u4efb\u52a1\u4f20\u9012\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff0c\u5141\u8bb8\u5b83\u4eec\u5f02\u6b65\u6267\u884c\u3002\n\u5b8c\u6210\u5757\uff1a \u4f7f\u7528\u5b8c\u6210\u5757\u6765\u901a\u77e5\u4e3b\u7ebf\u7a0b\u4f55\u65f6\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u3002\n8. \u89e3\u91ca\u5982\u4f55\u5904\u7406\u5e76\u884c\u4efb\u52a1\u7684\u5b8c\u6210\u3002\n\n\u5728 Objective-C \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u5e76\u884c\u4efb\u52a1\u7684\u5b8c\u6210\uff1a\n\nGCD dispatch_group_notify\uff1a \u4f7f\u7528 dispatch_group_notify \u5728 dispatch \u7ec4\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u4e00\u4e2a\u5757\u3002\nNSOperationQueue \u5b8c\u6210\u5757\uff1a \u4f7f\u7528 NSOperationQueue \u7684\u5b8c\u6210\u5757\u5728\u961f\u5217\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u5b8c\u6210\u540e\u5f97\u5230\u901a\u77e5\u3002\nCombine \u6846\u67b6\uff1a \u4f7f\u7528 Combine \u6846\u67b6\u7684 Publishers \u548c Subscribers \u6765\u5904\u7406\u5f02\u6b65\u6570\u636e\u6d41\u53ca\u5176\u5b8c\u6210\u4e8b\u4ef6\u3002\n9. \u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u5e76\u53d1\u7f16\u7a0b\u6765\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002\n\n\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u63d0\u9ad8 Objective-C \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff1a\n\n\u5c06\u8017\u65f6\u7684\u4efb\u52a1\u5378\u8f7d\u5230\u540e\u53f0\u7ebf\u7a0b\uff1a \u8fd9\u5141\u8bb8\u4e3b\u7ebf\u7a0b\u4fdd\u6301\u54cd\u5e94\u5e76\u5904\u7406\u7528\u6237\u4ea4\u4e92\uff0c\u540c\u65f6\u540e\u53f0\u7ebf\u7a0b\u6b63\u5728\u5904\u7406\u4efb\u52a1\u3002\n\u5229\u7528\u591a\u4e2a CPU \u6838\u5fc3\uff1a \u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u662f CPU \u5bc6\u96c6\u578b\u7684\uff0c\u4f7f\u7528\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5c06\u5de5\u4f5c\u8d1f\u8f7d\u5206\u6563\u5230\u591a\u4e2a CPU \u6838\u5fc3\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002\n\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u63d0\u9ad8 Objective-C \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff1a\n\n\u5c06\u8017\u65f6\u7684\u4efb\u52a1\u5378\u8f7d\u5230\u540e\u53f0\u7ebf\u7a0b\uff1a \u8fd9\u5141\u8bb8\u4e3b\u7ebf\u7a0b\u4fdd\u6301\u54cd\u5e94\u5e76\u5904\u7406\u7528\u6237\u4ea4\u4e92\uff0c\u540c\u65f6\u540e\u53f0\u7ebf\u7a0b\u6b63\u5728\u5904\u7406\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4ece\u7f51\u7edc\u4e0b\u8f7d\u56fe\u50cf\uff0c\u53ef\u4ee5\u4f7f\u7528 GCD \u5c06\u4e0b\u8f7d\u4efb\u52a1\u5206\u53d1\u5230\u540e\u53f0\u7ebf\u7a0b\u3002\u8fd9\u6837\uff0c\u4e3b\u7ebf\u7a0b\u5c31\u4e0d\u5fc5\u7b49\u5f85\u56fe\u50cf\u4e0b\u8f7d\u5b8c\u6210\uff0c\u800c\u53ef\u4ee5\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u4e8b\u4ef6\u3002\n\n\u5229\u7528\u591a\u4e2a CPU \u6838\u5fc3\uff1a \u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u662f CPU \u5bc6\u96c6\u578b\u7684\uff0c\u4f7f\u7528\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5c06\u5de5\u4f5c\u8d1f\u8f7d\u5206\u6563\u5230\u591a\u4e2a CPU \u6838\u5fc3\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u5bf9\u89c6\u9891\u8fdb\u884c\u7f16\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u884c\u5904\u7406\u4e0d\u540c\u7684\u89c6\u9891\u5e27\u3002\n\n\u63d0\u9ad8 I\/O \u54cd\u5e94\u901f\u5ea6\uff1a  \u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u5904\u7406 I\/O \u64cd\u4f5c\uff0c\u786e\u4fdd\u5373\u4f7f\u5728\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u6216\u6587\u4ef6\u8bbf\u95ee\u65f6\uff0cUI \u754c\u9762\u4e5f\u80fd\u4fdd\u6301\u54cd\u5e94\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4ece\u670d\u52a1\u5668\u52a0\u8f7d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 NSOperationQueue \u5c06\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u5206\u53d1\u5230\u540e\u53f0\u7ebf\u7a0b\u3002\u8fd9\u6837\uff0c\u4e3b\u7ebf\u7a0b\u5c31\u4e0d\u5fc5\u7b49\u5f85\u7f51\u7edc\u8bf7\u6c42\u5b8c\u6210\uff0c\u800c\u53ef\u4ee5\u7ee7\u7eed\u66f4\u65b0 UI \u754c\u9762\u3002\n\n10.  \u63d0\u4f9b\u4e00\u4e2a\u4f7f\u7528\u5e76\u53d1\u7f16\u7a0b\u6765\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u793a\u4f8b\u3002\n\n\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 GCD \u6765\u63d0\u9ad8\u56fe\u50cf\u52a0\u8f7d\u6027\u80fd\u7684\u793a\u4f8b\uff1a\n\nObjective-C\n- (void)loadImage:(NSString *)imageURL completion:(void (^)(UIImage *image))completion {\n  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\n    NSData *imageData = &#91;NSData dataWithContentsOfURL:&#91;NSURL URLWithString:imageURL]];\n    UIImage *image = &#91;UIImage imageWithData:imageData];\n    dispatch_async(dispatch_get_main_queue(), ^{\n      if (completion) {\n        completion(image);\n      }\n    });\n  });\n}\n\u8bf7\u8c28\u614e\u4f7f\u7528\u4ee3\u7801\u3002\ncontent_copy\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cloadImage \u65b9\u6cd5\u9996\u5148\u5c06\u56fe\u50cf\u52a0\u8f7d\u4efb\u52a1\u5206\u53d1\u5230\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\u3002\u7136\u540e\uff0c\u5b83\u5728\u540e\u53f0\u7ebf\u7a0b\u4e0a\u52a0\u8f7d\u56fe\u50cf\u6570\u636e\u5e76\u521b\u5efa UIImage \u5bf9\u8c61\u3002\u6700\u540e\uff0c\u5b83\u5c06 UIImage \u5bf9\u8c61\u4f20\u9012\u56de\u4e3b\u7ebf\u7a0b\u4ee5\u66f4\u65b0 UI \u754c\u9762\u3002\n\n\u901a\u8fc7\u5c06\u56fe\u50cf\u52a0\u8f7d\u4efb\u52a1\u5206\u53d1\u5230\u540e\u53f0\u7ebf\u7a0b\uff0c\u6b64\u793a\u4f8b\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4e3b\u7ebf\u7a0b\u5728\u52a0\u8f7d\u56fe\u50cf\u65f6\u4fdd\u6301\u54cd\u5e94\u3002\n\n\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528\u5e76\u53d1\u7f16\u7a0b\u7684\u5176\u4ed6\u793a\u4f8b\uff1a\n\n\u5728\u540e\u53f0\u6267\u884c\u7f51\u7edc\u8bf7\u6c42\n\u5904\u7406\u89c6\u9891\u6216\u97f3\u9891\u6570\u636e\n\u8fdb\u884c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\n\u66f4\u65b0 UI \u754c\u9762\u800c\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\n\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u63d0\u9ad8 Objective-C \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u4f46\u662f\uff0c\u91cd\u8981\u7684\u662f\u8981\u6b63\u786e\u4f7f\u7528\u5e76\u53d1\u7f16\u7a0b\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u6b7b\u9501\u548c\u5176\u4ed6\u95ee\u9898\u3002\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001Gemini \u95ee\u7b54\u751f\u6210 Objective-C \u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7528\u4e8e\u5f00\u53d1 macOS\u3001iOS&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6283\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[66,114,112],"class_list":["post-6283","post","type-post","status-publish","format-standard","hentry","category-5","tag-ios","tag-114","tag-112"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6283"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6283\/revisions"}],"predecessor-version":[{"id":6284,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6283\/revisions\/6284"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}