{"id":6426,"date":"2024-08-04T08:18:14","date_gmt":"2024-08-04T00:18:14","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6426"},"modified":"2024-08-04T08:27:31","modified_gmt":"2024-08-04T00:27:31","slug":"ios-%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e8%a6%81%e7%82%b9-swift-%e4%b8%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6426","title":{"rendered":"iOS \u5e76\u53d1\u7f16\u7a0b\u8981\u70b9 &#8211; Swift \u4e3a\u4f8b"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>\u5728 Swift \u4e2d\uff0c\u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u6d89\u53ca\u591a\u4e2a\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u77e5\u8bc6\u70b9\uff1a<\/p>\n<h3>1. <strong>Grand Central Dispatch (GCD)<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>DispatchQueue<\/strong>\uff1aGCD \u7684\u4e3b\u8981\u7ec4\u4ef6\u4e4b\u4e00\u3002\u53ef\u4ee5\u521b\u5efa\u4e32\u884c\u6216\u5e76\u884c\u961f\u5217\u6765\u8c03\u5ea6\u4efb\u52a1\u3002<\/p>\n<ul>\n<li><strong>\u4e32\u884c\u961f\u5217<\/strong>\uff1a\u4efb\u52a1\u6309\u987a\u5e8f\u4e00\u4e2a\u63a5\u4e00\u4e2a\u6267\u884c\u3002<\/li>\n<li><strong>\u5e76\u884c\u961f\u5217<\/strong>\uff1a\u4efb\u52a1\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">\/\/ \u4e32\u884c\u961f\u5217\nlet serialQueue = DispatchQueue(label: \"com.example.serialQueue\")\n\n\/\/ \u5e76\u884c\u961f\u5217\nlet concurrentQueue = DispatchQueue(label: \"com.example.concurrentQueue\", attributes: .concurrent)<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5f02\u6b65\u4e0e\u540c\u6b65<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5f02\u6b65\uff08<code>async<\/code>\uff09<\/strong>\uff1a\u4efb\u52a1\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u963b\u585e\u3002<\/li>\n<li><strong>\u540c\u6b65\uff08<code>sync<\/code>\uff09<\/strong>\uff1a\u4efb\u52a1\u5728\u5f53\u524d\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">serialQueue.async {\n  \/\/ \u5f02\u6b65\u6267\u884c\u7684\u4ee3\u7801\n}\n\nserialQueue.sync {\n  \/\/ \u540c\u6b65\u6267\u884c\u7684\u4ee3\u7801\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>2. <strong>Operation \u548c OperationQueue<\/strong><\/h3>\n<ul>\n<li><strong>Operation<\/strong>\uff1a\u4e00\u79cd\u7528\u4e8e\u6267\u884c\u4efb\u52a1\u7684\u9ad8\u7ea7\u62bd\u8c61\uff0c\u652f\u6301\u4f18\u5148\u7ea7\u3001\u53d6\u6d88\u3001\u4f9d\u8d56\u5173\u7cfb\u7b49\u529f\u80fd\u3002<\/li>\n<li>\n<p><strong>OperationQueue<\/strong>\uff1a\u7528\u4e8e\u7ba1\u7406\u548c\u8c03\u5ea6 <code>Operation<\/code> \u5bf9\u8c61\u7684\u961f\u5217\u3002<\/p>\n<pre><code class=\"language-swift\">let operationQueue = OperationQueue()\n\nlet operation = BlockOperation {\n  \/\/ \u6267\u884c\u4efb\u52a1\n}\n\noperationQueue.addOperation(operation)<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>3. <strong>DispatchSemaphore<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>DispatchSemaphore<\/strong>\uff1a\u7528\u4e8e\u63a7\u5236\u5bf9\u8d44\u6e90\u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8ba1\u6570\u5668\u6765\u9650\u5236\u540c\u65f6\u8bbf\u95ee\u7684\u7ebf\u7a0b\u6570\u3002<\/p>\n<pre><code class=\"language-swift\">let semaphore = DispatchSemaphore(value: 1)\n\nsemaphore.wait()  \/\/ \u8bf7\u6c42\u4fe1\u53f7\u91cf\n\/\/ \u4e34\u754c\u533a\u4ee3\u7801\nsemaphore.signal()  \/\/ \u91ca\u653e\u4fe1\u53f7\u91cf<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>4. <strong>Thread Safety<\/strong><\/h3>\n<ul>\n<li><strong>\u9501\uff08Lock\uff09<\/strong>\uff1a\u4f8b\u5982 <code>NSLock<\/code>\uff0c\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u907f\u514d\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<li><strong>\u8bfb\u5199\u9501\uff08Read-Write Lock\uff09<\/strong>\uff1a\u4f8b\u5982 <code>NSRecursiveLock<\/code> \u548c <code>os_unfair_lock<\/code>\uff0c\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\uff0c\u4f46\u5199\u5165\u65f6\u4f1a\u52a0\u9501\u3002<\/li>\n<\/ul>\n<h3>5. <strong>Swift Concurrency\uff08Swift 5.5 \u53ca\u66f4\u9ad8\u7248\u672c\uff09<\/strong><\/h3>\n<ul>\n<li>\n<p><strong><code>async<\/code> \u548c <code>await<\/code><\/strong>\uff1a\u7528\u4e8e\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\uff0c\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<pre><code class=\"language-swift\">func fetchData() async -> Data {\n  \/\/ \u5f02\u6b65\u83b7\u53d6\u6570\u636e\n}\n\nfunc processData() async {\n  let data = await fetchData()\n  \/\/ \u5904\u7406\u6570\u636e\n}<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u4efb\u52a1\uff08Task\uff09<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u5f02\u6b65\u4efb\u52a1\uff0c\u53ef\u4ee5\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u8fd0\u884c\u3002<\/p>\n<pre><code class=\"language-swift\">Task {\n  await processData()\n}<\/code><\/pre>\n<\/li>\n<li>\n<p><strong><code>actor<\/code><\/strong>\uff1a\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u72b6\u6001\uff0c\u786e\u4fdd\u6570\u636e\u5b89\u5168\uff0c\u9632\u6b62\u6570\u636e\u7ade\u4e89\u3002<\/p>\n<pre><code class=\"language-swift\">actor DataManager {\n  private var data: [String] = []\n\n  func addData(_ newData: String) {\n      data.append(newData)\n  }\n\n  func getData() -> [String] {\n      return data\n  }\n}<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5e76\u53d1\u4efb\u52a1\u7ec4\uff08TaskGroup\uff09<\/strong>\uff1a\u7528\u4e8e\u5e76\u884c\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u5e76\u7b49\u5f85\u5b83\u4eec\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"language-swift\">await withTaskGroup(of: Void.self) { group in\n  group.addTask {\n      \/\/ \u6267\u884c\u4efb\u52a1 1\n  }\n  group.addTask {\n      \/\/ \u6267\u884c\u4efb\u52a1 2\n  }\n  \/\/ \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>6. <strong>\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n<ul>\n<li>\n<p>\u5728\u5f02\u6b65\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528 <code>do-catch<\/code> \u8bed\u53e5\u5904\u7406\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-swift\">do {\n  try await someAsyncFunction()\n} catch {\n  print(\"Error occurred: \\(error)\")\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u638c\u63e1\u8fd9\u4e9b\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\uff0c\u5c06\u5e2e\u52a9\u4f60\u5728 Swift \u4e2d\u66f4\u6709\u6548\u5730\u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 \u5728 Swift \u4e2d\uff0c\u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u6d89\u53ca\u591a\u4e2a\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u77e5\u8bc6\u70b9&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6426\">\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":[135],"tags":[67,114],"class_list":["post-6426","post","type-post","status-publish","format-standard","hentry","category-135","tag-swift","tag-114"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6426","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=6426"}],"version-history":[{"count":2,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6426\/revisions"}],"predecessor-version":[{"id":6428,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6426\/revisions\/6428"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}