2

Sorry for terrible question, but I've been reading the swift book and following tutorials, and I've found about this NSCoder protocol and disabling it and whatnot.

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

I understand about required inits, but I don't understand this code I have to write. Does it say that if a NSCoder is passed, a fatal error occurs? Also, what does the word coder mean? Why do I need it there in front of the variable name?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Jaxkr
  • 1,236
  • 2
  • 13
  • 33

2 Answers2

3

The purpose of that code is to fulfill the requirement of the NSCoding protocol, which says that you must implement init(coder:), without actually bothering to write any meaningful implementation of that method. If you had something meaningful to do here, you would delete the fatalError line and do something meaningful. As it is, you are saying: "I have no implementation for this, so if it ever is called, we are in serious trouble and I want to crash deliberately!"

The word coder is an "external name" for this parameter. In Swift, parameters can have both an internal name and an external name. coder means, when you call this function, call this parameter coder: (which in fact is just what you do). It is absolutely necessary because this is how the world, including Cocoa, sees this function; coder: is part of its name, its identity. However, the word aDecoder is just the internal name of the local parameter, and is merely a serving suggestion; you can use another internal name or you could even delete it, which would make the internal name the same as the external name.

The situation here is that you are overriding a Cocoa function, and you must use the correct name of that function. That means the function name, the external parameter names, and the parameter types. They must all be exactly right, or it won't be the same function as the function you are supposed to be overriding. But the internal parameter names, the local variables passed into the body of the function, are up to you, and you can change them if you like. I often do.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • OK... So is that why I can simply do coder: NSCoder and I don't need the variable name? – Jaxkr Dec 04 '14 at 03:33
  • Exactly. That keeps the external name and also uses it as the internal name. – matt Dec 04 '14 at 03:33
  • Read the section on function parameter names in the book: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_254 – matt Dec 04 '14 at 03:34
  • Thank you so much. Reading that section of the docs now. So just confirming: when a protocol requires a certain class/struct/whatever to accept a certain variable, you use that name to fulfill the protocol, and then optionally define a local version of the var right after the global version if you want to use a different name. Right? – Jaxkr Dec 04 '14 at 03:36
  • More general than that. When you do a `required` or `override` func, you must get the name and the external parameter names and the parameter types exactly right - but the internal parameter names are totally up to you. – matt Dec 04 '14 at 03:37
  • Thanks for all your help. :) I understand now. And there are no instances when using an internal variable would be required by the compiler, right? As the variables inside the body of a function are on their own scope? It's purely for developer convenience and code quality? – Jaxkr Dec 04 '14 at 03:41
  • Exactly so. That's why I like to change the internal names. `aDecoder` is kind of yucky; `coder` is good enough. And yes they are just local variable names scoped inside the function body. Whereas the external names are fixed labels, like the name of the function itself. (In Objective-C they are _part_ of the name.) – matt Dec 04 '14 at 03:44
0

It means this method is not implemented. It does look like a method somebody needs (the signature matches a contrat/interface/protocol) - thats why it gets a specific parameter (in this case aDecoder). But the method itself is not written yet. You can do just nothing in those cases, but sometimes you dont want to silently fall through, then you just raise an error like the sample code does. Thats not really specific to required init or coders.

eckes
  • 10,103
  • 1
  • 59
  • 71