简体   繁体   English

Swift中函数参数中的变量类型

[英]Variable type in function params in Swift

I am still on the learning curve of Swift/iOS and get confused by functions in the following format. 我仍然处于Swift / iOS的学习曲线上,并被以下格式的功能所迷惑。 As only coder is specified as a NSCoder type, I do not understand why you would need decoder in there. 由于仅将coder指定为NSCoder类型,因此我不明白为什么在那里需要decoder

At this link , decoder is an unarchiver object, but what is unarchiver ? 此链接上 ,解码器是一个未存档的对象,但是什么是未存档的

Could someone explain this? 有人可以解释一下吗?

init(coder decoder: NSCoder)

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). init(coder decoder: NSCoder)定义了一个函数,其中编码器是公共参数名称(在调用方法时可见),而解码器是局部变量名称(在方法内部引用)。

Unarchiver is an object responsible for restoring the objects from the storage data (eg when you extract the object saved in user defaults). Unarchiver是负责从存储数据还原对象的对象(例如,当您提取用户默认设置中保存的对象时)。

Take a look: 看一看:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html 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. 单词coder仅描述下一个参数将/将要执行的操作。 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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM