在回答这个问题时,我们发现了一个调用标签是必需的init.这在Swift中是正常的.
class Foo {
init(one: Int, two: String) { }
}
let foo = Foo(42, "Hello world") // Missing argument labels 'one:two:' in call
Run Code Online (Sandbox Code Playgroud)
然而,陌生人的力量在起作用:
extension Foo {
func run(one: String, two: [Int]) { }
}
foo.run(one: "Goodbye", two: []) // Extraneous argument label 'one:' in call
Run Code Online (Sandbox Code Playgroud)
要在此处使用参数标签,必须明确声明.
我没有在文档中看到非常详尽的解释所有这些内容.哪些类/实例/全局函数是必需的参数标签?是否始终使用参数标签导出和导入Obj-C方法?
我喜欢将常用的方法放在一个单独的文件中.我发现这个答案使用Swift中另一个类中的一个类的函数,但是我按照我想要的方式使用它会出错.
假设我想创建一个名为msgBox的方法,弹出一个警告框.我创建了一个单独的空Swift文件并将此代码放入其中.
import UIKit
class Utils: UIViewController {
class func msgBox (titleStr:String = "Untitled", messageStr:String = "Alert text", buttonStr:String = "Ok") {
var alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: buttonStr, style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我想这样称呼它,但我这样做会出错.有谁知道我做错了什么?
Utils.msgBox(titleStr: "Hello!", messageStr: "Are you sure?")
Run Code Online (Sandbox Code Playgroud)