1

Apple's documentation states:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

I have also seen this question, but still wasn't able to find an answer.

//  struct  

    struct FlickrObj { // No Error ????
        var title : String
        var photographer : String

    }  

// class  

class FlickrObj { // will create error: Class 'FlickrObj' has no initializers
    var title : String
    var photographer : String

}

Apple says both classes and structs must set their stored properties but why doesn't the Struct give any compile time errors?‌

Community
  • 1
  • 1
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 2
    See the documentation on [memberwise initialisers for structs](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID213) – Hamish Jun 25 '16 at 16:33
  • 1
    Or my online book: http://www.apeth.com/swiftBook/ch04.html#_struct_initializers_properties_and_methods – matt Jun 25 '16 at 16:37
  • Can someone please enlighten me. Is this a bad question? I mean it would be bad if there were similar questions on SO. Just because its answer is derived from Documentation doesn't mean it's clear. And even if it is...many good answers are extracted from documentation. Seeing 2 very similar ways one giving an error and one not giving an error is confusing. – mfaani Jun 25 '16 at 16:55
  • 1
    The downvotes might be because of “Kindly include details in your answer,” which is redundant (all answers should include relevant detail) and comes off as a bit bossy. That’s a fair downvote. Or it might be that because _they_ have already identified the crucial sentence in the documentation, they think you shouldn’t need to ask. That’s not a fair downvote, IMO. It is genuinely confusing that Swift synthesizes initializers for structs but not classes. – Paul Cantrell Jun 25 '16 at 16:57
  • 1
    @Honey It doesn't look great when you quote from the documentation in your question, only to have the answer given to you on the very same page as that quote, just a few scrolls down. The Swift language guide is also very well written, both in terms of clarity and detail – with plenty of examples. Although that being said, this is indeed a legitimate question that I understand can easily be confusing, so I agree it doesn't warrant a downvote. I would just advise you to do a little bit more reading before asking ;) – Hamish Jun 25 '16 at 17:05
  • @originaluser2 I get your point. Funny thing was I did geek around, looking into the documentation and did Googling and found 2-3 links, spent nearly 50 minutes and then posted only to realize it was a few lines down. – mfaani Jun 25 '16 at 17:13

1 Answers1

1

It's because, in the absence of any explicitly declared initializers, the struct itself declares the implicit memberwise initializer init(title:photographer:).

But classes don't do that.

So the class is failing to fulfill the initialization contract (all instance properties must be initialized before the instance itself finishes initializing), but the struct is not failing the contract.

matt
  • 515,959
  • 87
  • 875
  • 1,141