Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the question mark means in public init?(coder aDecoder: NSCoder)?

I don't think the question mark in public init?(coder aDecoder: NSCoder) means optionals. Also, when I override it I find I don't need to write the question mark at all.

So what does it mean exactly ?

--- Update --

The comment below had helped me figure that out, it is called "failable initializer", another example to make the concept easier to understand is UIFont's connivence init because that UIFont may not exist.

public /*not inherited*/ init?(name fontName: String, size fontSize: CGFloat)
like image 866
Qiulang 邱朗 Avatar asked Feb 22 '16 08:02

Qiulang 邱朗


People also ask

What is init ?( Coder Nscoder?

init(coder:)Returns an object initialized from data in a given unarchiver.

What is a coder Swift?

@Raining The syntax is standard Swift function header syntax. "coder" is the name of the parameter that any code that calls this function will be required to use. If you don't want the caller to have to name the param, you write "_" instead of "coder".


1 Answers

It's called failable initializer. In the book, The Swift Programming Language, it describes it as

“It is sometimes useful to define a class, structure, or enumeration for which initialization can fail. This failure might be triggered by invalid initialization parameter values, the absence of a required external resource, or some other condition that prevents initialization from succeeding.”

Check the "Failable Initializers" section in the Swift Docs

like image 107
Ch0k0l8 Avatar answered Sep 28 '22 01:09

Ch0k0l8