{"id":546,"date":"2018-02-23T23:39:11","date_gmt":"2018-02-23T15:39:11","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/"},"modified":"2018-02-23T23:39:11","modified_gmt":"2018-02-23T15:39:11","slug":"swift-%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=546","title":{"rendered":"Swift \u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u7c7b\u578b\u8f6c\u6362<\/h1>\n<p>Swift \u8bed\u8a00\u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u5224\u65ad\u5b9e\u4f8b\u7684\u7c7b\u578b\u3002\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u5b9e\u4f8b\u7c7b\u578b\u662f\u5426\u5c5e\u4e8e\u5176\u7236\u7c7b\u6216\u8005\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\nSwift \u4e2d\u7c7b\u578b\u8f6c\u6362\u4f7f\u7528 is \u548c as \u64cd\u4f5c\u7b26\u5b9e\u73b0\uff0cis \u7528\u4e8e\u68c0\u6d4b\u503c\u7684\u7c7b\u578b\uff0cas \u7528\u4e8e\u8f6c\u6362\u7c7b\u578b\u3002<\/p>\n<p>\u7c7b\u578b\u8f6c\u6362\u4e5f\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u4e00\u4e2a\u7c7b\u662f\u5426\u5b9e\u73b0\u4e86\u67d0\u4e2a\u534f\u8bae\u3002<\/p>\n<hr>\n<h2>\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5c42\u6b21<\/h2>\n<p>\u4ee5\u4e0b\u5b9a\u4e49\u4e86\u4e09\u4e2a\u7c7b\uff1aSubjects\u3001Chemistry\u3001Maths\uff0cChemistry \u548c Maths \u7ee7\u627f\u4e86 Subjects\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\nclass Subjects {\n    var physics: String\n    init(physics: String) {\n        self.physics = physics\n    }\n}\n\nclass Chemistry: Subjects {\n    var equations: String\n    init(physics: String, equations: String) {\n        self.equations = equations\n        super.init(physics: physics)\n    }\n}\n\nclass Maths: Subjects {\n    var formulae: String\n    init(physics: String, formulae: String) {\n        self.formulae = formulae\n        super.init(physics: physics)\n    }\n}\n\nlet sa = [\n    Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\"),\n    Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")]\n\n\nlet samplechem = Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplechem.physics)\")\nprint(\"\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \\(samplechem.equations)\")\n\n\nlet samplemaths = Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplemaths.physics)\")\nprint(\"\u5b9e\u4f8b\u516c\u5f0f\u662f: \\(samplemaths.formulae)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u56fa\u4f53\u7269\u7406\n\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \u8d6b\u5179\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u6d41\u4f53\u52a8\u529b\u5b66\n\u5b9e\u4f8b\u516c\u5f0f\u662f: \u5343\u5146\u8d6b\n<\/pre>\n<hr>\n<h2>\u68c0\u67e5\u7c7b\u578b<\/h2>\n<p>\u7c7b\u578b\u8f6c\u6362\u7528\u4e8e\u68c0\u6d4b\u5b9e\u4f8b\u7c7b\u578b\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u5b9e\u4f8b\u7c7b\u578b\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5c06\u5b83\u7528\u5728\u7c7b\u548c\u5b50\u7c7b\u7684\u5c42\u6b21\u7ed3\u6784\u4e0a\uff0c\u68c0\u67e5\u7279\u5b9a\u7c7b\u5b9e\u4f8b\u7684\u7c7b\u578b\u5e76\u4e14\u8f6c\u6362\u8fd9\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u7c7b\u578b\u6210\u4e3a\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u5176\u4ed6\u7c7b\u578b\u3002<\/p>\n<p>\u7c7b\u578b\u68c0\u67e5\u4f7f\u7528 <b>is<\/b> \u5173\u952e\u5b57\u3002<\/p>\n<p>\u64cd\u4f5c\u7b26 <b>is<\/b> \u6765\u68c0\u67e5\u4e00\u4e2a\u5b9e\u4f8b\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u5b50\u7c7b\u578b\u3002\u82e5\u5b9e\u4f8b\u5c5e\u4e8e\u90a3\u4e2a\u5b50\u7c7b\u578b\uff0c\u7c7b\u578b\u68c0\u67e5\u64cd\u4f5c\u7b26\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002<\/p>\n<pre>\nclass Subjects {\n    var physics: String\n    init(physics: String) {\n        self.physics = physics\n    }\n}\n\nclass Chemistry: Subjects {\n    var equations: String\n    init(physics: String, equations: String) {\n        self.equations = equations\n        super.init(physics: physics)\n    }\n}\n\nclass Maths: Subjects {\n    var formulae: String\n    init(physics: String, formulae: String) {\n        self.formulae = formulae\n        super.init(physics: physics)\n    }\n}\n\nlet sa = [\n    Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\"),\n    Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\"),\n    Chemistry(physics: \"\u70ed\u7269\u7406\u5b66\", equations: \"\u5206\u8d1d\"),\n    Maths(physics: \"\u5929\u4f53\u7269\u7406\u5b66\", formulae: \"\u5146\u8d6b\"),\n    Maths(physics: \"\u5fae\u5206\u65b9\u7a0b\", formulae: \"\u4f59\u5f26\u7ea7\u6570\")]\n\n\nlet samplechem = Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplechem.physics)\")\nprint(\"\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \\(samplechem.equations)\")\n\n\nlet samplemaths = Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplemaths.physics)\")\nprint(\"\u5b9e\u4f8b\u516c\u5f0f\u662f: \\(samplemaths.formulae)\")\n\nvar chemCount = 0\nvar mathsCount = 0\nfor item in sa {\n    \/\/ \u5982\u679c\u662f\u4e00\u4e2a Chemistry \u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u8fd4\u56de true\uff0c\u76f8\u53cd\u8fd4\u56de false\u3002\n    if item is Chemistry {\n        ++chemCount\n    } else if item is Maths {\n        ++mathsCount\n    }\n}\n\nprint(\"\u5316\u5b66\u79d1\u76ee\u5305\u542b \\(chemCount) \u4e2a\u4e3b\u9898\uff0c\u6570\u5b66\u5305\u542b \\(mathsCount) \u4e2a\u4e3b\u9898\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u56fa\u4f53\u7269\u7406\n\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \u8d6b\u5179\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u6d41\u4f53\u52a8\u529b\u5b66\n\u5b9e\u4f8b\u516c\u5f0f\u662f: \u5343\u5146\u8d6b\n\u5316\u5b66\u79d1\u76ee\u5305\u542b 2 \u4e2a\u4e3b\u9898\uff0c\u6570\u5b66\u5305\u542b 3 \u4e2a\u4e3b\u9898\n<\/pre>\n<hr>\n<h2>\u5411\u4e0b\u8f6c\u578b<\/h2>\n<p>\u5411\u4e0b\u8f6c\u578b\uff0c\u7528\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26(as? \u6216 as!)<\/p>\n<p>\u5f53\u4f60\u4e0d\u786e\u5b9a\u5411\u4e0b\u8f6c\u578b\u53ef\u4ee5\u6210\u529f\u65f6\uff0c\u7528\u7c7b\u578b\u8f6c\u6362\u7684\u6761\u4ef6\u5f62\u5f0f(as?)\u3002\u6761\u4ef6\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u53ef\u9009\u503c\uff08optional value\uff09\uff0c\u5e76\u4e14\u82e5\u4e0b\u8f6c\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u53ef\u9009\u503c\u5c06\u662f nil\u3002\n<\/p>\n<p>\n\u53ea\u6709\u4f60\u53ef\u4ee5\u786e\u5b9a\u5411\u4e0b\u8f6c\u578b\u4e00\u5b9a\u4f1a\u6210\u529f\u65f6\uff0c\u624d\u4f7f\u7528\u5f3a\u5236\u5f62\u5f0f(as!)\u3002\u5f53\u4f60\u8bd5\u56fe\u5411\u4e0b\u8f6c\u578b\u4e3a\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u7c7b\u578b\u65f6\uff0c\u5f3a\u5236\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\u4f1a\u89e6\u53d1\u4e00\u4e2a\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<pre>\nclass Subjects {\n    var physics: String\n    init(physics: String) {\n        self.physics = physics\n    }\n}\n\nclass Chemistry: Subjects {\n    var equations: String\n    init(physics: String, equations: String) {\n        self.equations = equations\n        super.init(physics: physics)\n    }\n}\n\nclass Maths: Subjects {\n    var formulae: String\n    init(physics: String, formulae: String) {\n        self.formulae = formulae\n        super.init(physics: physics)\n    }\n}\n\nlet sa = [\n    Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\"),\n    Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\"),\n    Chemistry(physics: \"\u70ed\u7269\u7406\u5b66\", equations: \"\u5206\u8d1d\"),\n    Maths(physics: \"\u5929\u4f53\u7269\u7406\u5b66\", formulae: \"\u5146\u8d6b\"),\n    Maths(physics: \"\u5fae\u5206\u65b9\u7a0b\", formulae: \"\u4f59\u5f26\u7ea7\u6570\")]\n\n\nlet samplechem = Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplechem.physics)\")\nprint(\"\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \\(samplechem.equations)\")\n\n\nlet samplemaths = Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplemaths.physics)\")\nprint(\"\u5b9e\u4f8b\u516c\u5f0f\u662f: \\(samplemaths.formulae)\")\n\nvar chemCount = 0\nvar mathsCount = 0\n\nfor item in sa {\n    \/\/ \u7c7b\u578b\u8f6c\u6362\u7684\u6761\u4ef6\u5f62\u5f0f\n    if let show = item as? Chemistry {\n        print(\"\u5316\u5b66\u4e3b\u9898\u662f: '\\(show.physics)', \\(show.equations)\")\n        \/\/ \u5f3a\u5236\u5f62\u5f0f\n    } else if let example = item as? Maths {\n        print(\"\u6570\u5b66\u4e3b\u9898\u662f: '\\(example.physics)',  \\(example.formulae)\")\n    }\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u56fa\u4f53\u7269\u7406\n\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \u8d6b\u5179\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u6d41\u4f53\u52a8\u529b\u5b66\n\u5b9e\u4f8b\u516c\u5f0f\u662f: \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u56fa\u4f53\u7269\u7406', \u8d6b\u5179\n\u6570\u5b66\u4e3b\u9898\u662f: '\u6d41\u4f53\u52a8\u529b\u5b66',  \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u70ed\u7269\u7406\u5b66', \u5206\u8d1d\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5929\u4f53\u7269\u7406\u5b66',  \u5146\u8d6b\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5fae\u5206\u65b9\u7a0b',  \u4f59\u5f26\u7ea7\u6570\n<\/pre>\n<hr>\n<h2>Any\u548cAnyObject\u7684\u7c7b\u578b\u8f6c\u6362<\/h2>\n<p>Swift\u4e3a\u4e0d\u786e\u5b9a\u7c7b\u578b\u63d0\u4f9b\u4e86\u4e24\u79cd\u7279\u6b8a\u7c7b\u578b\u522b\u540d\uff1a<\/p>\n<ul>\n<li><code>AnyObject<\/code>\u53ef\u4ee5\u4ee3\u8868\u4efb\u4f55class\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002<\/li>\n<li><code>Any<\/code>\u53ef\u4ee5\u8868\u793a\u4efb\u4f55\u7c7b\u578b\uff0c\u5305\u62ec\u65b9\u6cd5\u7c7b\u578b\uff08function types\uff09\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u53ea\u6709\u5f53\u4f60\u660e\u786e\u7684\u9700\u8981\u5b83\u7684\u884c\u4e3a\u548c\u529f\u80fd\u65f6\u624d\u4f7f\u7528<code>Any<\/code>\u548c<code>AnyObject<\/code>\u3002\u5728\u4f60\u7684\u4ee3\u7801\u91cc\u4f7f\u7528\u4f60\u671f\u671b\u7684\u660e\u786e\u7684\u7c7b\u578b\u603b\u662f\u66f4\u597d\u7684\u3002<\/p>\n<\/blockquote>\n<h3>Any \u5b9e\u4f8b<\/h3>\n<pre>\nclass Subjects {\n    var physics: String\n    init(physics: String) {\n        self.physics = physics\n    }\n}\n\nclass Chemistry: Subjects {\n    var equations: String\n    init(physics: String, equations: String) {\n        self.equations = equations\n        super.init(physics: physics)\n    }\n}\n\nclass Maths: Subjects {\n    var formulae: String\n    init(physics: String, formulae: String) {\n        self.formulae = formulae\n        super.init(physics: physics)\n    }\n}\n\nlet sa = [\n    Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\"),\n    Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\"),\n    Chemistry(physics: \"\u70ed\u7269\u7406\u5b66\", equations: \"\u5206\u8d1d\"),\n    Maths(physics: \"\u5929\u4f53\u7269\u7406\u5b66\", formulae: \"\u5146\u8d6b\"),\n    Maths(physics: \"\u5fae\u5206\u65b9\u7a0b\", formulae: \"\u4f59\u5f26\u7ea7\u6570\")]\n\n\nlet samplechem = Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplechem.physics)\")\nprint(\"\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \\(samplechem.equations)\")\n\n\nlet samplemaths = Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplemaths.physics)\")\nprint(\"\u5b9e\u4f8b\u516c\u5f0f\u662f: \\(samplemaths.formulae)\")\n\nvar chemCount = 0\nvar mathsCount = 0\n\nfor item in sa {\n    \/\/ \u7c7b\u578b\u8f6c\u6362\u7684\u6761\u4ef6\u5f62\u5f0f\n    if let show = item as? Chemistry {\n        print(\"\u5316\u5b66\u4e3b\u9898\u662f: '\\(show.physics)', \\(show.equations)\")\n        \/\/ \u5f3a\u5236\u5f62\u5f0f\n    } else if let example = item as? Maths {\n        print(\"\u6570\u5b66\u4e3b\u9898\u662f: '\\(example.physics)',  \\(example.formulae)\")\n    }\n}\n\n\/\/ \u53ef\u4ee5\u5b58\u50a8Any\u7c7b\u578b\u7684\u6570\u7ec4 exampleany\nvar exampleany = [Any]()\n\nexampleany.append(12)\nexampleany.append(3.14159)\nexampleany.append(\"Any \u5b9e\u4f8b\")\nexampleany.append(Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u5146\u8d6b\"))\n\nfor item2 in exampleany {\n    switch item2 {\n    case let someInt as Int:\n        print(\"\u6574\u578b\u503c\u4e3a \\(someInt)\")\n    case let someDouble as Double where someDouble &gt; 0:\n        print(\"Pi \u503c\u4e3a \\(someDouble)\")\n    case let someString as String:\n        print(\"\\(someString)\")\n    case let phy as Chemistry:\n        print(\"\u4e3b\u9898 '\\(phy.physics)', \\(phy.equations)\")\n    default:\n        print(\"None\")\n    }\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u56fa\u4f53\u7269\u7406\n\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \u8d6b\u5179\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u6d41\u4f53\u52a8\u529b\u5b66\n\u5b9e\u4f8b\u516c\u5f0f\u662f: \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u56fa\u4f53\u7269\u7406', \u8d6b\u5179\n\u6570\u5b66\u4e3b\u9898\u662f: '\u6d41\u4f53\u52a8\u529b\u5b66',  \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u70ed\u7269\u7406\u5b66', \u5206\u8d1d\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5929\u4f53\u7269\u7406\u5b66',  \u5146\u8d6b\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5fae\u5206\u65b9\u7a0b',  \u4f59\u5f26\u7ea7\u6570\n\u6574\u578b\u503c\u4e3a 12\nPi \u503c\u4e3a 3.14159\nAny \u5b9e\u4f8b\n\u4e3b\u9898 '\u56fa\u4f53\u7269\u7406', \u5146\u8d6b\n<\/pre>\n<h3>AnyObject \u5b9e\u4f8b<\/h3>\n<pre>\nclass Subjects {\n    var physics: String\n    init(physics: String) {\n        self.physics = physics\n    }\n}\n\nclass Chemistry: Subjects {\n    var equations: String\n    init(physics: String, equations: String) {\n        self.equations = equations\n        super.init(physics: physics)\n    }\n}\n\nclass Maths: Subjects {\n    var formulae: String\n    init(physics: String, formulae: String) {\n        self.formulae = formulae\n        super.init(physics: physics)\n    }\n}\n\n\/\/ [AnyObject] \u7c7b\u578b\u7684\u6570\u7ec4\nlet saprint: [AnyObject] = [\n    Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\"),\n    Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\"),\n    Chemistry(physics: \"\u70ed\u7269\u7406\u5b66\", equations: \"\u5206\u8d1d\"),\n    Maths(physics: \"\u5929\u4f53\u7269\u7406\u5b66\", formulae: \"\u5146\u8d6b\"),\n    Maths(physics: \"\u5fae\u5206\u65b9\u7a0b\", formulae: \"\u4f59\u5f26\u7ea7\u6570\")]\n\n\nlet samplechem = Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u8d6b\u5179\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplechem.physics)\")\nprint(\"\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \\(samplechem.equations)\")\n\n\nlet samplemaths = Maths(physics: \"\u6d41\u4f53\u52a8\u529b\u5b66\", formulae: \"\u5343\u5146\u8d6b\")\nprint(\"\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \\(samplemaths.physics)\")\nprint(\"\u5b9e\u4f8b\u516c\u5f0f\u662f: \\(samplemaths.formulae)\")\n\nvar chemCount = 0\nvar mathsCount = 0\n\nfor item in saprint {\n    \/\/ \u7c7b\u578b\u8f6c\u6362\u7684\u6761\u4ef6\u5f62\u5f0f\n    if let show = item as? Chemistry {\n        print(\"\u5316\u5b66\u4e3b\u9898\u662f: '\\(show.physics)', \\(show.equations)\")\n        \/\/ \u5f3a\u5236\u5f62\u5f0f\n    } else if let example = item as? Maths {\n        print(\"\u6570\u5b66\u4e3b\u9898\u662f: '\\(example.physics)',  \\(example.formulae)\")\n    }\n}\n\nvar exampleany = [Any]()\nexampleany.append(12)\nexampleany.append(3.14159)\nexampleany.append(\"Any \u5b9e\u4f8b\")\nexampleany.append(Chemistry(physics: \"\u56fa\u4f53\u7269\u7406\", equations: \"\u5146\u8d6b\"))\n\nfor item2 in exampleany {\n    switch item2 {\n    case let someInt as Int:\n        print(\"\u6574\u578b\u503c\u4e3a \\(someInt)\")\n    case let someDouble as Double where someDouble &gt; 0:\n        print(\"Pi \u503c\u4e3a \\(someDouble)\")\n    case let someString as String:\n        print(\"\\(someString)\")\n    case let phy as Chemistry:\n        print(\"\u4e3b\u9898 '\\(phy.physics)', \\(phy.equations)\")\n    default:\n        print(\"None\")\n    }\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u56fa\u4f53\u7269\u7406\n\u5b9e\u4f8b\u65b9\u7a0b\u5f0f: \u8d6b\u5179\n\u5b9e\u4f8b\u7269\u7406\u5b66\u662f: \u6d41\u4f53\u52a8\u529b\u5b66\n\u5b9e\u4f8b\u516c\u5f0f\u662f: \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u56fa\u4f53\u7269\u7406', \u8d6b\u5179\n\u6570\u5b66\u4e3b\u9898\u662f: '\u6d41\u4f53\u52a8\u529b\u5b66',  \u5343\u5146\u8d6b\n\u5316\u5b66\u4e3b\u9898\u662f: '\u70ed\u7269\u7406\u5b66', \u5206\u8d1d\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5929\u4f53\u7269\u7406\u5b66',  \u5146\u8d6b\n\u6570\u5b66\u4e3b\u9898\u662f: '\u5fae\u5206\u65b9\u7a0b',  \u4f59\u5f26\u7ea7\u6570\n\u6574\u578b\u503c\u4e3a 12\nPi \u503c\u4e3a 3.14159\nAny \u5b9e\u4f8b\n\u4e3b\u9898 '\u56fa\u4f53\u7269\u7406', \u5146\u8d6b\n<\/pre>\n<p> \u5728\u4e00\u4e2aswitch\u8bed\u53e5\u7684case\u4e2d\u4f7f\u7528\u5f3a\u5236\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\uff08as, \u800c\u4e0d\u662f as?\uff09\u6765\u68c0\u67e5\u548c\u8f6c\u6362\u5230\u4e00\u4e2a\u660e\u786e\u7684\u7c7b\u578b\u3002<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u7c7b\u578b\u8f6c\u6362 Swift \u8bed\u8a00\u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u5224\u65ad\u5b9e\u4f8b\u7684\u7c7b\u578b\u3002\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u5b9e\u4f8b\u7c7b\u578b\u662f&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=546\">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-546","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\/546","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=546"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/546\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}