无法转换类型' Int'期望参数类型' UInt'

时间:2016-11-02 18:34:01

标签: swift xcode textfield

我试图将insertspaceInExpiryDateTextField()函数调用到textField()函数中并获得上述错误。

//使到期日期文本字段采取" /" 2位数之后。

func insertspaceInExpiryDateTextField(string: String,inout andPreserveCursorPosition cursorPosition: UInt) -> String {

    var stringWithAddedSpaces: String = ""
    for index in 0.stride(to: string.characters.count, by: 1) {
        if index != 0 && index % 2 == 0 {
            stringWithAddedSpaces += " "

            if index < Int(cursorPosition) {
                cursorPosition += 1
            }
        }
        let characterToAdd: Character = Array(string.characters) [index]
        stringWithAddedSpaces.append(characterToAdd)
    }
    return stringWithAddedSpaces
}

//将上述函数调用到此函数中 //使Expirey Date Field仅占4位数....还给出&#34; /&#34;在2位数之后划分月份和年份。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {



if textField == self.txt_expDate {

            self.insertspaceInExpiryDateTextField(textField.text!, andPreserveCursorPosition: (&UInt(range.length)))     // Here I get the error

            txt_expDate.text! = txt_expDate.text!.stringByReplacingOccurrencesOfString(" ", withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)

            return ((txt_expDate.text?.characters.count)! >= 4 && range.length == 0) ? false : true
        }
}

//其他一切正常,但UInt错误除外。

//我还查了Error 'cannot convert value of type 'int' to expected argument type 'UInt'但没有用..

我有什么特别的错误吗?谢谢 感谢

1 个答案:

答案 0 :(得分:0)

NSRange的长度属性被桥接为Int而不是Swift中的UInt(我不知道为什么; its NSUInteger in Objective C)。您可以按照以下方式在游乐场中查看:

print(type(of: NSRange(location: 0, length: 0).length))