鏈〉鍖呭惈鍐呭锛�/p>
Swift 鏄竴闂ㄥ紑鍙�iOS, OS X 鍜�watchOS 搴旂敤鐨勬柊璇█銆傜劧鑰岋紝濡傛灉浣犳湁 C 鎴栬� Objective-C 寮�彂缁忛獙鐨勮瘽锛屼綘浼氬彂鐜�Swift 鐨勫緢澶氬唴瀹归兘鏄綘鐔熸倝鐨勩�
Swift 鍖呭惈浜�C 鍜�Objective-C 涓婃墍鏈夊熀纭�暟鎹被鍨嬶紝Int
琛ㄧず鏁村瀷鍊硷紱Double
鍜�code>Float琛ㄧず娴偣鍨嬪�锛�code>Bool鏄竷灏斿瀷鍊硷紱String
鏄枃鏈瀷鏁版嵁銆係wift 杩樻彁渚涗簡涓変釜鍩烘湰鐨勯泦鍚堢被鍨嬶紝Array
锛�code>Set鍜�code>Dictionary锛岃瑙�a href="http://www.swift51.com/swift2.1/chapter2/04_Collection_Types.html">闆嗗悎绫诲瀷銆�/p>
灏卞儚 C 璇█涓�牱锛孲wift 浣跨敤鍙橀噺鏉ヨ繘琛屽瓨鍌ㄥ苟閫氳繃鍙橀噺鍚嶆潵鍏宠仈鍊笺�鍦�Swift 涓紝骞挎硾鐨勪娇鐢ㄧ潃鍊间笉鍙彉鐨勫彉閲忥紝瀹冧滑灏辨槸甯搁噺锛岃�涓旀瘮 C 璇█鐨勫父閲忔洿寮哄ぇ銆傚湪 Swift 涓紝濡傛灉浣犺澶勭悊鐨勫�涓嶉渶瑕佹敼鍙橈紝閭d娇鐢ㄥ父閲忓彲浠ヨ浣犵殑浠g爜鏇村姞瀹夊叏骞朵笖鏇存竻鏅板湴琛ㄨ揪浣犵殑鎰忓浘銆�/p>
闄や簡鎴戜滑鐔熸倝鐨勭被鍨嬶紝Swift 杩樺鍔犱簡 Objective-C 涓病鏈夌殑楂橀樁鏁版嵁绫诲瀷姣斿鍏冪粍锛圱uple锛夈�鍏冪粍鍙互璁╀綘鍒涘缓鎴栬�浼犻�涓�粍鏁版嵁锛屾瘮濡備綔涓哄嚱鏁扮殑杩斿洖鍊兼椂锛屼綘鍙互鐢ㄤ竴涓厓缁勫彲浠ヨ繑鍥炲涓�銆�/p>
Swift 杩樺鍔犱簡鍙�锛圤ptional锛夌被鍨嬶紝鐢ㄤ簬澶勭悊鍊肩己澶辩殑鎯呭喌銆傚彲閫夎〃绀�ldquo;閭e効鏈変竴涓�锛屽苟涓斿畠绛変簬 x ”鎴栬�“閭e効娌℃湁鍊�rdquo;銆傚彲閫夋湁鐐瑰儚鍦�Objective-C 涓娇鐢�code>nil锛屼絾鏄畠鍙互鐢ㄥ湪浠讳綍绫诲瀷涓婏紝涓嶄粎浠呮槸绫汇�鍙�绫诲瀷姣�Objective-C 涓殑nil
鎸囬拡鏇村姞瀹夊叏涔熸洿鍏疯〃鐜板姏锛屽畠鏄�Swift 璁稿寮哄ぇ鐗规�鐨勯噸瑕佺粍鎴愰儴鍒嗐�
Swift 鏄竴闂ㄧ被鍨嬪畨鍏ㄧ殑璇█锛屽彲閫夌被鍨嬪氨鏄竴涓緢濂界殑渚嬪瓙銆係wift 鍙互璁╀綘娓呮鍦扮煡閬撳�鐨勭被鍨嬨�濡傛灉浣犵殑浠g爜鏈熸湜寰楀埌涓�釜String
锛岀被鍨嬪畨鍏ㄤ細闃绘浣犱笉灏忓績浼犲叆涓�釜Int
銆備綘鍙互鍦ㄥ紑鍙戦樁娈靛敖鏃╁彂鐜板苟淇閿欒銆�/p>
杩欎袱琛屼唬鐮佸彲浠ヨ鐞嗚В涓猴細
“澹版槑涓�釜鍚嶅瓧鏄�code>maximumNumberOfLoginAttempts鐨勬柊甯搁噺锛屽苟缁欏畠涓�釜鍊�code>10銆傜劧鍚庯紝澹版槑涓�釜鍚嶅瓧鏄�code>currentLoginAttempt鐨勫彉閲忓苟灏嗗畠鐨勫�鍒濆鍖栦负0
銆�rdquo;
鍦ㄨ繖涓緥瀛愪腑锛屽厑璁哥殑鏈�ぇ灏濊瘯鐧诲綍娆℃暟琚0鏄庝负涓�釜甯搁噺锛屽洜涓鸿繖涓�涓嶄細鏀瑰彉銆傚綋鍓嶅皾璇曠櫥褰曟鏁拌澹版槑涓轰竴涓彉閲忥紝鍥犱负姣忔灏濊瘯鐧诲綍澶辫触鐨勬椂鍊欓兘闇�澧炲姞杩欎釜鍊笺�
浣犲彲浠ュ湪涓�涓0鏄庡涓父閲忔垨鑰呭涓彉閲忥紝鐢ㄩ�鍙烽殧寮�細
var x = 0.0, y = 0.0, z = 0.0
娉ㄦ剰锛�br />濡傛灉浣犵殑浠g爜涓湁涓嶉渶瑕佹敼鍙樼殑鍊硷紝璇蜂娇鐢�code>let鍏抽敭瀛楀皢瀹冨0鏄庝负甯搁噺銆傚彧灏嗛渶瑕佹敼鍙樼殑鍊煎0鏄庝负鍙橀噺銆�/p>
褰撲綘澹版槑甯搁噺鎴栬�鍙橀噺鐨勬椂鍊欏彲浠ュ姞涓婄被鍨嬫爣娉紙type annotation锛夛紝璇存槑甯搁噺鎴栬�鍙橀噺涓瀛樺偍鐨勫�鐨勭被鍨嬨�濡傛灉瑕佹坊鍔犵被鍨嬫爣娉紝闇�鍦ㄥ父閲忔垨鑰呭彉閲忓悕鍚庨潰鍔犱笂涓�釜鍐掑彿鍜岀┖鏍硷紝鐒跺悗鍔犱笂绫诲瀷鍚嶇О銆�/p>
杩欎釜渚嬪瓙缁�code>welcomeMessage鍙橀噺娣诲姞浜嗙被鍨嬫爣娉紝琛ㄧず杩欎釜鍙橀噺鍙互瀛樺偍String
绫诲瀷鐨勫�锛�/p>
var welcomeMessage: String
澹版槑涓殑鍐掑彿浠h〃鐫�ldquo;鏄�..绫诲瀷”锛屾墍浠ヨ繖琛屼唬鐮佸彲浠ヨ鐞嗚В涓猴細
“澹版槑涓�釜绫诲瀷涓�code>String锛屽悕瀛椾负welcomeMessage
鐨勫彉閲忋�”
“绫诲瀷涓�code>String”鐨勬剰鎬濇槸“鍙互瀛樺偍浠绘剰String
绫诲瀷鐨勫�銆�rdquo;
welcomeMessage
鍙橀噺鐜板湪鍙互琚缃垚浠绘剰瀛楃涓诧細
welcomeMessage = "Hello"
浣犲彲浠ュ湪涓�涓畾涔夊涓悓鏍风被鍨嬬殑鍙橀噺锛岀敤閫楀彿鍒嗗壊锛屽苟鍦ㄦ渶鍚庝竴涓彉閲忓悕涔嬪悗娣诲姞绫诲瀷鏍囨敞锛�/p>
var red, green, blue: Double
娉ㄦ剰锛�br />涓�埇鏉ヨ浣犲緢灏戦渶瑕佸啓绫诲瀷鏍囨敞銆傚鏋滀綘鍦ㄥ0鏄庡父閲忔垨鑰呭彉閲忕殑鏃跺�璧嬩簡涓�釜鍒濆鍊硷紝Swift鍙互鎺ㄦ柇鍑鸿繖涓父閲忔垨鑰呭彉閲忕殑绫诲瀷锛岃鍙傝�绫诲瀷瀹夊叏鍜岀被鍨嬫帹鏂�/a>銆傚湪涓婇潰鐨勪緥瀛愪腑锛屾病鏈夌粰
welcomeMessage
璧嬪垵濮嬪�锛屾墍浠ュ彉閲�code>welcomeMessage鐨勭被鍨嬫槸閫氳繃涓�釜绫诲瀷鏍囨敞鎸囧畾鐨勶紝鑰屼笉鏄�杩囧垵濮嬪�鎺ㄦ柇鐨勩�
浣犲彲浠ョ敤浠讳綍浣犲枩娆㈢殑瀛楃浣滀负甯搁噺鍜屽彉閲忓悕锛屽寘鎷�Unicode 瀛楃锛�/p>
let π = 3.14159 let 浣犲ソ = "浣犲ソ涓栫晫" let 馃惗馃惍 = "dogcow"
甯搁噺涓庡彉閲忓悕涓嶈兘鍖呭惈鏁板绗﹀彿锛岀澶达紝淇濈暀鐨勶紙鎴栬�闈炴硶鐨勶級Unicode 鐮佷綅锛岃繛绾夸笌鍒惰〃绗︺�涔熶笉鑳戒互鏁板瓧寮�ご锛屼絾鏄彲浠ュ湪甯搁噺涓庡彉閲忓悕鐨勫叾浠栧湴鏂瑰寘鍚暟瀛椼�
涓�棪浣犲皢甯搁噺鎴栬�鍙橀噺澹版槑涓虹‘瀹氱殑绫诲瀷锛屼綘灏变笉鑳戒娇鐢ㄧ浉鍚岀殑鍚嶅瓧鍐嶆杩涜澹版槑锛屾垨鑰呮敼鍙樺叾瀛樺偍鐨勫�鐨勭被鍨嬨�鍚屾椂锛屼綘涔熶笉鑳藉皢甯搁噺涓庡彉閲忚繘琛屼簰杞�
娉ㄦ剰锛�br />濡傛灉浣犻渶瑕佷娇鐢ㄤ笌Swift淇濈暀鍏抽敭瀛楃浉鍚岀殑鍚嶇О浣滀负甯搁噺鎴栬�鍙橀噺鍚嶏紝浣犲彲浠ヤ娇鐢ㄥ弽寮曞彿锛坄锛夊皢鍏抽敭瀛楀寘鍥寸殑鏂瑰紡灏嗗叾浣滀负鍚嶅瓧浣跨敤銆傛棤璁哄浣曪紝浣犲簲褰撻伩鍏嶄娇鐢ㄥ叧閿瓧浣滀负甯搁噺鎴栧彉閲忓悕锛岄櫎闈炰綘鍒棤閫夋嫨銆�/p>
浣犲彲浠ユ洿鏀圭幇鏈夌殑鍙橀噺鍊间负鍏朵粬鍚岀被鍨嬬殑鍊硷紝鍦ㄤ笅闈㈢殑渚嬪瓙涓紝friendlyWelcome
鐨勫�浠�code>"Hello!"鏀逛负浜�code>"Bonjour!":
var friendlyWelcome = "Hello!" friendlyWelcome = "Bonjour!" // friendlyWelcome 鐜板湪鏄�"Bonjour!"
涓庡彉閲忎笉鍚岋紝甯搁噺鐨勫�涓�棪琚‘瀹氬氨涓嶈兘鏇存敼浜嗐�灏濊瘯杩欐牱鍋氫細瀵艰嚧缂栬瘧鏃舵姤閿欙細
let languageName = "Swift" languageName = "Swift++" // 杩欎細鎶ョ紪璇戞椂閿欒 - languageName 涓嶅彲鏀瑰彉
print(_:separator:terminator:)
鏄竴涓敤鏉ヨ緭鍑轰竴涓垨澶氫釜鍊煎埌閫傚綋杈撳嚭鍖虹殑鍏ㄥ眬鍑芥暟銆傚鏋滀綘鐢�Xcode锛�code>print(_:separator:terminator:)灏嗕細杈撳嚭鍐呭鍒�ldquo;console”闈㈡澘涓娿�separator
鍜�code>terminator鍙傛暟鍏锋湁榛樿鍊硷紝鍥犳浣犺皟鐢ㄨ繖涓嚱鏁扮殑鏃跺�鍙互蹇界暐瀹冧滑銆傞粯璁ゆ儏鍐典笅锛岃鍑芥暟閫氳繃娣诲姞鎹㈣绗︽潵缁撴潫褰撳墠琛屻�濡傛灉涓嶆兂鎹㈣锛屽彲浠ヤ紶閫掍竴涓┖瀛楃涓茬粰terminator
鍙傛暟--渚嬪锛�code>print(someValue, terminator:"")銆傚叧浜庡弬鏁伴粯璁ゅ�鐨勬洿澶氫俊鎭紝璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/06_Functions.html#default_parameter_values">榛樿鍙傛暟鍊�/a>銆�/p>
Swift 鐢ㄥ瓧绗︿覆鎻掑�锛坰tring interpolation锛夌殑鏂瑰紡鎶婂父閲忓悕鎴栬�鍙橀噺鍚嶅綋鍋氬崰浣嶇鍔犲叆鍒伴暱瀛楃涓蹭腑锛孲wift 浼氱敤褰撳墠甯搁噺鎴栧彉閲忕殑鍊兼浛鎹㈣繖浜涘崰浣嶇銆傚皢甯搁噺鎴栧彉閲忓悕鏀惧叆鍦嗘嫭鍙蜂腑锛屽苟鍦ㄥ紑鎷彿鍓嶄娇鐢ㄥ弽鏂滄潬灏嗗叾杞箟锛�/p>
print("The current value of friendlyWelcome is \(friendlyWelcome)") // 杈撳嚭 "The current value of friendlyWelcome is Bonjour!
娉ㄦ剰锛�br />瀛楃涓叉彃鍊兼墍鏈夊彲鐢ㄧ殑閫夐」锛岃鍙傝�瀛楃涓叉彃鍊�/a>銆�/p>
璇峰皢浣犵殑浠g爜涓殑闈炴墽琛屾枃鏈敞閲婃垚鎻愮ず鎴栬�绗旇浠ユ柟渚夸綘灏嗘潵闃呰銆係wift 鐨勭紪璇戝櫒灏嗕細鍦ㄧ紪璇戜唬鐮佹椂鑷姩蹇界暐鎺夋敞閲婇儴鍒嗐�
Swift 涓殑娉ㄩ噴涓�C 璇█鐨勬敞閲婇潪甯哥浉浼笺�鍗曡娉ㄩ噴浠ュ弻姝f枩鏉狅紙//
锛変綔涓鸿捣濮嬫爣璁�
// 杩欐槸涓�釜娉ㄩ噴
浣犱篃鍙互杩涜澶氳娉ㄩ噴锛屽叾璧峰鏍囪涓哄崟涓鏂滄潬鍚庤窡闅忎竴涓槦鍙凤紙/*
锛夛紝缁堟鏍囪涓轰竴涓槦鍙峰悗璺熼殢鍗曚釜姝f枩鏉狅紙*/
锛�
/* 杩欐槸涓�釜, 澶氳娉ㄩ噴 */
涓�C 璇█澶氳娉ㄩ噴涓嶅悓锛孲wift 鐨勫琛屾敞閲婂彲浠ュ祵濂楀湪鍏跺畠鐨勫琛屾敞閲婁箣涓�浣犲彲浠ュ厛鐢熸垚涓�釜澶氳娉ㄩ噴鍧楋紝鐒跺悗鍦ㄨ繖涓敞閲婂潡涔嬩腑鍐嶅祵濂楁垚绗簩涓琛屾敞閲娿�缁堟娉ㄩ噴鏃跺厛鎻掑叆绗簩涓敞閲婂潡鐨勭粓姝㈡爣璁帮紝鐒跺悗鍐嶆彃鍏ョ涓�釜娉ㄩ噴鍧楃殑缁堟鏍囪锛�/p>
/* 杩欐槸绗竴涓琛屾敞閲婄殑寮�ご /* 杩欐槸绗簩涓宓屽鐨勫琛屾敞閲�*/ 杩欐槸绗竴涓琛屾敞閲婄殑缁撳熬 */
閫氳繃杩愮敤宓屽澶氳娉ㄩ噴锛屼綘鍙互蹇�鏂逛究鐨勬敞閲婃帀涓�ぇ娈典唬鐮侊紝鍗充娇杩欐浠g爜涔嬩腑宸茬粡鍚湁浜嗗琛屾敞閲婂潡銆�/p>
涓庡叾浠栧ぇ閮ㄥ垎缂栫▼璇█涓嶅悓锛孲wift 骞朵笉寮哄埗瑕佹眰浣犲湪姣忔潯璇彞鐨勭粨灏惧浣跨敤鍒嗗彿锛�code>;锛夛紝褰撶劧锛屼綘涔熷彲浠ユ寜鐓т綘鑷繁鐨勪範鎯坊鍔犲垎鍙枫�鏈変竴绉嶆儏鍐典笅蹇呴』瑕佺敤鍒嗗彿锛屽嵆浣犳墦绠楀湪鍚屼竴琛屽唴鍐欏鏉$嫭绔嬬殑璇彞锛�/p>
let cat = "馃惐"; print(cat) // 杈撳嚭 "馃惐"
鏁存暟灏辨槸娌℃湁灏忔暟閮ㄥ垎鐨勬暟瀛楋紝姣斿42
鍜�code>-23銆傛暣鏁板彲浠ユ槸鏈夌鍙�/code>锛堟銆佽礋銆侀浂锛夋垨鑰�code>鏃犵鍙�/code>锛堟銆侀浂锛夈�
Swift 鎻愪緵浜�锛�6锛�2鍜�4浣嶇殑鏈夌鍙峰拰鏃犵鍙锋暣鏁扮被鍨嬨�杩欎簺鏁存暟绫诲瀷鍜�C 璇█鐨勫懡鍚嶆柟寮忓緢鍍忥紝姣斿8浣嶆棤绗﹀彿鏁存暟绫诲瀷鏄�code>UInt8
锛�2浣嶆湁绗﹀彿鏁存暟绫诲瀷鏄�code>Int32銆傚氨鍍�Swift 鐨勫叾浠栫被鍨嬩竴鏍凤紝鏁存暟绫诲瀷閲囩敤澶у啓鍛藉悕娉曘�
浣犲彲浠ヨ闂笉鍚屾暣鏁扮被鍨嬬殑min
鍜�code>max灞炴�鏉ヨ幏鍙栧搴旂被鍨嬬殑鏈�皬鍊煎拰鏈�ぇ鍊硷細
let minValue = UInt8.min // minValue 涓�0锛屾槸 UInt8 绫诲瀷 let maxValue = UInt8.max // maxValue 涓�255锛屾槸 UInt8 绫诲瀷
min
鍜�code>max鎵�紶鍥炲�鐨勭被鍨嬶紝姝f槸鍏舵墍瀵圭殑鏁存暟绫诲瀷(濡備笂渚婾Int8, 鎵�紶鍥炵殑绫诲瀷鏄疷Int8)锛屽彲鐢ㄥ湪琛ㄨ揪寮忎腑鐩稿悓绫诲瀷鍊兼梺銆�/p>
涓�埇鏉ヨ锛屼綘涓嶉渶瑕佷笓闂ㄦ寚瀹氭暣鏁扮殑闀垮害銆係wift 鎻愪緵浜嗕竴涓壒娈婄殑鏁存暟绫诲瀷Int
锛岄暱搴︿笌褰撳墠骞冲彴鐨勫師鐢熷瓧闀跨浉鍚岋細
闄ら潪浣犻渶瑕佺壒瀹氶暱搴︾殑鏁存暟锛屼竴鑸潵璇翠娇鐢�code>Int灏卞浜嗐�杩欏彲浠ユ彁楂樹唬鐮佷竴鑷存�鍜屽彲澶嶇敤鎬с�鍗充娇鏄湪32浣嶅钩鍙颁笂锛�code>Int鍙互瀛樺偍鐨勬暣鏁拌寖鍥翠篃鍙互杈惧埌-2,147,483,648
~2,147,483,647
锛屽ぇ澶氭暟鏃跺�杩欏凡缁忚冻澶熷ぇ浜嗐�
Swift 涔熸彁渚涗簡涓�釜鐗规畩鐨勬棤绗﹀彿绫诲瀷UInt
锛岄暱搴︿笌褰撳墠骞冲彴鐨勫師鐢熷瓧闀跨浉鍚岋細
娉ㄦ剰锛�br />灏介噺涓嶈浣跨敤
UInt
锛岄櫎闈炰綘鐪熺殑闇�瀛樺偍涓�釜鍜屽綋鍓嶅钩鍙板師鐢熷瓧闀跨浉鍚岀殑鏃犵鍙锋暣鏁般�闄や簡杩欑鎯呭喌锛屾渶濂戒娇鐢�code>Int锛屽嵆浣夸綘瑕佸瓨鍌ㄧ殑鍊煎凡鐭ユ槸闈炶礋鐨勩�缁熶竴浣跨敤Int
鍙互鎻愰珮浠g爜鐨勫彲澶嶇敤鎬э紝閬垮厤涓嶅悓绫诲瀷鏁板瓧涔嬮棿鐨勮浆鎹紝骞朵笖鍖归厤鏁板瓧鐨勭被鍨嬫帹鏂紝璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/01_The_Basics.html#type_safety_and_type_inference">绫诲瀷瀹夊叏鍜岀被鍨嬫帹鏂�/a>銆�/p>
鍚岀悊锛屽鏋滀綘娌℃湁缁欐诞鐐瑰瓧闈㈤噺鏍囨槑绫诲瀷锛孲wift 浼氭帹鏂綘鎯宠鐨勬槸Double
锛�/p>
let pi = 3.14159 // pi 浼氳鎺ㄦ祴涓�Double 绫诲瀷
褰撴帹鏂诞鐐规暟鐨勭被鍨嬫椂锛孲wift 鎬绘槸浼氶�鎷�code>Double鑰屼笉鏄�code>Float銆�/p>
濡傛灉琛ㄨ揪寮忎腑鍚屾椂鍑虹幇浜嗘暣鏁板拰娴偣鏁帮紝浼氳鎺ㄦ柇涓�code>Double绫诲瀷锛�/p>
let anotherPi = 3 + 0.14159 // anotherPi 浼氳鎺ㄦ祴涓�Double 绫诲瀷
鍘熷鍊�code>3娌℃湁鏄惧紡澹版槑绫诲瀷锛岃�琛ㄨ揪寮忎腑鍑虹幇浜嗕竴涓诞鐐瑰瓧闈㈤噺锛屾墍浠ヨ〃杈惧紡浼氳鎺ㄦ柇涓�code>Double绫诲瀷銆�/p>
鏁存暟瀛楅潰閲忓彲浠ヨ鍐欎綔锛�/p>
涓嬮潰鐨勬墍鏈夋暣鏁板瓧闈㈤噺鐨勫崄杩涘埗鍊奸兘鏄�code>17:
let decimalInteger = 17 let binaryInteger = 0b10001 // 浜岃繘鍒剁殑17 let octalInteger = 0o21 // 鍏繘鍒剁殑17 let hexadecimalInteger = 0x11 // 鍗佸叚杩涘埗鐨�7
娴偣瀛楅潰閲忓彲浠ユ槸鍗佽繘鍒讹紙娌℃湁鍓嶇紑锛夋垨鑰呮槸鍗佸叚杩涘埗锛堝墠缂�槸0x
锛夈�灏忔暟鐐逛袱杈瑰繀椤绘湁鑷冲皯涓�釜鍗佽繘鍒舵暟瀛楋紙鎴栬�鏄崄鍏繘鍒剁殑鏁板瓧锛夈�鍗佽繘鍒舵诞鐐规暟涔熷彲浠ユ湁涓�釜鍙�鐨勬寚鏁帮紙exponent)锛岄�杩囧ぇ鍐欐垨鑰呭皬鍐欑殑 e
鏉ユ寚瀹氾紱鍗佸叚杩涘埗娴偣鏁板繀椤绘湁涓�釜鎸囨暟锛岄�杩囧ぇ鍐欐垨鑰呭皬鍐欑殑 p
鏉ユ寚瀹氥�
濡傛灉涓�釜鍗佽繘鍒舵暟鐨勬寚鏁颁负exp
锛岄偅杩欎釜鏁扮浉褰撲簬鍩烘暟鍜�0^exp鐨勪箻绉細
1.25e2
琛ㄧず 1.25 × 10^2锛岀瓑浜�nbsp;125.0
銆�/li> 1.25e-2
琛ㄧず 1.25 × 10^-2锛岀瓑浜�nbsp;0.0125
銆�/li> 濡傛灉涓�釜鍗佸叚杩涘埗鏁扮殑鎸囨暟涓�code>exp锛岄偅杩欎釜鏁扮浉褰撲簬鍩烘暟鍜�^exp鐨勪箻绉細
0xFp2
琛ㄧず 15 × 2^2锛岀瓑浜�nbsp;60.0
銆�/li> 0xFp-2
琛ㄧず 15 × 2^-2锛岀瓑浜�nbsp;3.75
銆�/li> 涓嬮潰鐨勮繖浜涙诞鐐瑰瓧闈㈤噺閮界瓑浜庡崄杩涘埗鐨�code>12.1875锛�/p>
let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0
鏁板�绫诲瓧闈㈤噺鍙互鍖呮嫭棰濆鐨勬牸寮忔潵澧炲己鍙鎬с�鏁存暟鍜屾诞鐐规暟閮藉彲浠ユ坊鍔犻澶栫殑闆跺苟涓斿寘鍚笅鍒掔嚎锛屽苟涓嶄細褰卞搷瀛楅潰閲忥細
let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1
涓嶅悓鏁存暟绫诲瀷鐨勫彉閲忓拰甯搁噺鍙互瀛樺偍涓嶅悓鑼冨洿鐨勬暟瀛椼�Int8
绫诲瀷鐨勫父閲忔垨鑰呭彉閲忓彲浠ュ瓨鍌ㄧ殑鏁板瓧鑼冨洿鏄�code>-128~127
锛岃�UInt8
绫诲瀷鐨勫父閲忔垨鑰呭彉閲忚兘瀛樺偍鐨勬暟瀛楄寖鍥存槸0
~255
銆傚鏋滄暟瀛楄秴鍑轰簡甯搁噺鎴栬�鍙橀噺鍙瓨鍌ㄧ殑鑼冨洿锛岀紪璇戠殑鏃跺�浼氭姤閿欙細
let cannotBeNegative: UInt8 = -1 // UInt8 绫诲瀷涓嶈兘瀛樺偍璐熸暟锛屾墍浠ヤ細鎶ラ敊 let tooBig: Int8 = Int8.max + 1 // Int8 绫诲瀷涓嶈兘瀛樺偍瓒呰繃鏈�ぇ鍊肩殑鏁帮紝鎵�互浼氭姤閿�
鐢变簬姣忕鏁存暟绫诲瀷閮藉彲浠ュ瓨鍌ㄤ笉鍚岃寖鍥寸殑鍊硷紝鎵�互浣犲繀椤绘牴鎹笉鍚屾儏鍐甸�鎷╂�浣跨敤鏁板�鍨嬬被鍨嬭浆鎹€�杩欑閫夋嫨鎬т娇鐢ㄧ殑鏂瑰紡锛屽彲浠ラ闃查殣寮忚浆鎹㈢殑閿欒骞惰浣犵殑浠g爜涓殑绫诲瀷杞崲鎰忓浘鍙樺緱娓呮櫚銆�/p>
瑕佸皢涓�鏁板瓧绫诲瀷杞崲鎴愬彟涓�锛屼綘瑕佺敤褰撳墠鍊兼潵鍒濆鍖栦竴涓湡鏈涚被鍨嬬殑鏂版暟瀛楋紝杩欎釜鏁板瓧鐨勭被鍨嬪氨鏄綘鐨勭洰鏍囩被鍨嬨�鍦ㄤ笅闈㈢殑渚嬪瓙涓紝甯搁噺twoThousand
鏄�code>UInt16绫诲瀷锛岀劧鑰屽父閲�code>one鏄�code>UInt8绫诲瀷銆傚畠浠笉鑳界洿鎺ョ浉鍔狅紝鍥犱负瀹冧滑绫诲瀷涓嶅悓銆傛墍浠ヨ璋冪敤UInt16(one)
鏉ュ垱寤轰竴涓柊鐨�code>UInt16鏁板瓧骞剁敤one
鐨勫�鏉ュ垵濮嬪寲锛岀劧鍚庝娇鐢ㄨ繖涓柊鏁板瓧鏉ヨ绠楋細
let twoThousand: UInt16 = 2_000 let one: UInt8 = 1 let twoThousandAndOne = twoThousand + UInt16(one)
鐜板湪涓や釜鏁板瓧鐨勭被鍨嬮兘鏄�code>UInt16锛屽彲浠ヨ繘琛岀浉鍔犮�鐩爣甯搁噺twoThousandAndOne
鐨勭被鍨嬭鎺ㄦ柇涓�code>UInt16锛屽洜涓哄畠鏄袱涓�code>UInt16鍊肩殑鍜屻�
SomeType(ofInitialValue)
鏄皟鐢�Swift 鏋勯�鍣ㄥ苟浼犲叆涓�釜鍒濆鍊肩殑榛樿鏂规硶銆傚湪璇█鍐呴儴锛�code>UInt16鏈変竴涓瀯閫犲櫒锛屽彲浠ユ帴鍙椾竴涓�code>UInt8绫诲瀷鐨勫�锛屾墍浠ヨ繖涓瀯閫犲櫒鍙互鐢ㄧ幇鏈夌殑UInt8
鏉ュ垱寤轰竴涓柊鐨�code>UInt16銆傛敞鎰忥紝浣犲苟涓嶈兘浼犲叆浠绘剰绫诲瀷鐨勫�锛屽彧鑳戒紶鍏�code>UInt16鍐呴儴鏈夊搴旀瀯閫犲櫒鐨勫�銆備笉杩囦綘鍙互鎵╁睍鐜版湁鐨勭被鍨嬫潵璁╁畠鍙互鎺ユ敹鍏朵粬绫诲瀷鐨勫�锛堝寘鎷嚜瀹氫箟绫诲瀷锛夛紝璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/20_Extensions.html">鎵╁睍銆�/p>
鏁存暟鍜屾诞鐐规暟鐨勮浆鎹㈠繀椤绘樉寮忔寚瀹氱被鍨嬶細
let three = 3 let pointOneFourOneFiveNine = 0.14159 let pi = Double(three) + pointOneFourOneFiveNine // pi 绛変簬 3.14159锛屾墍浠ヨ鎺ㄦ祴涓�Double 绫诲瀷
杩欎釜渚嬪瓙涓紝甯搁噺three
鐨勫�琚敤鏉ュ垱寤轰竴涓�code>Double绫诲瀷鐨勫�锛屾墍浠ュ姞鍙蜂袱杈圭殑鏁扮被鍨嬮』鐩稿悓銆傚鏋滀笉杩涜杞崲锛屼袱鑰呮棤娉曠浉鍔犮�
娴偣鏁板埌鏁存暟鐨勫弽鍚戣浆鎹㈠悓鏍疯锛屾暣鏁扮被鍨嬪彲浠ョ敤Double
鎴栬�Float
绫诲瀷鏉ュ垵濮嬪寲锛�/p>
let integerPi = Int(pi) // integerPi 绛変簬 3锛屾墍浠ヨ鎺ㄦ祴涓�Int 绫诲瀷
褰撶敤杩欑鏂瑰紡鏉ュ垵濮嬪寲涓�釜鏂扮殑鏁存暟鍊兼椂锛屾诞鐐瑰�浼氳鎴柇銆備篃灏辨槸璇�code>4.75浼氬彉鎴�code>4锛�code>-3.9浼氬彉鎴�code>-3銆�/p>
娉ㄦ剰锛�br />缁撳悎鏁板瓧绫诲父閲忓拰鍙橀噺涓嶅悓浜庣粨鍚堟暟瀛楃被瀛楅潰閲忋�瀛楅潰閲�code>3鍙互鐩存帴鍜屽瓧闈㈤噺
0.14159
鐩稿姞锛屽洜涓烘暟瀛楀瓧闈㈤噺鏈韩娌℃湁鏄庣‘鐨勭被鍨嬨�瀹冧滑鐨勭被鍨嬪彧鍦ㄧ紪璇戝櫒闇�姹傚�鐨勬椂鍊欒鎺ㄦ祴銆�/p>
绫诲瀷鍒悕锛坱ype aliases锛夊氨鏄粰鐜版湁绫诲瀷瀹氫箟鍙︿竴涓悕瀛椼�浣犲彲浠ヤ娇鐢�code>typealias鍏抽敭瀛楁潵瀹氫箟绫诲瀷鍒悕銆�/p>
褰撲綘鎯宠缁欑幇鏈夌被鍨嬭捣涓�釜鏇存湁鎰忎箟鐨勫悕瀛楁椂锛岀被鍨嬪埆鍚嶉潪甯告湁鐢ㄣ�鍋囪浣犳鍦ㄥ鐞嗙壒瀹氶暱搴︾殑澶栭儴璧勬簮鐨勬暟鎹細
typealias AudioSample = UInt16
瀹氫箟浜嗕竴涓被鍨嬪埆鍚嶄箣鍚庯紝浣犲彲浠ュ湪浠讳綍浣跨敤鍘熷鍚嶇殑鍦版柟浣跨敤鍒悕锛�/p>
var maxAmplitudeFound = AudioSample.min // maxAmplitudeFound 鐜板湪鏄�0
鏈緥涓紝AudioSample
琚畾涔変负UInt16
鐨勪竴涓埆鍚嶃�鍥犱负瀹冩槸鍒悕锛�code>AudioSample.min瀹為檯涓婃槸UInt16.min
锛屾墍浠ヤ細缁�code>maxAmplitudeFound璧嬩竴涓垵鍊�code>0銆�/p>
orangesAreOrange
鍜�code>turnipsAreDelicious鐨勭被鍨嬩細琚帹鏂负Bool
锛屽洜涓哄畠浠殑鍒濆�鏄竷灏斿瓧闈㈤噺銆傚氨鍍忎箣鍓嶆彁鍒扮殑Int
鍜�code>Double涓�牱锛屽鏋滀綘鍒涘缓鍙橀噺鐨勬椂鍊欑粰瀹冧滑璧嬪�true
鎴栬�false
锛岄偅浣犱笉闇�灏嗗父閲忔垨鑰呭彉閲忓0鏄庝负Bool
绫诲瀷銆傚垵濮嬪寲甯搁噺鎴栬�鍙橀噺鐨勬椂鍊欏鏋滄墍璧嬬殑鍊肩被鍨嬪凡鐭ワ紝灏卞彲浠ヨЕ鍙戠被鍨嬫帹鏂紝杩欒 Swift 浠g爜鏇村姞绠�磥骞朵笖鍙鎬ф洿楂樸�
褰撲綘缂栧啓鏉′欢璇彞姣斿if
璇彞鐨勬椂鍊欙紝甯冨皵鍊奸潪甯告湁鐢細
if turnipsAreDelicious { print("Mmm, tasty turnips!") } else { print("Eww, turnips are horrible.") } // 杈撳嚭 "Eww, turnips are horrible."
鏉′欢璇彞锛屼緥濡�code>if锛岃鍙傝�鎺у埗娴�/a>銆�/p>
濡傛灉浣犲湪闇�浣跨敤Bool
绫诲瀷鐨勫湴鏂逛娇鐢ㄤ簡闈炲竷灏斿�锛孲wift 鐨勭被鍨嬪畨鍏ㄦ満鍒朵細鎶ラ敊銆備笅闈㈢殑渚嬪瓙浼氭姤鍛婁竴涓紪璇戞椂閿欒锛�/p>
let i = 1 if i { // 杩欎釜渚嬪瓙涓嶄細閫氳繃缂栬瘧锛屼細鎶ラ敊 }
鐒惰�锛屼笅闈㈢殑渚嬪瓙鏄悎娉曠殑锛�/p>
let i = 1 if i == 1 { // 杩欎釜渚嬪瓙浼氱紪璇戞垚鍔� }
i == 1
鐨勬瘮杈冪粨鏋滄槸Bool
绫诲瀷锛屾墍浠ョ浜屼釜渚嬪瓙鍙互閫氳繃绫诲瀷妫�煡銆傜被浼�code>i == 1杩欐牱鐨勬瘮杈冿紝璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/05_Control_Flow.html">鍩烘湰鎿嶄綔绗�/a>銆�/p>
鍜�Swift 涓殑鍏朵粬绫诲瀷瀹夊叏鐨勪緥瀛愪竴鏍凤紝杩欎釜鏂规硶鍙互閬垮厤閿欒骞朵繚璇佽繖鍧椾唬鐮佺殑鎰忓浘鎬绘槸娓呮櫚鐨勩�
鍏冪粍锛坱uples锛�/em>鎶婂涓�缁勫悎鎴愪竴涓鍚堝�銆傚厓缁勫唴鐨勫�鍙互鏄换鎰忕被鍨嬶紝骞朵笉瑕佹眰鏄浉鍚岀被鍨嬨�
涓嬮潰杩欎釜渚嬪瓙涓紝(404, "Not Found")
鏄竴涓弿杩�nbsp;HTTP 鐘舵�鐮侊紙HTTP status code锛�/em>鐨勫厓缁勩�HTTP 鐘舵�鐮佹槸褰撲綘璇锋眰缃戦〉鐨勬椂鍊�web 鏈嶅姟鍣ㄨ繑鍥炵殑涓�釜鐗规畩鍊笺�濡傛灉浣犺姹傜殑缃戦〉涓嶅瓨鍦ㄥ氨浼氳繑鍥炰竴涓�code>404 Not Found鐘舵�鐮併�
let http404Error = (404, "Not Found") // http404Error 鐨勭被鍨嬫槸 (Int, String)锛屽�鏄�(404, "Not Found")
(404, "Not Found")
鍏冪粍鎶婁竴涓�code>Int鍊煎拰涓�釜String
鍊肩粍鍚堣捣鏉ヨ〃绀�HTTP 鐘舵�鐮佺殑涓や釜閮ㄥ垎锛氫竴涓暟瀛楀拰涓�釜浜虹被鍙鐨勬弿杩般�杩欎釜鍏冪粍鍙互琚弿杩颁负“涓�釜绫诲瀷涓�code>(Int, String)鐨勫厓缁�rdquo;銆�/p>
浣犲彲浠ユ妸浠绘剰椤哄簭鐨勭被鍨嬬粍鍚堟垚涓�釜鍏冪粍锛岃繖涓厓缁勫彲浠ュ寘鍚墍鏈夌被鍨嬨�鍙浣犳兂锛屼綘鍙互鍒涘缓涓�釜绫诲瀷涓�code>(Int, Int, Int)鎴栬�(String, Bool)
鎴栬�鍏朵粬浠讳綍浣犳兂瑕佺殑缁勫悎鐨勫厓缁勩�
浣犲彲浠ュ皢涓�釜鍏冪粍鐨勫唴瀹瑰垎瑙o紙decompose锛夋垚鍗曠嫭鐨勫父閲忓拰鍙橀噺锛岀劧鍚庝綘灏卞彲浠ユ甯镐娇鐢ㄥ畠浠簡锛�/p>
let (statusCode, statusMessage) = http404Error print("The status code is \(statusCode)") // 杈撳嚭 "The status code is 404" print("The status message is \(statusMessage)") // 杈撳嚭 "The status message is Not Found"
濡傛灉浣犲彧闇�涓�儴鍒嗗厓缁勫�锛屽垎瑙g殑鏃跺�鍙互鎶婅蹇界暐鐨勯儴鍒嗙敤涓嬪垝绾匡紙_
锛夋爣璁帮細
let (justTheStatusCode, _) = http404Error print("The status code is \(justTheStatusCode)") // 杈撳嚭 "The status code is 404"
姝ゅ锛屼綘杩樺彲浠ラ�杩囦笅鏍囨潵璁块棶鍏冪粍涓殑鍗曚釜鍏冪礌锛屼笅鏍囦粠闆跺紑濮嬶細
print("The status code is \(http404Error.0)") // 杈撳嚭 "The status code is 404" print("The status message is \(http404Error.1)") // 杈撳嚭 "The status message is Not Found"
浣犲彲浠ュ湪瀹氫箟鍏冪粍鐨勬椂鍊欑粰鍗曚釜鍏冪礌鍛藉悕锛�/p>
let http200Status = (statusCode: 200, description: "OK")
缁欏厓缁勪腑鐨勫厓绱犲懡鍚嶅悗锛屼綘鍙互閫氳繃鍚嶅瓧鏉ヨ幏鍙栬繖浜涘厓绱犵殑鍊硷細
print("The status code is \(http200Status.statusCode)") // 杈撳嚭 "The status code is 200" print("The status message is \(http200Status.description)") // 杈撳嚭 "The status message is OK"
浣滀负鍑芥暟杩斿洖鍊兼椂锛屽厓缁勯潪甯告湁鐢ㄣ�涓�釜鐢ㄦ潵鑾峰彇缃戦〉鐨勫嚱鏁板彲鑳戒細杩斿洖涓�釜(Int, String)
鍏冪粍鏉ユ弿杩版槸鍚﹁幏鍙栨垚鍔熴�鍜屽彧鑳借繑鍥炰竴涓被鍨嬬殑鍊兼瘮杈冭捣鏉ワ紝涓�釜鍖呭惈涓や釜涓嶅悓绫诲瀷鍊肩殑鍏冪粍鍙互璁╁嚱鏁扮殑杩斿洖淇℃伅鏇存湁鐢ㄣ�璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/06_Functions.html#Function_Parameters_and_Return_Values">鍑芥暟鍙傛暟涓庤繑鍥炲�銆�/p>
娉ㄦ剰锛�br />鍏冪粍鍦ㄤ复鏃剁粍缁囧�鐨勬椂鍊欏緢鏈夌敤锛屼絾鏄苟涓嶉�鍚堝垱寤哄鏉傜殑鏁版嵁缁撴瀯銆傚鏋滀綘鐨勬暟鎹粨鏋勫苟涓嶆槸涓存椂浣跨敤锛岃浣跨敤绫绘垨鑰呯粨鏋勪綋鑰屼笉鏄厓缁勩�璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/09_Classes_and_Structures.html">绫诲拰缁撴瀯浣�/a>銆�/p>
浣跨敤鍙�绫诲瀷锛坥ptionals锛�/em>鏉ュ鐞嗗�鍙兘缂哄け鐨勬儏鍐点�鍙�绫诲瀷琛ㄧず锛�/p>
鎴栬�
娉ㄦ剰锛�br />C 鍜�Objective-C 涓苟娌℃湁鍙�绫诲瀷杩欎釜姒傚康銆傛渶鎺ヨ繎鐨勬槸 Objective-C 涓殑涓�釜鐗规�锛屼竴涓柟娉曡涓嶈繑鍥炰竴涓璞¤涓嶈繑鍥�code>nil锛�code>nil琛ㄧず“缂哄皯涓�釜鍚堟硶鐨勫璞�rdquo;銆傜劧鑰岋紝杩欏彧瀵瑰璞¤捣浣滅敤——瀵逛簬缁撴瀯浣擄紝鍩烘湰鐨�C 绫诲瀷鎴栬�鏋氫妇绫诲瀷涓嶈捣浣滅敤銆傚浜庤繖浜涚被鍨嬶紝Objective-C 鏂规硶涓�埇浼氳繑鍥炰竴涓壒娈婂�锛堟瘮濡�code>NSNotFound锛夋潵鏆楃ず鍊肩己澶便�杩欑鏂规硶鍋囪鏂规硶鐨勮皟鐢ㄨ�鐭ラ亾骞惰寰楀鐗规畩鍊艰繘琛屽垽鏂�鐒惰�锛孲wift 鐨勫彲閫夌被鍨嬪彲浠ヨ浣犳殫绀�em>浠绘剰绫诲瀷鐨勫�缂哄け锛屽苟涓嶉渶瑕佷竴涓壒娈婂�銆�/p>
鏉ョ湅涓�釜渚嬪瓙銆係wift 鐨�code>String绫诲瀷鏈変竴绉嶆瀯閫犲櫒锛屼綔鐢ㄦ槸灏嗕竴涓�code>String鍊艰浆鎹㈡垚涓�釜Int
鍊笺�鐒惰�锛屽苟涓嶆槸鎵�湁鐨勫瓧绗︿覆閮藉彲浠ヨ浆鎹㈡垚涓�釜鏁存暟銆傚瓧绗︿覆"123"
鍙互琚浆鎹㈡垚鏁板瓧123
锛屼絾鏄瓧绗︿覆"hello, world"
涓嶈銆�/p>
涓嬮潰鐨勪緥瀛愪娇鐢ㄨ繖绉嶆瀯閫犲櫒鏉ュ皾璇曞皢涓�釜String
杞崲鎴�code>Int锛�/p>
let possibleNumber = "123" let convertedNumber = Int(possibleNumber) // convertedNumber 琚帹娴嬩负绫诲瀷 "Int?"锛�鎴栬�绫诲瀷 "optional Int"
鍥犱负璇ユ瀯閫犲櫒鍙兘浼氬け璐ワ紝鎵�互瀹冭繑鍥炰竴涓�em>鍙�绫诲瀷锛坥ptional锛�code>Int锛岃�涓嶆槸涓�釜Int
銆備竴涓彲閫夌殑Int
琚啓浣�code>Int?鑰屼笉鏄�code>Int銆傞棶鍙锋殫绀哄寘鍚殑鍊兼槸鍙�绫诲瀷锛屼篃灏辨槸璇村彲鑳藉寘鍚�code>Int鍊间篃鍙兘涓嶅寘鍚�銆傦紙涓嶈兘鍖呭惈鍏朵粬浠讳綍鍊兼瘮濡�code>Bool鍊兼垨鑰�code>String鍊笺�鍙兘鏄�code>Int鎴栬�浠�箞閮芥病鏈夈�锛�/p>
浣犲彲浠ョ粰鍙�鍙橀噺璧嬪�涓�code>nil鏉ヨ〃绀哄畠娌℃湁鍊硷細
var serverResponseCode: Int? = 404 // serverResponseCode 鍖呭惈涓�釜鍙�鐨�Int 鍊�404 serverResponseCode = nil // serverResponseCode 鐜板湪涓嶅寘鍚�
娉ㄦ剰锛�br />
nil
涓嶈兘鐢ㄤ簬闈炲彲閫夌殑甯搁噺鍜屽彉閲忋�濡傛灉浣犵殑浠g爜涓湁甯搁噺鎴栬�鍙橀噺闇�澶勭悊鍊肩己澶辩殑鎯呭喌锛岃鎶婂畠浠0鏄庢垚瀵瑰簲鐨勫彲閫夌被鍨嬨�
濡傛灉浣犲0鏄庝竴涓彲閫夊父閲忔垨鑰呭彉閲忎絾鏄病鏈夎祴鍊硷紝瀹冧滑浼氳嚜鍔ㄨ璁剧疆涓�code>nil锛�/p>
var surveyAnswer: String? // surveyAnswer 琚嚜鍔ㄨ缃负 nil
娉ㄦ剰锛�br />Swift 鐨�code>nil鍜�Objective-C 涓殑
nil
骞朵笉涓�牱銆傚湪 Objective-C 涓紝nil
鏄竴涓寚鍚戜笉瀛樺湪瀵硅薄鐨勬寚閽堛�鍦�Swift 涓紝nil
涓嶆槸鎸囬拡——瀹冩槸涓�釜纭畾鐨勫�锛岀敤鏉ヨ〃绀哄�缂哄け銆備换浣曠被鍨嬬殑鍙�鐘舵�閮藉彲浠ヨ璁剧疆涓�code>nil锛屼笉鍙槸瀵硅薄绫诲瀷銆�/p>
浣犲彲浠ヤ娇鐢�code>if璇彞鍜�code>nil姣旇緝鏉ュ垽鏂竴涓彲閫夊�鏄惁鍖呭惈鍊笺�浣犲彲浠ヤ娇鐢�ldquo;鐩哥瓑”(==
)鎴�ldquo;涓嶇瓑”(!=
)鏉ユ墽琛屾瘮杈冦�
濡傛灉鍙�绫诲瀷鏈夊�锛屽畠灏嗕笉绛変簬nil
:
if convertedNumber != nil { print("convertedNumber contains some integer value.") } // 杈撳嚭 "convertedNumber contains some integer value."
褰撲綘纭畾鍙�绫诲瀷纭疄鍖呭惈鍊间箣鍚庯紝浣犲彲浠ュ湪鍙�鐨勫悕瀛楀悗闈㈠姞涓�釜鎰熷徆鍙凤紙!
锛夋潵鑾峰彇鍊笺�杩欎釜鎯婂徆鍙疯〃绀�ldquo;鎴戠煡閬撹繖涓彲閫夋湁鍊硷紝璇蜂娇鐢ㄥ畠銆�rdquo;杩欒绉颁负鍙�鍊肩殑寮哄埗瑙f瀽锛坒orced unwrapping锛�/em>锛�/p>
if convertedNumber != nil { print("convertedNumber has an integer value of \(convertedNumber!).") } // 杈撳嚭 "convertedNumber has an integer value of 123."
鏇村鍏充簬if
璇彞鐨勫唴瀹癸紝璇峰弬鑰�a href="http://www.swift51.com/swift2.1/chapter2/05_Control_Flow.html">鎺у埗娴�/a>銆�/p>
娉ㄦ剰锛�br />浣跨敤
!
鏉ヨ幏鍙栦竴涓笉瀛樺湪鐨勫彲閫夊�浼氬鑷磋繍琛屾椂閿欒銆備娇鐢�code>!鏉ュ己鍒惰В鏋愬�涔嬪墠锛屼竴瀹氳纭畾鍙�鍖呭惈涓�釜闈�code>nil鐨勫�銆�/p>
浣跨敤鍙�缁戝畾锛坥ptional binding锛�/em>鏉ュ垽鏂彲閫夌被鍨嬫槸鍚﹀寘鍚�锛屽鏋滃寘鍚氨鎶婂�璧嬬粰涓�釜涓存椂甯搁噺鎴栬�鍙橀噺銆傚彲閫夌粦瀹氬彲浠ョ敤鍦�code>if鍜�code>while璇彞涓紝杩欐潯璇彞涓嶄粎鍙互鐢ㄦ潵鍒ゆ柇鍙�绫诲瀷涓槸鍚︽湁鍊硷紝鍚屾椂鍙互灏嗗彲閫夌被鍨嬩腑鐨勫�璧嬬粰涓�釜甯搁噺鎴栬�鍙橀噺銆�code>if鍜�code>while璇彞锛岃鍙傝�鎺у埗娴�/a>銆�/p>
鍍忎笅闈㈣繖鏍峰湪if
璇彞涓啓涓�釜鍙�缁戝畾锛�/p>
if let constantName = someOptional { statements }
浣犲彲浠ュ儚涓婇潰杩欐牱浣跨敤鍙�缁戝畾鏉ラ噸鍐�code>possibleNumber杩欎釜渚嬪瓙锛�/p>
if let actualNumber = Int(possibleNumber) { print("\'\(possibleNumber)\' has an integer value of \(actualNumber)") } else { print("\'\(possibleNumber)\' could not be converted to an integer") } // 杈撳嚭 "'123' has an integer value of 123"
杩欐浠g爜鍙互琚悊瑙d负锛�/p>
“濡傛灉Int(possibleNumber)
杩斿洖鐨勫彲閫�code>Int鍖呭惈涓�釜鍊硷紝鍒涘缓涓�釜鍙仛actualNumber
鐨勬柊甯搁噺骞跺皢鍙�鍖呭惈鐨勫�璧嬬粰瀹冦�”
濡傛灉杞崲鎴愬姛锛�code>actualNumber甯搁噺鍙互鍦�code>if璇彞鐨勭涓�釜鍒嗘敮涓娇鐢ㄣ�瀹冨凡缁忚鍙�绫诲瀷 鍖呭惈鐨�/em> 鍊煎垵濮嬪寲杩囷紝鎵�互涓嶉渶瑕佸啀浣跨敤!
鍚庣紑鏉ヨ幏鍙栧畠鐨勫�銆傚湪杩欎釜渚嬪瓙涓紝actualNumber
鍙鐢ㄦ潵杈撳嚭杞崲缁撴灉銆�/p>
浣犲彲浠ュ湪鍙�缁戝畾涓娇鐢ㄥ父閲忓拰鍙橀噺銆傚鏋滀綘鎯冲湪if
璇彞鐨勭涓�釜鍒嗘敮涓搷浣�code>actualNumber鐨勫�锛屼綘鍙互鏀规垚if var actualNumber
锛岃繖鏍峰彲閫夌被鍨嬪寘鍚殑鍊煎氨浼氳璧嬬粰涓�釜鍙橀噺鑰岄潪甯搁噺銆�/p>
浣犲彲浠ュ寘鍚涓彲閫夌粦瀹氬湪if
璇彞涓紝骞朵娇鐢�code>where瀛愬彞鍋氬竷灏斿�鍒ゆ柇銆�/p>
if let firstNumber = Int("4"), secondNumber = Int("42") where firstNumber < secondNumber { print("\(firstNumber) < \(secondNumber)") } // prints "4 < 42"
濡備笂鎵�堪锛屽彲閫夌被鍨嬫殫绀轰簡甯搁噺鎴栬�鍙橀噺鍙互“娌℃湁鍊�rdquo;銆傚彲閫夊彲浠ラ�杩�code>if璇彞鏉ュ垽鏂槸鍚︽湁鍊硷紝濡傛灉鏈夊�鐨勮瘽鍙互閫氳繃鍙�缁戝畾鏉ヨВ鏋愬�銆�/p>
鏈夋椂鍊欏湪绋嬪簭鏋舵瀯涓紝绗竴娆¤璧嬪�涔嬪悗锛屽彲浠ョ‘瀹氫竴涓彲閫夌被鍨�em>鎬讳細鏈夊�銆傚湪杩欑鎯呭喌涓嬶紝姣忔閮借鍒ゆ柇鍜岃В鏋愬彲閫夊�鏄潪甯镐綆鏁堢殑锛屽洜涓哄彲浠ョ‘瀹氬畠鎬讳細鏈夊�銆�/p>
杩欑绫诲瀷鐨勫彲閫夌姸鎬佽瀹氫箟涓洪殣寮忚В鏋愬彲閫夌被鍨嬶紙implicitly unwrapped optionals锛夈�鎶婃兂瑕佺敤浣滃彲閫夌殑绫诲瀷鐨勫悗闈㈢殑闂彿锛�code>String?锛夋敼鎴愭劅鍙瑰彿锛�code>String!锛夋潵澹版槑涓�釜闅愬紡瑙f瀽鍙�绫诲瀷銆�/p>
褰撳彲閫夌被鍨嬭绗竴娆¤祴鍊间箣鍚庡氨鍙互纭畾涔嬪悗涓�洿鏈夊�鐨勬椂鍊欙紝闅愬紡瑙f瀽鍙�绫诲瀷闈炲父鏈夌敤銆傞殣寮忚В鏋愬彲閫夌被鍨嬩富瑕佽鐢ㄥ湪 Swift 涓被鐨勬瀯閫犺繃绋嬩腑锛岃鍙傝�鏃犱富寮曠敤浠ュ強闅愬紡瑙f瀽鍙�灞炴�銆�/p>
涓�釜闅愬紡瑙f瀽鍙�绫诲瀷鍏跺疄灏辨槸涓�釜鏅�鐨勫彲閫夌被鍨嬶紝浣嗘槸鍙互琚綋鍋氶潪鍙�绫诲瀷鏉ヤ娇鐢紝骞朵笉闇�姣忔閮戒娇鐢ㄨВ鏋愭潵鑾峰彇鍙�鍊笺�涓嬮潰鐨勪緥瀛愬睍绀轰簡鍙�绫诲瀷String
鍜岄殣寮忚В鏋愬彲閫夌被鍨�code>String涔嬮棿鐨勫尯鍒細
let possibleString: String? = "An optional string." let forcedString: String = possibleString! // 闇�鎯婂徆鍙锋潵鑾峰彇鍊� let assumedString: String! = "An implicitly unwrapped optional string." let implicitString: String = assumedString // 涓嶉渶瑕佹劅鍙瑰彿
浣犲彲浠ユ妸闅愬紡瑙f瀽鍙�绫诲瀷褰撳仛涓�釜鍙互鑷姩瑙f瀽鐨勫彲閫夌被鍨嬨�浣犺鍋氱殑鍙槸澹版槑鐨勬椂鍊欐妸鎰熷徆鍙锋斁鍒扮被鍨嬬殑缁撳熬锛岃�涓嶆槸姣忔鍙栧�鐨勫彲閫夊悕瀛楃殑缁撳熬銆�/p>
娉ㄦ剰锛�br />濡傛灉浣犲湪闅愬紡瑙f瀽鍙�绫诲瀷娌℃湁鍊肩殑鏃跺�灏濊瘯鍙栧�锛屼細瑙﹀彂杩愯鏃堕敊璇�鍜屼綘鍦ㄦ病鏈夊�鐨勬櫘閫氬彲閫夌被鍨嬪悗闈㈠姞涓�釜鎯婂徆鍙蜂竴鏍枫�
浣犱粛鐒跺彲浠ユ妸闅愬紡瑙f瀽鍙�绫诲瀷褰撳仛鏅�鍙�绫诲瀷鏉ュ垽鏂畠鏄惁鍖呭惈鍊硷細
if assumedString != nil { print(assumedString) } // 杈撳嚭 "An implicitly unwrapped optional string."
浣犱篃鍙互鍦ㄥ彲閫夌粦瀹氫腑浣跨敤闅愬紡瑙f瀽鍙�绫诲瀷鏉ユ鏌ュ苟瑙f瀽瀹冪殑鍊硷細
if let definiteString = assumedString { print(definiteString) } // 杈撳嚭 "An implicitly unwrapped optional string."
娉ㄦ剰锛�br />濡傛灉涓�釜鍙橀噺涔嬪悗鍙兘鍙樻垚
nil
鐨勮瘽璇蜂笉瑕佷娇鐢ㄩ殣寮忚В鏋愬彲閫夌被鍨嬨�濡傛灉浣犻渶瑕佸湪鍙橀噺鐨勭敓鍛藉懆鏈熶腑鍒ゆ柇鏄惁鏄�code>nil鐨勮瘽锛岃浣跨敤鏅�鍙�绫诲瀷銆�/p>
浣犲彲浠ヤ娇鐢�em>閿欒澶勭悊锛坋rror handling锛�/em>鏉ュ簲瀵圭▼搴忔墽琛屼腑鍙兘浼氶亣鍒扮殑閿欒鏉′欢銆�/p>
鐩稿浜庡彲閫変腑杩愮敤鍊肩殑瀛樺湪涓庣己澶辨潵琛ㄨ揪鍑芥暟鐨勬垚鍔熶笌澶辫触锛岄敊璇鐞嗗彲浠ユ帹鏂け璐ョ殑鍘熷洜锛屽苟浼犳挱鑷崇▼搴忕殑鍏朵粬閮ㄥ垎銆�/p>
褰撲竴涓嚱鏁伴亣鍒伴敊璇潯浠讹紝瀹冭兘鎶ラ敊銆傝皟鐢ㄥ嚱鏁扮殑鍦版柟鑳芥姏鍑洪敊璇秷鎭苟鍚堢悊澶勭悊銆�/p>
func canThrowAnError() throws { // 杩欎釜鍑芥暟鏈夊彲鑳芥姏鍑洪敊璇� }
涓�釜鍑芥暟鍙互閫氳繃鍦ㄥ0鏄庝腑娣诲姞throws
鍏抽敭璇嶆潵鎶涘嚭閿欒娑堟伅銆傚綋浣犵殑鍑芥暟鑳芥姏鍑洪敊璇秷鎭椂, 浣犲簲璇ュ湪琛ㄨ揪寮忎腑鍓嶇疆try
鍏抽敭璇嶃�
do { try canThrowAnError() // 娌℃湁閿欒娑堟伅鎶涘嚭 } catch { // 鏈変竴涓敊璇秷鎭姏鍑� }
涓�釜do
璇彞鍒涘缓浜嗕竴涓柊鐨勫寘鍚綔鐢ㄥ煙,浣垮緱閿欒鑳借浼犳挱鍒颁竴涓垨澶氫釜catch
浠庡彞銆�/p>
杩欓噷鏈変竴涓敊璇鐞嗗浣曠敤鏉ュ簲瀵逛笉鍚岄敊璇潯浠剁殑渚嬪瓙銆�/p>
func makeASandwich() throws { // ... } do { try makeASandwich() eatASandwich() } catch Error.OutOfCleanDishes { washDishes() } catch Error.MissingIngredients(let ingredients) { buyGroceries(ingredients) }
鍦ㄦ渚嬩腑锛�code>makeASandwich()锛堝仛涓�釜涓夋槑娌伙級鍑芥暟浼氭姏鍑轰竴涓敊璇秷鎭鏋滄病鏈夊共鍑�殑鐩樺瓙鎴栬�鏌愪釜鍘熸枡缂哄け銆傚洜涓�code>makeASandwich()鎶涘嚭閿欒锛屽嚱鏁拌皟鐢ㄨ鍖呰9鍦�code>try琛ㄨ揪寮忎腑銆傚皢鍑芥暟鍖呰9鍦ㄤ竴涓�code>do璇彞涓�浠讳綍琚姏鍑虹殑閿欒浼氳浼犳挱鍒版彁渚涚殑catch
浠庡彞涓�
濡傛灉娌℃湁閿欒琚姏鍑� eatASandwich()
鍑芥暟浼氳璋冪敤銆傚鏋滀竴涓尮閰�code>Error.OutOfCleanDishes鐨勯敊璇鎶涘嚭,washDishes
鍑芥暟浼氳璋冪敤銆傚鏋滀竴涓尮閰�code>Error.MissingIngredients鐨勯敊璇鎶涘嚭锛�code>buyGroceries(_:)鍑芥暟浼氶殢鐫�catch
鎵�崟鎹夊埌鐨勫叧鑱斿�[String]
琚皟鐢ㄣ�
鎶涘嚭锛屾崟鎹夛紝浠ュ強浼犳挱閿欒浼氬湪閿欒澶勭悊绔犺妭璇︾粏璇存槑銆�/p>
鍙�绫诲瀷鍙互璁╀綘鍒ゆ柇鍊兼槸鍚﹀瓨鍦紝浣犲彲浠ュ湪浠g爜涓紭闆呭湴澶勭悊鍊肩己澶辩殑鎯呭喌銆傜劧鑰岋紝鍦ㄦ煇浜涙儏鍐典笅锛屽鏋滃�缂哄け鎴栬�鍊煎苟涓嶆弧瓒崇壒瀹氱殑鏉′欢锛屼綘鐨勪唬鐮佸彲鑳芥病鍔炴硶缁х画鎵ц銆傝繖鏃讹紝浣犲彲浠ュ湪浣犵殑浠g爜涓Е鍙戜竴涓�em>鏂█锛坅ssertion锛�/em>鏉ョ粨鏉熶唬鐮佽繍琛屽苟閫氳繃璋冭瘯鏉ユ壘鍒板�缂哄け鐨勫師鍥犮�
鏂█浼氬湪杩愯鏃跺垽鏂竴涓�杈戞潯浠舵槸鍚︿负true
銆備粠瀛楅潰鎰忔�鏉ヨ锛屾柇瑷�ldquo;鏂█”涓�釜鏉′欢鏄惁涓虹湡銆備綘鍙互浣跨敤鏂█鏉ヤ繚璇佸湪杩愯鍏朵粬浠g爜涔嬪墠锛屾煇浜涢噸瑕佺殑鏉′欢宸茬粡琚弧瓒炽�濡傛灉鏉′欢鍒ゆ柇涓�code>true锛屼唬鐮佽繍琛屼細缁х画杩涜锛涘鏋滄潯浠跺垽鏂负false
锛屼唬鐮佹墽琛岀粨鏉燂紝浣犵殑搴旂敤琚粓姝€�
濡傛灉浣犵殑浠g爜鍦ㄨ皟璇曠幆澧冧笅瑙﹀彂浜嗕竴涓柇瑷�紝姣斿浣犲湪 Xcode 涓瀯寤哄苟杩愯涓�釜搴旂敤锛屼綘鍙互娓呮鍦扮湅鍒颁笉鍚堟硶鐨勭姸鎬佸彂鐢熷湪鍝噷骞舵鏌ユ柇瑷�瑙﹀彂鏃朵綘鐨勫簲鐢ㄧ殑鐘舵�銆傛澶栵紝鏂█鍏佽浣犻檮鍔犱竴鏉¤皟璇曚俊鎭�
浣犲彲浠ヤ娇鐢ㄥ叏灞�code>assert(_:_file:line:)鍑芥暟鏉ュ啓涓�釜鏂█銆傚悜杩欎釜鍑芥暟浼犲叆涓�釜缁撴灉涓�code>true鎴栬�false
鐨勮〃杈惧紡浠ュ強涓�潯淇℃伅锛屽綋琛ㄨ揪寮忕殑缁撴灉涓�code>false鐨勬椂鍊欒繖鏉′俊鎭細琚樉绀猴細
let age = -3 assert(age >= 0, "A person's age cannot be less than zero") // 鍥犱负 age < 0锛屾墍浠ユ柇瑷�細瑙﹀彂
鍦ㄨ繖涓緥瀛愪腑锛屽彧鏈�code>age >= 0涓�code>true鐨勬椂鍊欙紝鍗�code>age鐨勫�闈炶礋鐨勬椂鍊欙紝浠g爜鎵嶄細缁х画鎵ц銆傚鏋�code>age鐨勫�鏄礋鏁帮紝灏卞儚浠g爜涓偅鏍凤紝age >= 0
涓�code>false锛屾柇瑷�瑙﹀彂锛岀粓姝㈠簲鐢ㄣ�
濡傛灉涓嶉渶瑕佹柇瑷�俊鎭紝鍙互鐪佺暐锛屽氨鍍忚繖鏍凤細
assert(age >= 0)
娉ㄦ剰锛�br />褰撲唬鐮佷娇鐢ㄤ紭鍖栫紪璇戠殑鏃跺�锛屾柇瑷�皢浼氳绂佺敤锛屼緥濡傚湪 Xcode 涓紝浣跨敤榛樿鐨�target Release 閰嶇疆閫夐」鏉�build 鏃讹紝鏂█浼氳绂佺敤銆�/p>
褰撴潯浠跺彲鑳戒负鍋囨椂浣跨敤鏂█锛屼絾鏄渶缁堜竴瀹氳淇濊瘉鏉′欢涓虹湡锛岃繖鏍蜂綘鐨勪唬鐮佹墠鑳界户缁繍琛屻�鏂█鐨勯�鐢ㄦ儏鏅細
nil
锛屼絾鏄悗闈㈢殑浠g爜杩愯闇�涓�釜闈�code>nil鍊笺�