0

I am still on the learning curve of Swift/iOS and get confused by functions in the following format. As only coder is specified as a NSCoder type, I do not understand why you would need decoder in there.

At this link, decoder is an unarchiver object, but what is unarchiver?

Could someone explain this?

init(coder decoder: NSCoder)

2 Answers 2

2

init(coder decoder: NSCoder) defines a function where coder is public param name (visible when you call the method) and decoder is local variable name (to be referenced to inside the method).

Unarchiver is an object responsible for restoring the objects from the storage data (e.g. when you extract the object saved in user defaults).

Sign up to request clarification or add additional context in comments.

Comments

1

Take a look:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

That says:

External Parameter Names

Sometimes it’s useful to name each parameter when you call a function, to indicate the purpose of each argument you pass to the function.

The word coder just describes what the next parameter is/will do. In init functions I think you are always forced to write that external parameter names.

In custom/normal functions is up to you to include these names, or just the parameters directly.

So you could do:

 func sumToNumbers(theFirstNumber firstNumber :NSInteger, andSecondNumber secondNumber:NSInteger){
   }
or
  func sumToNumbers(firstNumber :NSInteger, secondNumber:NSInteger){

 }

Regards.

Comments

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.