{"id":175223,"date":"2020-04-09T11:34:27","date_gmt":"2020-04-09T03:34:27","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=175223"},"modified":"2020-03-24T12:36:08","modified_gmt":"2020-03-24T04:36:08","slug":"swift-enumeration-tutorial","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/swift-enumeration-tutorial.html","title":{"rendered":"Swift \u679a\u4e3e\u8bb2\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSwift \u662f\u4e00\u79cd\u652f\u6301\u591a\u7f16\u7a0b\u8303\u5f0f\u548c\u7f16\u8bd1\u5f0f\u7684\u5f00\u6e90\u7f16\u7a0b\u8bed\u8a00,\u82f9\u679c\u4e8e2014\u5e74WWDC\uff08\u82f9\u679c\u5f00\u53d1\u8005\u5927\u4f1a\uff09\u53d1\u5e03\uff0c\u7528\u4e8e\u5f00\u53d1 iOS\uff0cOS X \u548c watchOS \u5e94\u7528\u7a0b\u5e8f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u679a\u4e3e\u7b80\u5355\u7684\u8bf4\u4e5f\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u4e0d\u8fc7\u662f\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u53ea\u5305\u542b\u81ea\u5b9a\u4e49\u7684\u7279\u5b9a\u6570\u636e\uff0c\u5b83\u662f\u4e00\u7ec4\u6709\u5171\u540c\u7279\u6027\u7684\u6570\u636e\u7684\u96c6\u5408\u3002<\/p>\n

Swift \u7684\u679a\u4e3e\u7c7b\u4f3c\u4e8e Objective C \u548c C \u7684\u7ed3\u6784\uff0c\u679a\u4e3e\u7684\u529f\u80fd\u4e3a:<\/p>\n

  • \u5b83\u58f0\u660e\u5728\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316\u7c7b\u6765\u8bbf\u95ee\u5b83\u7684\u503c\u3002<\/li>\n
  • \u679a\u4e3e\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff08initializers\uff09\u6765\u63d0\u4f9b\u4e00\u4e2a\u521d\u59cb\u6210\u5458\u503c\uff1b\u53ef\u4ee5\u5728\u539f\u59cb\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\u6269\u5c55\u5b83\u4eec\u7684\u529f\u80fd\u3002<\/li>\n
  • \u53ef\u4ee5\u9075\u5b88\u534f\u8bae\uff08protocols\uff09\u6765\u63d0\u4f9b\u6807\u51c6\u7684\u529f\u80fd\u3002<\/li>\n
    \u8bed\u6cd5<\/strong><\/div>\n

    Swift \u4e2d\u4f7f\u7528 enum \u5173\u952e\u8bcd\u6765\u521b\u5efa\u679a\u4e3e\u5e76\u4e14\u628a\u5b83\u4eec\u7684\u6574\u4e2a\u5b9a\u4e49\u653e\u5728\u4e00\u5bf9\u5927\u62ec\u53f7\u5185\uff1a<\/p>\n

    \r\nenum enumname {\r\n   \/\/ \u679a\u4e3e\u5b9a\u4e49\u653e\u5728\u8fd9\u91cc\r\n}<\/pre>\n

    \u4f8b\u5982\u6211\u4eec\u5b9a\u4e49\u4ee5\u4e0b\u8868\u793a\u661f\u671f\u7684\u679a\u4e3e\uff1a<\/p>\n

    \r\nimport Cocoa\r\n\r\n\/\/ \u5b9a\u4e49\u679a\u4e3e\r\nenum DaysofaWeek {\r\n    case Sunday\r\n    case Monday\r\n    case TUESDAY\r\n    case WEDNESDAY\r\n    case THURSDAY\r\n    case FRIDAY\r\n    case Saturday\r\n}\r\n\r\nvar weekDay = DaysofaWeek.THURSDAY\r\nweekDay = .THURSDAY\r\nswitch weekDay\r\n{\r\ncase .Sunday:\r\n    print(\"\u661f\u671f\u5929\")\r\ncase .Monday:\r\n    print(\"\u661f\u671f\u4e00\")\r\ncase .TUESDAY:\r\n    print(\"\u661f\u671f\u4e8c\")\r\ncase .WEDNESDAY:\r\n    print(\"\u661f\u671f\u4e09\")\r\ncase .THURSDAY:\r\n    print(\"\u661f\u671f\u56db\")\r\ncase .FRIDAY:\r\n    print(\"\u661f\u671f\u4e94\")\r\ncase .Saturday:\r\n    print(\"\u661f\u671f\u516d\")\r\n}<\/pre>\n

    \u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

    \r\n\u661f\u671f\u56db<\/pre>\n

    \u679a\u4e3e\u4e2d\u5b9a\u4e49\u7684\u503c\uff08\u5982 Sunday\uff0cMonday\uff0c\u2026\u2026\u548cSaturday\uff09\u662f\u8fd9\u4e2a\u679a\u4e3e\u7684\u6210\u5458\u503c\uff08\u6216\u6210\u5458\uff09\u3002case\u5173\u952e\u8bcd\u8868\u793a\u4e00\u884c\u65b0\u7684\u6210\u5458\u503c\u5c06\u88ab\u5b9a\u4e49\u3002<\/p>\n

    \u6ce8\u610f\uff1a \u548c C \u548c Objective-C \u4e0d\u540c\uff0cSwift \u7684\u679a\u4e3e\u6210\u5458\u5728\u88ab\u521b\u5efa\u65f6\u4e0d\u4f1a\u88ab\u8d4b\u4e88\u4e00\u4e2a\u9ed8\u8ba4\u7684\u6574\u578b\u503c\u3002\u5728\u4e0a\u9762\u7684DaysofaWeek\u4f8b\u5b50\u4e2d\uff0cSunday\uff0cMonday\uff0c\u2026\u2026\u548cSaturday\u4e0d\u4f1a\u9690\u5f0f\u5730\u8d4b\u503c\u4e3a0\uff0c1\uff0c\u2026\u2026\u548c6\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u679a\u4e3e\u6210\u5458\u672c\u8eab\u5c31\u6709\u5b8c\u5907\u7684\u503c\uff0c\u8fd9\u4e9b\u503c\u662f\u5df2\u7ecf\u660e\u786e\u5b9a\u4e49\u597d\u7684DaysofaWeek\u7c7b\u578b\u3002<\/p>\n

    \r\nvar weekDay = DaysofaWeek.THURSDAY <\/pre>\n

    weekDay\u7684\u7c7b\u578b\u53ef\u4ee5\u5728\u5b83\u88abDaysofaWeek\u7684\u4e00\u4e2a\u53ef\u80fd\u503c\u521d\u59cb\u5316\u65f6\u63a8\u65ad\u51fa\u6765\u3002\u4e00\u65e6weekDay\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2aDaysofaWeek\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7f29\u5199\u8bed\u6cd5\uff08.\uff09\u5c06\u5176\u8bbe\u7f6e\u4e3a\u53e6\u4e00\u4e2aDaysofaWeek\u7684\u503c\uff1a<\/p>\n

    \r\nvar weekDay = .THURSDAY <\/pre>\n

    \u5f53weekDay\u7684\u7c7b\u578b\u5df2\u77e5\u65f6\uff0c\u518d\u6b21\u4e3a\u5176\u8d4b\u503c\u53ef\u4ee5\u7701\u7565\u679a\u4e3e\u540d\u3002\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\u7684\u679a\u4e3e\u503c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u5177\u6709\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n

    \u679a\u4e3e\u53ef\u5206\u4e3a\u76f8\u5173\u503c\u4e0e\u539f\u59cb\u503c\u3002<\/p>\n

    \u76f8\u5173\u503c\u4e0e\u539f\u59cb\u503c\u7684\u533a\u522b<\/strong><\/div>\n

    \"\"<\/p>\n

    \u76f8\u5173\u503c<\/strong><\/div>\n

    \u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a Student \u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u662f Name \u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08String\uff09\uff0c\u6216\u8005\u662f Mark \u7684\u4e00\u4e2a\u76f8\u5173\u503c\uff08Int\uff0cInt\uff0cInt\uff09\u3002<\/p>\n

    \r\nimport Cocoa\r\n\r\nenum Student{\r\n    case Name(String)\r\n    case Mark(Int,Int,Int)\r\n}\r\nvar studDetails = Student.Name(\"Runoob\")\r\nvar studMarks = Student.Mark(98,97,95)\r\nswitch studMarks {\r\ncase .Name(let studName):\r\n    print(\"\u5b66\u751f\u7684\u540d\u5b57\u662f: \\(studName)\u3002\")\r\ncase .Mark(let Mark1, let Mark2, let Mark3):\r\n    print(\"\u5b66\u751f\u7684\u6210\u7ee9\u662f: \\(Mark1),\\(Mark2),\\(Mark3)\u3002\")\r\n}<\/pre>\n

    \u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

    \r\n\u5b66\u751f\u7684\u6210\u7ee9\u662f: 98,97,95\u3002<\/pre>\n
    \u539f\u59cb\u503c<\/strong><\/div>\n

    \u539f\u59cb\u503c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\uff0c\u6216\u8005\u4efb\u4f55\u6574\u578b\u503c\u6216\u6d6e\u70b9\u578b\u503c\u3002\u6bcf\u4e2a\u539f\u59cb\u503c\u5728\u5b83\u7684\u679a\u4e3e\u58f0\u660e\u4e2d\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002<\/p>\n

    \u5728\u539f\u59cb\u503c\u4e3a\u6574\u6570\u7684\u679a\u4e3e\u65f6\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u7684\u4e3a\u6bcf\u4e00\u4e2a\u6210\u5458\u8d4b\u503c\uff0cSwift\u4f1a\u81ea\u52a8\u4e3a\u4f60\u8d4b\u503c\u3002<\/p>\n

    \u4f8b\u5982\uff0c\u5f53\u4f7f\u7528\u6574\u6570\u4f5c\u4e3a\u539f\u59cb\u503c\u65f6\uff0c\u9690\u5f0f\u8d4b\u503c\u7684\u503c\u4f9d\u6b21\u9012\u589e1\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u503c\u6ca1\u6709\u88ab\u8d4b\u521d\u503c\uff0c\u5c06\u4f1a\u88ab\u81ea\u52a8\u7f6e\u4e3a0\u3002<\/p>\n

    \r\nimport Cocoa\r\n\r\nenum Month: Int {\r\n    case January = 1, February, March, April, May, June, July, August, September, October, November, December\r\n}\r\n\r\nlet yearMonth = Month.May.rawValue\r\nprint(\"\u6570\u5b57\u6708\u4efd\u4e3a: \\(yearMonth)\u3002\")<\/pre>\n

    \u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

    \r\n\u6570\u5b57\u6708\u4efd\u4e3a: 5\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u679a\u4e3e\u7b80\u5355\u7684\u8bf4\u4e5f\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u4e0d\u8fc7\u662f\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u53ea\u5305\u542b\u81ea\u5b9a\u4e49\u7684\u7279\u5b9a\u6570\u636e\uff0c\u5b83\u662f\u4e00\u7ec4\u6709\u5171\u540c\u7279\u6027\u7684\u6570\u636e\u7684\u96c6\u5408\u3002 S […]<\/p>\n","protected":false},"author":1470,"featured_media":175141,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-175223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=175223"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175223\/revisions"}],"predecessor-version":[{"id":175228,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175223\/revisions\/175228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/175141"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=175223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=175223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=175223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}