对于具有外部参数名称的方法调用,我可以cmd在任何参数名称上单击Xcode以跳转到方法定义.例如,在
let a = Array(count: 3, repeatedValue: 0)
Run Code Online (Sandbox Code Playgroud)
一个cmd上的"计数"或"repeatedValue" -click直接跳转到Array初始化方法
init(count: Int, repeatedValue: Element)
Run Code Online (Sandbox Code Playgroud)
但是,我还没有找到一种方法来为没有外部参数名称的方法调用做同样的事情 ,如
let c = Array("abc".characters)
Run Code Online (Sandbox Code Playgroud)
当然我可以查找该characters方法返回一个String.CharacterView依次符合的方法SequenceType,因此这将调用Array初始化程序
init<S : SequenceType where S.Generator.Element == _Buffer.Element>(_ s: S)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有人 为这种情况找到了直接的"跳转到定义"方法.
如果类型具有许多重载的init方法(没有外部参数名称),那么这将非常有用,以确定实际调用哪一个.
以上示例来自Swift 2/Xcode 7 beta,但问题与特殊的Swift/Xcode版本无关.
(也发布在Apple开发者论坛:https://forums.developer.apple.com/thread/12687.)
我正在尝试验证表单以确保用户输入的是整数而不是字符串。我可以检查数字是否为整数,如下所示:
var possibleNumber = timeRetrieved.text
convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"
if convertedNumber != nil {
println("It's a number!")
totalTime = convertedNumber!
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我想确保用户没有输入任何文本、双打等。我只想要整数。以下代码不起作用,因为如果变量是整数,它的计算结果为 true。如果变量不是整数,我应该使用什么代码来评估?
if convertedNumber != nil {
let alertController = UIAlertController(title: "Validation Error", message: "You must enter an integer number!", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: {(alert : UIAlertAction!) in
alertController.dismissViewControllerAnimated(true, completion: nil)
})
alertController.addAction(alertAction)
presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)