{"id":511,"date":"2018-02-23T23:38:54","date_gmt":"2018-02-23T15:38:54","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e5%8f%af%e9%80%89optionals%e7%b1%bb%e5%9e%8b\/"},"modified":"2018-02-23T23:38:54","modified_gmt":"2018-02-23T15:38:54","slug":"swift-%e5%8f%af%e9%80%89optionals%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=511","title":{"rendered":"Swift \u53ef\u9009(Optionals)\u7c7b\u578b"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u53ef\u9009(Optionals)\u7c7b\u578b<\/h1>\n<p>\nSwift \u7684\u53ef\u9009\uff08Optional\uff09\u7c7b\u578b\uff0c\u7528\u4e8e\u5904\u7406\u503c\u7f3a\u5931\u7684\u60c5\u51b5\u3002\u53ef\u9009\u8868\u793a&#8221;\u90a3\u513f\u6709\u4e00\u4e2a\u503c\uff0c\u5e76\u4e14\u5b83\u7b49\u4e8e x &#8220;\u6216\u8005&#8221;\u90a3\u513f\u6ca1\u6709\u503c&#8221;\u3002<\/p>\n<p>Swfit\u8bed\u8a00\u5b9a\u4e49\u540e\u7f00\uff1f\u4f5c\u4e3a\u547d\u540d\u7c7b\u578bOptional\u7684\u7b80\u5199\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u4ee5\u4e0b\u4e24\u79cd\u58f0\u660e\u662f\u76f8\u7b49\u7684\uff1a<\/p>\n<pre>\nvar optionalInteger: Int?\nvar optionalInteger: Optional&lt;Int&gt;\n<\/pre>\n<p>\n\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf optionalInteger \u90fd\u662f\u53ef\u9009\u6574\u6570\u7c7b\u578b\u3002\u6ce8\u610f\uff0c\u5728\u7c7b\u578b\u548c <strong>?<\/strong>\u4e4b\u95f4\u6ca1\u6709\u7a7a\u683c\u3002\n<\/p>\n<p>\nOptional \u662f\u4e00\u4e2a\u542b\u6709\u4e24\u79cd\u60c5\u51b5\u7684\u679a\u4e3e\uff0cNone \u548c Some(T)\uff0c\u7528\u6765\u8868\u793a\u53ef\u80fd\u6709\u6216\u53ef\u80fd\u6ca1\u6709\u503c\u3002\u4efb\u4f55\u7c7b\u578b\u90fd\u53ef\u4ee5\u660e\u786e\u58f0\u660e\u4e3a\uff08\u6216\u8005\u9690\u5f0f\u8f6c\u6362\uff09\u53ef\u9009\u7c7b\u578b\u3002\u5f53\u58f0\u660e\u4e00\u4e2a\u53ef\u9009\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u8981\u786e\u4fdd\u7528\u62ec\u53f7\u7ed9 ? \u64cd\u4f5c\u7b26\u4e00\u4e2a\u5408\u9002\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u58f0\u660e\u53ef\u9009\u6574\u6570\u6570\u7ec4\uff0c\u5e94\u8be5\u5199\u6210 (Int[])? \u5199\u6210 Int[]? \u4f1a\u62a5\u9519\u3002\n<\/p>\n<p>\n\u5f53\u4f60\u58f0\u660e\u4e00\u4e2a\u53ef\u9009\u53d8\u91cf\u6216\u8005\u53ef\u9009\u5c5e\u6027\u7684\u65f6\u5019\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u503c\uff0c\u5b83\u7684\u503c\u4f1a\u9ed8\u8ba4\u4e3a nil\u3002<\/p>\n<p>\n\u53ef\u9009\u9879\u9075\u7167 LogicValue \u534f\u8bae\uff0c\u56e0\u6b64\u53ef\u4ee5\u51fa\u73b0\u5728\u5e03\u5c14\u73af\u5883\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u53ef\u9009\u7c7b\u578bT?\u5305\u542b\u7c7b\u578b\u4e3aT\u7684\u4efb\u4f55\u503c\uff08\u4e5f\u5c31\u662f\u8bf4\u5b83\u7684\u503c\u662f Optional.Some(T) \uff09\uff0c\u8fd9\u4e2a\u53ef\u9009\u7c7b\u578b\u7b49\u4e8e true\uff0c\u53cd\u4e4b\u4e3a false\u3002\n<\/p>\n<p>\n\u5982\u679c\u4e00\u4e2a\u53ef\u9009\u7c7b\u578b\u7684\u5b9e\u4f8b\u5305\u542b\u4e00\u4e2a\u503c\uff0c\u4f60\u53ef\u4ee5\u7528\u540e\u7f00\u64cd\u4f5c\u7b26 \uff01\u6765\u8bbf\u95ee\u8fd9\u4e2a\u503c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\noptionalInteger = 42\noptionalInteger! \/\/ 42\n<\/pre>\n<p>\n\u4f7f\u7528\u64cd\u4f5c\u7b26\uff01\u53bb\u83b7\u53d6\u503c\u4e3anil\u7684\u53ef\u9009\u53d8\u91cf\u4f1a\u6709\u8fd0\u884c\u65f6\u9519\u8bef\u3002\n<\/p>\n<p>\n\u4f60\u53ef\u4ee5\u7528\u53ef\u9009\u94fe\u63a5\u548c\u53ef\u9009\u7ed1\u5b9a\u9009\u62e9\u6027\u6267\u884c\u53ef\u9009\u8868\u8fbe\u5f0f\u4e0a\u7684\u64cd\u4f5c\u3002\u5982\u679c\u503c\u4e3anil\uff0c\u4efb\u4f55\u64cd\u4f5c\u90fd\u4e0d\u4f1a\u6267\u884c\uff0c\u4e5f\u4e0d\u4f1a\u6709\u8fd0\u884c\u62a5\u9519\u3002\n<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u8be6\u7ec6\u770b\u4e0b\u4ee5\u4e0b\u5b9e\u4f8b\u6765\u4e86\u89e3 Swift \u4e2d\u53ef\u9009\u7c7b\u578b\u7684\u5e94\u7528:<\/p>\n<pre>\nimport Cocoa\n\nvar myString:String? = nil\n\nif myString != nil {\n    print(myString)\n}else{\n    print(\"\u5b57\u7b26\u4e32\u4e3a nil\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b57\u7b26\u4e32\u4e3a nil\n<\/pre>\n<p>\u53ef\u9009\u7c7b\u578b\u7c7b\u4f3c\u4e8eObjective-C\u4e2d\u6307\u9488\u7684nil\u503c\uff0c\u4f46\u662fnil\u53ea\u5bf9\u7c7b(class)\u6709\u7528\uff0c\u800c\u53ef\u9009\u7c7b\u578b\u5bf9\u6240\u6709\u7684\u7c7b\u578b\u90fd\u53ef\u7528\uff0c\u5e76\u4e14\u66f4\u5b89\u5168\u3002<\/p>\n<hr>\n<h2>\u5f3a\u5236\u89e3\u6790<\/h2>\n<p>\u5f53\u4f60\u786e\u5b9a\u53ef\u9009\u7c7b\u578b\u786e\u5b9e\u5305\u542b\u503c\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u53ef\u9009\u7684\u540d\u5b57\u540e\u9762\u52a0\u4e00\u4e2a\u611f\u53f9\u53f7\uff08!\uff09\u6765\u83b7\u53d6\u503c\u3002\u8fd9\u4e2a\u611f\u53f9\u53f7\u8868\u793a&#8221;\u6211\u77e5\u9053\u8fd9\u4e2a\u53ef\u9009\u6709\u503c\uff0c\u8bf7\u4f7f\u7528\u5b83\u3002&#8221;\u8fd9\u88ab\u79f0\u4e3a\u53ef\u9009\u503c\u7684\u5f3a\u5236\u89e3\u6790\uff08forced unwrapping\uff09\u3002<\/p>\n<p>\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar myString:String?\n\nmyString = \"Hello, Swift!\"\n\nif myString != nil {\n   print(myString)\n}else{\n   print(\"myString \u503c\u4e3a nil\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nOptional(\"Hello, Swift!\")\n<\/pre>\n<p>\u5f3a\u5236\u89e3\u6790\u53ef\u9009\u503c\uff0c\u4f7f\u7528\u611f\u53f9\u53f7\uff08!\uff09\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar myString:String?\n\nmyString = \"Hello, Swift!\"\n\nif myString != nil {\n   \/\/ \u5f3a\u5236\u89e3\u6790\n   print( myString! )\n}else{\n   print(\"myString \u503c\u4e3a nil\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nHello, Swift!\n<\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<br \/>\u4f7f\u7528<code>!<\/code>\u6765\u83b7\u53d6\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u53ef\u9009\u503c\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u4f7f\u7528<code>!<\/code>\u6765\u5f3a\u5236\u89e3\u6790\u503c\u4e4b\u524d\uff0c\u4e00\u5b9a\u8981\u786e\u5b9a\u53ef\u9009\u5305\u542b\u4e00\u4e2a\u975e<code>nil<\/code>\u7684\u503c\u3002<\/p>\n<\/blockquote>\n<hr>\n<h2>\u81ea\u52a8\u89e3\u6790<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5728\u58f0\u660e\u53ef\u9009\u53d8\u91cf\u65f6\u4f7f\u7528\u611f\u53f9\u53f7\uff08!\uff09\u66ff\u6362\u95ee\u53f7\uff08?\uff09\u3002\u8fd9\u6837\u53ef\u9009\u53d8\u91cf\u5728\u4f7f\u7528\u65f6\u5c31\u4e0d\u9700\u8981\u518d\u52a0\u4e00\u4e2a\u611f\u53f9\u53f7\uff08!\uff09\u6765\u83b7\u53d6\u503c\uff0c\u5b83\u4f1a\u81ea\u52a8\u89e3\u6790\u3002<\/p>\n<p>\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar myString:String!\n\nmyString = \"Hello, Swift!\"\n\nif myString != nil {\n   print(myString)\n}else{\n   print(\"myString \u503c\u4e3a nil\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nHello, Swift!\n<\/pre>\n<hr>\n<h2>\u53ef\u9009\u7ed1\u5b9a<\/h2>\n<p>\n\u4f7f\u7528\u53ef\u9009\u7ed1\u5b9a\uff08optional binding\uff09\u6765\u5224\u65ad\u53ef\u9009\u7c7b\u578b\u662f\u5426\u5305\u542b\u503c\uff0c\u5982\u679c\u5305\u542b\u5c31\u628a\u503c\u8d4b\u7ed9\u4e00\u4e2a\u4e34\u65f6\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u3002\u53ef\u9009\u7ed1\u5b9a\u53ef\u4ee5\u7528\u5728if\u548cwhile\u8bed\u53e5\u4e2d\u6765\u5bf9\u53ef\u9009\u7c7b\u578b\u7684\u503c\u8fdb\u884c\u5224\u65ad\u5e76\u628a\u503c\u8d4b\u7ed9\u4e00\u4e2a\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u3002\n<\/p>\n<p>\u50cf\u4e0b\u9762\u8fd9\u6837\u5728if\u8bed\u53e5\u4e2d\u5199\u4e00\u4e2a\u53ef\u9009\u7ed1\u5b9a\uff1a<\/p>\n<pre>\nif let constantName = someOptional {\n    statements\n}\n<\/pre>\n<p>\n\u8ba9\u6211\u4eec\u6765\u770b\u4e0b\u4e00\u4e2a\u7b80\u5355\u7684\u53ef\u9009\u7ed1\u5b9a\u5b9e\u4f8b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar myString:String?\n\nmyString = \"Hello, Swift!\"\n\nif let yourString = myString {\n   print(\"\u4f60\u7684\u5b57\u7b26\u4e32\u503c\u4e3a - \\(yourString)\")\n}else{\n   print(\"\u4f60\u7684\u5b57\u7b26\u4e32\u6ca1\u6709\u503c\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u4f60\u7684\u5b57\u7b26\u4e32\u503c\u4e3a - Hello, Swift!\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u53ef\u9009(Optionals)\u7c7b\u578b Swift \u7684\u53ef\u9009\uff08Optional\uff09\u7c7b\u578b\uff0c\u7528&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=511\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28],"tags":[74,119],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=511"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}