我在objective-c中有这段代码:
[textField.text stringByReplacingCharactersInRange:range withString:string];
尝试转换为swift,如下所示:
textField.text.stringByReplacingCharactersInRange(range, withString: string)
但是编译器说Int is not identical to 'String.index'
我应该如何修改表达式?
我在UITextFieldDelegate方法中使用了objective-c / swift表达式:
func textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String!) -> Bool {
答案 0 :(得分:1)
使用bridgeToObjectiveC()
textField.text.bridgeToObjectiveC().stringByReplacingCharactersInRange(range, withString: string)
它会更加明确
var st = "abc"
str.bridgeToObjectiveC().stringByReplacingCharactersInRange(NSMakeRange(2,1), withString:"r")
也可以对NSString
进行显式投射,而不需要bridgeToObjectiveC
var st = "abc" as NSString
let abc = st.stringByReplacingCharactersInRange(NSMakeRange(2, 3), withString: "abc")