【发布时间】:2016-11-02 18:34:00
【问题描述】:
我正在尝试将 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
}
// 将上面的函数调用到这个函数中 // 使到期日期字段只占用 4 位数字......同时在 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',但没有用..
我有什么特别的地方出错了吗?谢谢 谢谢
【问题讨论】:
-
self.insertspaceInExpiryDateTextField(textField.text!, andPreserveCursorPosition: (&UInt(range.length))) // 这里我得到错误