{"id":6276,"date":"2024-07-10T14:13:29","date_gmt":"2024-07-10T06:13:29","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6276"},"modified":"2024-07-10T14:13:29","modified_gmt":"2024-07-10T06:13:29","slug":"combine-%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6276","title":{"rendered":"Combine \u6846\u67b6"},"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\">Combine \u6846\u67b6\u662f Apple \u4e8e 2019 \u5e74\u63a8\u51fa\u7684\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4e8b\u4ef6\u548c\u6570\u636e\u6d41\u7684\u58f0\u660e\u5f0f\u7f16\u7a0b\u6846\u67b6\u3002\u5b83\u9002\u7528\u4e8e\u6240\u6709 Apple \u5e73\u53f0\uff0c\u5305\u62ec iOS\u3001macOS\u3001iPadOS\u3001watchOS \u548c tvOS\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combine \u6846\u67b6\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u53d1\u5e03\u8005 (Publisher) \u548c\u8ba2\u9605\u8005 (Subscriber)\u3002\u53d1\u5e03\u8005\u8d1f\u8d23\u53d1\u5e03\u503c\u6216\u4e8b\u4ef6\uff0c\u800c\u8ba2\u9605\u8005\u5219\u8d1f\u8d23\u63a5\u6536\u548c\u5904\u7406\u8fd9\u4e9b\u503c\u6216\u4e8b\u4ef6\u3002\u53d1\u5e03\u8005\u548c\u8ba2\u9605\u8005\u4e4b\u95f4\u4f7f\u7528\u8ba2\u9605 (Subscription) \u8fdb\u884c\u8fde\u63a5\uff0c\u8ba2\u9605\u8005\u53ef\u4ee5\u53d6\u6d88\u8ba2\u9605\u53d1\u5e03\u8005\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combine \u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u6765\u5904\u7406\u6570\u636e\u6d41\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u8f6c\u6362\u3001\u7ec4\u5408\u548c\u9519\u8bef\u5904\u7406\u3002\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\u53ef\u4ee5\u4ee5\u58f0\u660e\u5f0f\u7684\u65b9\u5f0f\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u6784\u5efa\u590d\u6742\u7684\u4e8b\u4ef6\u5904\u7406\u7ba1\u9053\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combine \u6846\u67b6\u7684\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u58f0\u660e\u5f0f\u7f16\u7a0b\uff1a<\/strong> Combine \u6846\u67b6\u4f7f\u7528\u58f0\u660e\u5f0f\u7f16\u7a0b\u8303\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u4e8b\u4ef6\u548c\u6570\u636e\u6d41\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3001\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n\n\n\n<li><strong>\u7c7b\u578b\u5b89\u5168\uff1a<\/strong> Combine \u6846\u67b6\u652f\u6301 Swift \u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u786e\u4fdd\u4ee3\u7801\u7c7b\u578b\u5b89\u5168\uff0c\u5e76\u9632\u6b62\u5e38\u89c1\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u7ec4\u5408\u6027\uff1a<\/strong> Combine \u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u7b26\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u6784\u5efa\u590d\u6742\u7684\u4e8b\u4ef6\u5904\u7406\u7ba1\u9053\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u53cb\u597d\u6027\uff1a<\/strong> Combine \u6846\u67b6\u7684\u8bbe\u8ba1\u6613\u4e8e\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5\u548c\u7aef\u5230\u7aef\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Combine \u6846\u67b6\u7684\u5e38\u89c1\u7528\u9014\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5904\u7406\u7528\u6237\u754c\u9762\u4e8b\u4ef6\uff1a<\/strong> \u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06 Combine \u6846\u67b6\u7528\u4e8e\u5904\u7406\u6309\u94ae\u70b9\u51fb\u3001\u6587\u672c\u8f93\u5165\u548c\u624b\u52bf\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u7f51\u7edc\u8bf7\u6c42\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528 Combine \u6846\u67b6\u6765\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\uff0c\u5e76\u4ee5\u58f0\u660e\u5f0f\u7684\u65b9\u5f0f\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u6d41\u5904\u7406\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528 Combine \u6846\u67b6\u6765\u5904\u7406\u5404\u79cd\u6570\u636e\u6d41\uff0c\u4f8b\u5982\u4f20\u611f\u5668\u6570\u636e\u3001\u4f4d\u7f6e\u66f4\u65b0\u548c\u5b9e\u65f6\u6570\u636e\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e9b Combine \u6846\u67b6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa\u4e00\u4e2a\u53d1\u5e03\u8005\uff0c\u6bcf\u9694 1 \u79d2\u53d1\u5e03\u4e00\u4e2a\u6570\u5b57\nlet numbersPublisher = Timer.publish(every: 1, on: .main, in: .common)\n\n\/\/ \u8fc7\u6ee4\u5076\u6570\nlet evenNumbersPublisher = numbersPublisher\n    .filter { $0 % 2 == 0 }\n\n\/\/ \u8f6c\u6362\u6570\u5b57\u4e3a\u5b57\u7b26\u4e32\nlet stringNumbersPublisher = evenNumbersPublisher\n    .map { String($0) }\n\n\/\/ \u8ba2\u9605\u5e76\u6253\u5370\u5b57\u7b26\u4e32\nstringNumbersPublisher\n    .sink(receiveCompletion: { _ in }, receiveValue: { print($0) })\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u6bcf\u9694 1 \u79d2\u53d1\u5e03\u4e00\u4e2a\u6570\u5b57\u7684\u53d1\u5e03\u8005\u3002\u7136\u540e\uff0c\u5b83\u4f7f\u7528 <code class=\"\">filter<\/code> \u64cd\u4f5c\u7b26\u8fc7\u6ee4\u5076\u6570\uff0c\u4f7f\u7528 <code class=\"\">map<\/code> \u64cd\u4f5c\u7b26\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u4f7f\u7528 <code class=\"\">sink<\/code> \u64cd\u4f5c\u7b26\u8ba2\u9605\u5e76\u6253\u5370\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combine \u6846\u67b6\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u7b80\u5316\u5f02\u6b65\u4e8b\u4ef6\u548c\u6570\u636e\u6d41\u7684\u5904\u7406\u3002\u5b83\u9002\u7528\u4e8e\u5404\u79cd iOS \u5f00\u53d1\u573a\u666f\uff0c\u5e76\u5df2\u6210\u4e3a\u8bb8\u591a Apple \u5b98\u65b9\u5e94\u7528\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001Gemini \u95ee\u7b54\u751f\u6210 Combine \u6846\u67b6\u662f Apple \u4e8e 2019 \u5e74\u63a8\u51fa\u7684\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4e8b\u4ef6\u548c\u6570&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6276\">\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,67,112],"class_list":["post-6276","post","type-post","status-publish","format-standard","hentry","category-5","tag-ios","tag-swift","tag-112"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6276","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=6276"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6276\/revisions"}],"predecessor-version":[{"id":6277,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6276\/revisions\/6277"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}