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爜鎴浘

Swift Tips 32 - Assigning to self in struct initializers

浠g爜鍑哄: Swift Tips 032 by John Sundell[1]

灏忕瑪璁?/h2>

杩欐浠g爜鍦ㄨ浠€涔?/h3>

杩欐浠g爜涓?Bool 绫诲瀷杩涜浜嗘墿灞曪紝骞跺埄鐢ㄦ墿灞曚负鐜版湁鐨?Bool 绫诲瀷娣诲姞浜嗕竴涓柊鐨勬瀯閫犲櫒锛?/span>init(input: String)銆傚綋杈撳叆鍙傛暟涓?nbsp;yyes馃憤 鐨勬椂鍊欙紝鏋勯€犲嚭鏉ョ殑瀹炰緥鍊间负 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紝鎴戜滑搴旇鍥炴兂璧蜂簡鍊肩被鍨嬩笌绫荤被鍨嬪湪鏋勯€犺繃绋嬩腑鐨勫樊寮傦紝鎯冲繀杩欐椂鍊欑殑澶у锛屽浜庝唬鐮佹埅鍥句腑鑳界洿鎺ヤ娇鐢?self锛岃€屼笉闇€瑕佽皟鐢?super.init() 绛夋搷浣滀篃閮借兘鐞嗚В浜嗐€?/p>

濡傛灉杩樻槸鏈夊緢澶氱枒鎯戯紝涓嶅Θ閲嶆俯涓€涓嬪畼鏂规墜鍐岄噷鍏充簬鏋勯€犺繃绋嬬殑绔犺妭[5]锛?/p>

璁?Struct 鏋勯€犲櫒鍙樺緱鏇村ソ鐢ㄤ竴鐐癸紒

涓婁竴鑺傛垜浠槓杩颁簡鍊肩被鍨嬩笌绫荤被鍨嬪湪鏋勯€犺繃绋嬩腑鐨勫尯鍒紝鐜板湪鎴戜滑灏嗚瑙掕仛闆嗗湪鍊肩被鍨嬬殑鏋勯€犲櫒涓婏紝涓嬮潰鏄竴涓崄鍒嗗父瑙佺殑鍦烘櫙锛?/p>

褰撶粨鏋勪綋鍐呮病鏈変换浣曡嚜瀹氫箟鏋勯€犲櫒鐨勬椂鍊欙紝Swift 鑳藉涓?Struct 绫诲瀷鐢熸垚涓€涓粯璁ゆ瀯閫犲櫒锛屼篃鍙互鍙畠涓?/span>閫愪竴鎴愬憳鏋勯€犲櫒锛坢emberwise initializer锛夛紝渚嬪涓嬮潰鐨勪唬鐮侊紝Swift 灏变负 myStruct 缁撴瀯浣撳垱寤轰簡涓€涓瀯閫犲櫒锛?/span>init(myString: String?, myInt: Int?, myDouble: Double?)

struct myStruct {
let myString: String?
let myInt: Int?
let myDouble: Double?
}

Swift Tips 32 - Assigning to self in struct initializers

铏界劧杩欎釜鑷姩鐢熸垚鐨勯粯璁ゆ瀯閫犲櫒鐪嬭捣鏉ヤ腑瑙勪腑鐭╋紝涔熷府鎴戜滑鐪佸幓浜嗕笉灏戞墦瀛楃殑鍔熷か锛屼絾鍦ㄦ瘡娆′娇鐢ㄧ殑鏃跺€欙紝鎴戜滑涓嶅緱涓嶅畬鏁寸殑鍐欏嚭涓変釜鍙傛暟锛屽嵆浣胯繖涓弬鏁版槸娌℃湁鍊肩殑锛岃繖纭疄鏈夌偣楹荤儲锛?/p>

let aStruct = myStruct(myString: "1", myInt: nil, myDouble: nil)

閭d箞鏈変粈涔堝ソ鐨勬妧宸ф潵瑙e喅杩欎釜闂鍛紵

绛旀灏辨槸閲嶆柊澹版槑鏁翠釜鏋勯€犲嚱鏁板苟鍦ㄦ瀯閫犲嚱鏁扮殑姣忎釜鍙傛暟鍚庨潰娣诲姞榛樿鍊间负 nil 鐨勯€昏緫锛?/p>

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>

涓嶈繃锛岃繖骞朵笉鎰忓懗鐫€浣犲彧鏈?2 涓瀯閫犳柟娉曞彲浠ョ敤鍝︼紝鐜板湪鐨勪綘涔熷彲浠ョ敤涓嬮潰鐨勬柟寮忔潵鏋勯€犲疄渚嬩簡锛?/p>

myStructWithInit(myString: "Something")
//or
myStructWithInit(myString: "Something", myInt: 2)
//or
myStructWithInit(myString: "Something", myInt: 2, myDouble: 3.0)

鎯婁笉鎯婂枩锛屾剰涓嶆剰澶栵紒鐜板湪鎴戜滑缁堜簬鍙互鑷敱鐨勪娇鐢ㄦ瀯閫犲嚱鏁板暒锛?/p>

涔嬫墍浠ヨ兘杩欐牱鍋氾紝鏄洜涓哄綋鎴戜滑涓哄嚱鏁扮殑鏌愪釜鍙傛暟璁剧疆浜嗛粯璁ゅ€煎悗锛屽啀璋冪敤璇ュ嚱鏁版椂锛屽氨鍙互蹇界暐瀹冦€傝繖涓煡璇嗙偣鍙互浠庡畼鏂规墜鍐岄噷鐨?span class="mq-108">榛樿鍑芥暟鍊?/span>[6]涓€鑺備腑鎵惧埌銆?/p>

鎵€浠ワ紝浠婂ぉ浣犲浼氬浣曠敤榛樿鍙傛暟鏉ヤ紭鍖栫粨鏋勪綋鐨勬瀯閫犲嚱鏁颁娇鐢ㄤ綋楠屼簡涔堬紵

鍙傝€冭祫鏂?/span>

[1]

Swift Tips 032 by John Sundell: https://github.com/JohnSundell/SwiftTips#32-assigning-to-self-in-struct-initializers

[2]

涓ゆ寮忕殑鏋勯€犺繃绋? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#two-phase-initialization

[3]

鏋勯€犲櫒浠g悊鐨勪笁涓師鍒? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#initializer-delegation-for-class-types

[4]

鍥涚鏋勯€犲畨鍏ㄦ鏌? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization#an-quan-jian-cha-1

[5]

鏋勯€犺繃绋嬬殑绔犺妭: https://swiftgg.gitbook.io/swift/swift-jiao-cheng/14_initialization

[6]

榛樿鍑芥暟鍊? https://swiftgg.gitbook.io/swift/swift-jiao-cheng/06_functions#default-parameter-values


以上是关于Swift Tips 32 - Assigning to self in struct initializers的主要内容,如果未能解决你的问题,请参考以下文章

Swift Tips(18-32)

记OC迁移至swift中笔记20tips

Swift Tips

Swift Tips

Objective-C 和 Swift 混编项目的小 Tips

Swift Tips(1-17)