swift - Default initializer not working as expected for class contained in struct -
most simplified version of issue can expressed via code :
struct outer { class person { var name:string? } let member = person() } throws compile error : outer.person cannot constructed because has no accessible initializers.
any hints on why doesn't compile ?
possible workarounds (either 1 eliminate compile error) :
- setting initial value nil optional property var name:string? = nil . doesn't partially contradicts purpose of declaring optional (not needing explicitly assign value upon declaration)?
- explicitly mentioning reference type when creating class instance let member:person = person() . why isn't type inference doing same thing behind scenes ?
- replacing outer struct class.
observation, second workaround doesn't in case class local, belonging func :
func checkperson() { class person { var name:string? } let member:person = person() // still throws compile error }
case workaround nr 1. ( setting initial value nil ) 1 removes compile error.
i using xcode 6.2, os x yosemite 10.10.2
in xcode 6.2(swift 1.1), person class not have init() method included.
change code the following:
struct outer { class person { var name:string? init(){} } let member = person() } seems apple has fixed issue in xcode 6.3 beta(swift 1.2), wont show compile error, following:
struct outer { class person { var name:string? } let member = person() }
Comments
Post a Comment