1

Need to initialize an object, but my initializer isn't cooperating. I've got this so far but I'm not sure how to pass parameters to my object initializer(s).

init(coder aDecoder: NSCoder, caseImage: UIImage, inputName: String, inputDate: String) {
    self.caseImage = caseImage
    self.caseName.text = inputName
    self.caseDate.text = inputDate
    super.init( coder: aDecoder )
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

The initializer is being called by this code in another class:

func loadSampleData() {
    let Photo1 = UIImage(named: "retino1")!
    let case1 = TableCell(caseImage: Photo1, inputName: "John Smith", inputDate: "2015-10-18" ) //error
    patientCaseArray = [case1]
}

1 Answer 1

3

According to what you posted, you have to pass in a decoder in your init statement. The following is what you are trying to call, which is invalid because it doesn't exist:

TableCell(UIImage, String, String ) 

So you either need to create it:

init(caseImage: UIImage, inputName: String, inputDate: String) {
    super.init( coder: NSCoder() )
    self.caseImage = caseImage
    self.caseName.text = inputName
    self.caseDate.text = inputDate

}

or pass in NSCoder():

 let case1 = TableCell(coder:NSCoder(), caseImage: Photo1, inputName: "John Smith", inputDate: "2015-10-18" )
Sign up to request clarification or add additional context in comments.

2 Comments

I tried both methods, the first results in a different error. The other results on no change. With the first I get: Type NSCoder does not conform to protocol 'NilLiteralConvertible'.
yes, You can't make NSCoders literally, you need to create the actual item that is based on the NSCoder, I just realized I should have told you to just ignore the parameter and call the init() instead of init(decoder)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.