{"rsdb":{"rid":"345951","subhead":"","postdate":"0","aid":"246443","fid":"89","uid":"1","topic":"1","content":"
\n

Swift\u662f\u82f9\u679c\u63a8\u51fa\u7684\u4e00\u4e2a\u6bd4\u8f83\u65b0\u7684\u8bed\u8a00\uff0c\u5b83\u9664\u4e86\u501f\u9274\u8bed\u8a00\u5982C#\u3001Java\u7b49\u5185\u5bb9\u5916\uff0c\u597d\u50cf\u8fd8\u91c7\u7528\u4e86\u5f88\u591ajava<\/a> script\u811a\u672c\u91cc\u9762\u7684\u4e00\u4e9b\u811a\u672c\u8bed\u6cd5\uff0c\u7528\u8d77\u6765\u611f\u89c9\u975e\u5e38\u68d2\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u4f7f\u7528C#\u591a\u5e74\u7684\u6280\u672f\u63a7\uff0c\u5bf9\u8fd9\u79cd\u6bd4\u8f83\u8d85\u524d\u7684\u8bed\u8a00\u975e\u5e38\u611f\u5174\u8da3\uff0c\u4e4b\u524d\u4e5f\u5728\u5b66\u4e60ES6\u8bed\u6cd5\u7684\u65f6\u5019\u5b66\u4e60\u4e86\u962e\u4e00\u5cf0\u7684\u300aECMAScript 6 \u5165\u95e8<\/a>\u300b\uff0c\u5bf9java<\/a> script\u811a\u672c\u7684ES6\u8bed\u6cd5\u5199\u6cd5\u53f9\u4e3a\u89c2\u6b62\uff0c\u8fd9\u79cdSwift\u8bed\u8a00\u4e5f\u5177\u6709\u5f88\u591a\u8fd9\u79cd\u811a\u672c\u8bed\u6cd5\u7684\u7279\u70b9\uff0c\u53ef\u4ee5\u8bf4\u8fd9\u4e2aSwift\u5728\u5438\u6536\u4e86Object C\u7684\u4f18\u70b9\u5e76\u6452\u5f03\u4e00\u4e9b\u4e0d\u597d\u7684\u4e1c\u897f\u5916\uff0c\u540c\u65f6\u5438\u6536\u4e86\u5927\u91cf\u65b0\u4e00\u4ee3\u8bed\u8a00\u7684\u5404\u79cd\u7279\u70b9\uff0c\u5305\u62ec\u6cdb\u578b\u3001\u5143\u7956\u7b49\u7279\u70b9\u3002\u6211\u5728\u5b66\u4e60Swift\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u5b98\u65b9\u7684\u8bed\u8a00\u4ecb\u7ecd\u6587\u7ae0\uff08The Swift Programming Language<\/a>\uff09\u8fd8\u662f\u975e\u5e38\u6d45\u663e\u6613\u61c2\uff0c\u867d\u7136\u662f\u82f1\u6587\uff0c\u4e0d\u8fc7\u4ee3\u7801\u53ca\u5206\u6790\u8bf4\u660e\u90fd\u5f88\u5230\u4f4d\uff0c\u5c31\u662f\u5185\u5bb9\u663e\u5f97\u6bd4\u8f83\u591a\u4e00\u4e9b\uff0c\u800c\u6211\u4eec\u4f5c\u4e3a\u6280\u672f\u4eba\u5458\uff0c\u4e00\u822c\u770b\u4ee3\u7801\u5c31\u5f88\u597d\u4e86\u89e3\u4e86\u5404\u79cd\u8bed\u6cd5\u7279\u70b9\u4e86\uff0c\u57fa\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u6211\u5bf9\u5b98\u7f51\u7684\u6848\u4f8b\u4ee3\u7801\u8fdb\u884c\u4e86\u4e00\u4e2a\u6458\u8981\u603b\u7ed3\uff0c\u4ee5\u4ee3\u7801\u7684\u65b9\u5f0f\u8fdb\u884cSwift\u8bed\u8a00\u7684\u8bed\u6cd5\u7279\u70b9\u4ecb\u7ecd\uff0c\u603b\u7ed3\u4e00\u53e5\u8bdd\u5c31\u662f\uff1a\u5feb\u770bSample\u4ee3\u7801\uff0c\u901f\u5b66Swift\u8bed\u8a00\u3002<\/p> \n

1\u3001\u8bed\u6cd5\u901f\u89c8<\/h3> \n
\n
var<\/span> myVariable = 42\r\nmyVariable <\/span>= 50\r\nlet myConstant <\/span>= 42<\/pre> \n <\/div> \n 

\u53d8\u91cf\u5b9a\u4e49\u7528var\uff0c\u5e38\u91cf\u5219\u7528let\uff0c\u7c7b\u578b\u81ea\u884c\u63a8\u65ad\u3002<\/p> \n

 <\/p> \n

\n
let apples = 3\r\nlet oranges <\/span>= 5\r\nlet appleSummary <\/span>= "I have \\(apples) apples."\r\nlet fruitSummary <\/span>= "I have \\(apples + oranges) pieces of fruit."<\/pre> \n <\/div> \n 

\u7528\u62ec\u53f7\u5305\u542b\u53d8\u91cf<\/p> \n

 <\/p> \n

\n
let quotation = """\r\nI said <\/span>"I have \\(apples) apples."\r\nAnd then I said <\/span>"I have \\(apples + oranges) pieces of fruit."\r\n"""<\/pre> \n <\/div> \n 

\u4ee3\u7801\u901a\u8fc7\u4e09\u4e2a\u53cc\u5f15\u53f7\u6765\u5305\u542b\u9884\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff08\u5305\u62ec\u6362\u884c\u7b26\u53f7\uff09\uff0c\u5de6\u4fa7\u7f29\u8fdb\u7a7a\u683c\u7701\u7565\u3002<\/p> \n

 <\/p> \n

\n
var<\/span> shoppingList = ["catfish", "water", "tulips", "blue paint"]\r\nshoppingList[<\/span>1] = "bottle of water"\r\n \r\nvar<\/span> occupations = [\r\n    <\/span>"Malcolm": "Captain",\r\n    <\/span>"Kaylee": "Mechanic",\r\n]\r\noccupations[<\/span>"Jayne"] = "Public Relations"<\/pre> \n <\/div> \n 

\u6570\u7ec4\u548c\u5b57\u5178\u96c6\u5408\u521d\u59cb\u5316\u7b26\u5408\u5e38\u89c4\uff0c\u5b57\u5178\u540e\u9762\u53ef\u4ee5\u4fdd\u7559\u9017\u53f7\u7ed3\u5c3e<\/p> \n

\n
let emptyArray = [String]()\r\nlet emptyDictionary <\/span>= [String: Float]()<\/pre> \n <\/div> \n 

\u521d\u59cb\u5316\u51fd\u6570\u4e5f\u6bd4\u8f83\u7b80\u6d01\u3002<\/p> \n

 <\/p> \n

\n
let individualScores = [75, 43, 103, 87, 12]\r\n<\/span>var<\/span> teamScore = 0\r\nfor<\/span> score in<\/span> individualScores {\r\n    <\/span>if<\/span> score > 50 {\r\n        teamScore <\/span>+= 3\r\n    } <\/span>else<\/span> {\r\n        teamScore <\/span>+= 1\r\n    }\r\n}\r\nprint(teamScore)<\/span><\/pre> \n <\/div> \n 

\u63a7\u5236\u6d41\u7684if-else\u8fd9\u4e9b\u548c\u5176\u4ed6\u8bed\u8a00\u6ca1\u6709\u4ec0\u4e48\u5dee\u5f02\uff0cfor ... in \u5219\u662f\u8fed\u4ee3\u904d\u5386\u7684\u8bed\u6cd5\uff0c\u63a7\u5236\u6d41\u65b9\u5f0f\u8fd8\u652f\u6301\u5176\u4ed6\u7684while\u3001repeat...while\u7b49\u4e0d\u540c\u7684\u8bed\u6cd5\u3002<\/p> \n

 <\/p> \n

\n
var<\/span> optionalString: String? = "Hello"\r\nprint(optionalString <\/span>== nil)\r\n \r\n<\/span>var<\/span> optionalName: String? = "John Appleseed"\r\nvar<\/span> greeting = "Hello!"\r\nif<\/span> let name = optionalName {\r\n    greeting <\/span>= "Hello, \\(name)"\r\n}<\/span><\/pre> \n <\/div> \n 

\u8fd9\u90e8\u5206\u5219\u662f\u53ef\u7a7a\u7c7b\u578b\u7684\u4f7f\u7528\uff0c\u4ee5\u53ca\u53ef\u7a7a\u5224\u65ad\u8bed\u53e5\u7684\u4f7f\u7528\uff0c\u53ef\u7a7a\u5224\u65ad\u8bed\u53e5\u5728Swift\u4e2d\u4f7f\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u8fd9\u79cd\u76f8\u5f53\u4e8e\u5148\u6c42\u503c\u518d\u5224\u65ad\u662f\u5426\u8fdb\u5165\u5927\u62ec\u7b26\u8bed\u53e5\u3002<\/p> \n

 <\/p> \n

\n
let vegetable = "red pepper"\r\nswitch<\/span> vegetable {\r\n<\/span>case<\/span> "celery":\r\n    print(<\/span>"Add some raisins and make ants on a log.")\r\n<\/span>case<\/span> "cucumber", "watercress":\r\n    print(<\/span>"That would make a good tea sandwich.")\r\n<\/span>case<\/span> let x where x.hasSuffix("pepper"):\r\n    print(<\/span>"Is it a spicy \\(x)?")\r\n<\/span>default<\/span>:\r\n    print(<\/span>"Everything tastes good in soup.")\r\n}<\/span><\/pre> \n <\/div> \n 

Switch\u8bed\u6cd5\u548c\u5e38\u89c4\u7684\u8bed\u8a00\u4e0d\u540c\uff0c\u8fd9\u79cd\u7b80\u5316\u4e86\u4e00\u4e9b\u8bed\u6cd5\uff0c\u6bcf\u4e2a\u5b50\u6761\u4ef6\u4e0d\u7528\u663e\u5f0f\u7684\u5199break\u8bed\u53e5\uff08\u9ed8\u8ba4\u5c31\u662f\u8fd4\u56de\u7684\uff09\uff0c\u591a\u4e2a\u6761\u4ef6\u9017\u53f7\u5206\u5f00\u5373\u53ef\u516c\u7528\u4e00\u4e2a\u5224\u65ad\u5904\u7406\u3002<\/p> \n

 <\/p> \n

\n
let interestingNumbers = [\r\n    <\/span>"Prime": [2, 3, 5, 7, 11, 13],\r\n    <\/span>"Fibonacci": [1, 1, 2, 3, 5, 8],\r\n    <\/span>"Square": [1, 4, 9, 16, 25],\r\n]\r\n<\/span>var<\/span> largest = 0\r\nfor<\/span> (kind, numbers) in<\/span> interestingNumbers {\r\n    <\/span>for<\/span> number in<\/span> numbers {\r\n        <\/span>if<\/span> number > largest {\r\n            largest <\/span>= number\r\n        }\r\n    }\r\n}\r\nprint(largest)<\/span><\/pre> \n <\/div> \n 

\u4e0a\u9762\u5b57\u5178\u904d\u5386\u7684\u65b9\u5f0f\u91c7\u7528for...in\u7684\u65b9\u5f0f\u8fdb\u884c\u904d\u5386\uff0c\u53e6\u5916\u901a\u8fc7(kind, numbers)\u7684\u65b9\u5f0f\u8fdb\u884c\u4e00\u4e2a\u53c2\u6570\u7684\u89e3\u6784\u8fc7\u7a0b\uff0c\u628a\u5b57\u5178\u7684\u952e\u503c\u5206\u522b\u4ed8\u7ed9kind\uff0cnumbers\u8fd9\u4e24\u4e2a\u53c2\u6570\u3002<\/span><\/span><\/p> \n

 <\/p> \n

\n
var<\/span> total = 0\r\nfor<\/span> i in<\/span> 0..<4 {\r\n    total <\/span>+= i\r\n}\r\nprint(total)<\/span><\/pre> \n <\/div> \n 

\u4e0a\u9762\u7684for...in\u5faa\u73af\u91c7\u7528\u4e86\u4e00\u4e2a\u8bed\u6cd5\u7b26\u53f7..<\u5c5e\u4e8e\u6570\u5b66\u534a\u5c01\u95ed\u6982\u5ff5\uff0c\u4ece0\u52304\uff0c\u4e0d\u542b4\uff0c\u540c\u7406\u8fd8\u6709\u5168\u5c01\u95ed\u7b26\u53f7\uff1a...\u5168\u5305\u542b\u5de6\u53f3\u4e24\u4e2a\u8303\u56f4\u7684\u503c\u3002<\/p> \n

 <\/p> \n

\n
func greet(person: String, day: String) -> String {\r\n    <\/span>return<\/span> "Hello \\(person), today is \\(day)."\r\n}\r\ngreet(person: <\/span>"Bob", day: "Tuesd","orderid":"0","title":"\u5feb\u770bSample\u4ee3\u7801\uff0c\u901f\u5b66Swift\u8bed\u8a00\uff081\uff09-\u8bed\u6cd5\u901f\u89c8(\u4e00)","smalltitle":"","mid":"0","fname":"Swift","special_id":"0","bak_id":"0","info":"0","hits":"452","pages":"4","comments":"0","posttime":"2019-09-03 02:41:12","list":"1567449672","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u5feb\u770b<\/A> Sample<\/A> \u4ee3\u7801<\/A> \u901f\u5b66<\/A> Swift<\/A> \u8bed\u8a00<\/A> \u8bed\u6cd5<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"120.229.33.54","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5feb\u770bSample\u4ee3\u7801\uff0c\u901f\u5b66Swift\u8bed\u8a00\uff081\uff09-\u8bed\u6cd5\u901f\u89c8","lastview":"1713291341","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}