Swift Tips 32 - Assigning to self in struct initializers
Posted 涓€鐡滄妧鏈?/a> Swi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift Tips 32 - Assigning to self in struct initializers相关的知识,希望对你有一定的参考价值。
浠g爜鎴浘
浠g爜鍑哄: Swift Tips 032 by John Sundell[1]
灏忕瑪璁?/h2>
杩欐浠g爜鍦ㄨ浠€涔?/h3>
杩欐浠g爜涓?Bool 绫诲瀷杩涜浜嗘墿灞曪紝骞跺埄鐢ㄦ墿灞曚负鐜版湁鐨?Bool 绫诲瀷娣诲姞浜嗕竴涓柊鐨勬瀯閫犲櫒锛?/span>init(input: String)
銆傚綋杈撳叆鍙傛暟涓?nbsp;y
, yes
, 馃憤
鐨勬椂鍊欙紝鏋勯€犲嚭鏉ョ殑瀹炰緥鍊间负 true锛屽叾浣欐儏鍐靛垯涓?false锛岄渶瑕佹彁閱掔殑鏄?Bool 绫诲瀷鏄竴涓粨鏋勪綋锛屼篃鏄€肩被鍨嬨€?/span>
鍊肩被鍨嬪拰绫荤被鍨嬬殑鏋勯€犺繃绋?/h3>
鎴戜滑閮界煡閬擄紝鏋勯€犲櫒鍙互閫氳繃璋冪敤鍏跺畠鏋勯€犲櫒鏉ュ畬鎴愬疄渚嬬殑閮ㄥ垎鏋勯€犺繃绋嬶紝杩欎竴杩囩▼琚О涓烘瀯閫犲櫒浠g悊锛坕nitializer delegation锛夛紝杩欑妯″紡鑳介伩鍏嶅涓瀯閫犲櫒闂寸殑浠g爜閲嶅銆?/p>
浣嗘槸鏋勯€犲櫒浠g悊鐨勫疄鐜拌鍒欏拰缁勭粐褰㈠紡鍦ㄥ€肩被鍨嬶紙value type锛夊拰绫荤被鍨?class type)涓湁鎵€涓嶅悓銆?/p>
鍊肩被鍨嬫槸涓嶆敮鎸佺户鎵跨殑锛屼緥濡傛灇涓惧拰缁撴瀯浣擄紝鎵€浠ュ畠浠殑鏋勯€犲櫒浠g悊杩囩▼鐩稿绠€鍗曪紝鍥犱负瀹冧滑鍙兘浠g悊缁欒嚜宸辩殑鍏跺畠鏋勯€犲櫒銆?/p>
绫诲垯涓嶅悓锛屽畠鍙互缁ф壙鑷叾瀹冪被锛岃繖鎰忓懗鐫€绫昏纭繚鎵€鏈夌户鎵夸笅鏉ョ殑瀛樺偍鍨嬪睘鎬у湪鏋勯€犳椂鑳借姝g‘鐨勫垵濮嬪寲锛岃繖涔熸槸涓轰粈涔堢被绫诲瀷鍦ㄦ瀯閫犺繃绋嬩腑鏈?span class="mq-26">涓ゆ寮忕殑鏋勯€犺繃绋?/span>[2]锛岃閬靛畧鏋勯€犲櫒浠g悊鐨勪笁涓師鍒?/span>[3]锛屽苟杩涜鍥涚鏋勯€犲畨鍏ㄦ鏌?/span>[4]锛?/p>
璇翠簡杩欎箞澶氾紝鍥炲埌浠婂ぉ鐨勪唬鐮佷笂锛?/p>
閫氳繃鍓嶉潰鐨勮瑙o紝鎴戜滑搴旇鍥炴兂璧蜂簡鍊肩被鍨嬩笌绫荤被鍨嬪湪鏋勯€犺繃绋嬩腑鐨勫樊寮傦紝鎯冲繀杩欐椂鍊欑殑澶у锛屽浜庝唬鐮佹埅鍥句腑鑳界洿鎺ヤ娇鐢? 濡傛灉杩樻槸鏈夊緢澶氱枒鎯戯紝涓嶅Θ閲嶆俯涓€涓嬪畼鏂规墜鍐岄噷鍏充簬鏋勯€犺繃绋嬬殑绔犺妭[5]锛?/p>
涓婁竴鑺傛垜浠槓杩颁簡鍊肩被鍨嬩笌绫荤被鍨嬪湪鏋勯€犺繃绋嬩腑鐨勫尯鍒紝鐜板湪鎴戜滑灏嗚瑙掕仛闆嗗湪鍊肩被鍨嬬殑鏋勯€犲櫒涓婏紝涓嬮潰鏄竴涓崄鍒嗗父瑙佺殑鍦烘櫙锛?/p>
褰撶粨鏋勪綋鍐呮病鏈変换浣曡嚜瀹氫箟鏋勯€犲櫒鐨勬椂鍊欙紝Swift 鑳藉涓?Struct 绫诲瀷鐢熸垚涓€涓粯璁ゆ瀯閫犲櫒锛屼篃鍙互鍙畠涓?/span>閫愪竴鎴愬憳鏋勯€犲櫒锛坢emberwise initializer锛夛紝渚嬪涓嬮潰鐨勪唬鐮侊紝Swift 灏变负 铏界劧杩欎釜鑷姩鐢熸垚鐨勯粯璁ゆ瀯閫犲櫒鐪嬭捣鏉ヤ腑瑙勪腑鐭╋紝涔熷府鎴戜滑鐪佸幓浜嗕笉灏戞墦瀛楃殑鍔熷か锛屼絾鍦ㄦ瘡娆′娇鐢ㄧ殑鏃跺€欙紝鎴戜滑涓嶅緱涓嶅畬鏁寸殑鍐欏嚭涓変釜鍙傛暟锛屽嵆浣胯繖涓弬鏁版槸娌℃湁鍊肩殑锛岃繖纭疄鏈夌偣楹荤儲锛?/p>
閭d箞鏈変粈涔堝ソ鐨勬妧宸ф潵瑙e喅杩欎釜闂鍛紵 绛旀灏辨槸閲嶆柊澹版槑鏁翠釜鏋勯€犲嚱鏁板苟鍦ㄦ瀯閫犲嚱鏁扮殑姣忎釜鍙傛暟鍚庨潰娣诲姞榛樿鍊间负 nil 鐨勯€昏緫锛?/p>
姝ゆ椂锛屾垜浠啀娆″垱寤? 涓嶈繃锛岃繖骞朵笉鎰忓懗鐫€浣犲彧鏈?2 涓瀯閫犳柟娉曞彲浠ョ敤鍝︼紝鐜板湪鐨勪綘涔熷彲浠ョ敤涓嬮潰鐨勬柟寮忔潵鏋勯€犲疄渚嬩簡锛?/p>
鎯婁笉鎯婂枩锛屾剰涓嶆剰澶栵紒鐜板湪鎴戜滑缁堜簬鍙互鑷敱鐨勪娇鐢ㄦ瀯閫犲嚱鏁板暒锛?/p>
涔嬫墍浠ヨ兘杩欐牱鍋氾紝鏄洜涓哄綋鎴戜滑涓哄嚱鏁扮殑鏌愪釜鍙傛暟璁剧疆浜嗛粯璁ゅ€煎悗锛屽啀璋冪敤璇ュ嚱鏁版椂锛屽氨鍙互蹇界暐瀹冦€傝繖涓煡璇嗙偣鍙互浠庡畼鏂规墜鍐岄噷鐨?span class="mq-108">榛樿鍑芥暟鍊?/span>[6]涓€鑺備腑鎵惧埌銆?/p>
鎵€浠ワ紝浠婂ぉ浣犲浼氬浣曠敤榛樿鍙傛暟鏉ヤ紭鍖栫粨鏋勪綋鐨勬瀯閫犲嚱鏁颁娇鐢ㄤ綋楠屼簡涔堬紵 Swift Tips 032 by John Sundell: https://github.com/JohnSundell/SwiftTips#32-assigning-to-self-in-struct-initializers 涓ゆ寮忕殑鏋勯€犺繃绋? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#two-phase-initialization 鏋勯€犲櫒浠g悊鐨勪笁涓師鍒? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#initializer-delegation-for-class-types 鍥涚鏋勯€犲畨鍏ㄦ鏌? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#an-quan-jian-cha-1 鏋勯€犺繃绋嬬殑绔犺妭: https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization 榛樿鍑芥暟鍊? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/06_functions#default-parameter-valuesself
锛岃€屼笉闇€瑕佽皟鐢?super.init()
绛夋搷浣滀篃閮借兘鐞嗚В浜嗐€?/p>
璁?Struct 鏋勯€犲櫒鍙樺緱鏇村ソ鐢ㄤ竴鐐癸紒
myStruct
缁撴瀯浣撳垱寤轰簡涓€涓瀯閫犲櫒锛?/span>init(myString: String?, myInt: Int?, myDouble: Double?)
struct myStruct {
let myString: String?
let myInt: Int?
let myDouble: Double?
}let aStruct = myStruct(myString: "1", myInt: nil, myDouble: nil)
struct myStructWithInit {
let myString: String?
let myInt: Int?
let myDouble: Double?
init(myString: String? = nil, //馃憟
myInt: Int? = nil,
myDouble: Double? = nil) {
self.myString = myString
self.myInt = myInt
self.myDouble = myDouble
}
}myStructWithInit
绫诲瀷鐨勫疄渚嬶紝浼氬彂鐜颁唬鐮佹彁绀洪噷鐨勬瀯閫犲嚱鏁板彉鎴愪簡涓や釜锛佷竴涓槸鎴戜滑涔嬪墠瑙佽繃鐨勬牱寮忥紝鍙︿竴涓槸娌℃湁浠讳綍鍙傛暟鐨勬牱寮忋€?/p>
myStructWithInit(myString: "Something")
//or
myStructWithInit(myString: "Something", myInt: 2)
//or
myStructWithInit(myString: "Something", myInt: 2, myDouble: 3.0)鍙傝€冭祫鏂?/span>
以上是关于Swift Tips 32 - Assigning to self in struct initializers的主要内容,如果未能解决你的问题,请参考以下文章