栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Swift中使用stringByReplacingCharactersInRange

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Swift中使用stringByReplacingCharactersInRange

以下是在各种Swift版本中计算结果字符串的方法。

请注意,所有方法的使用

-[NSStringstringByReplacingOccurrencesOfString:withString:]
方式完全相同,只是语法不同。

这是计算结果字符串的首选方法。转换为Swift

Range
并在Swift上使用它
String
容易出错。例如,当对非ASCII字符串进行操作时,Johan的答案在很多方面都是错误的。

斯威夫特3:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {    let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string    // ... do something with `result`}

Swift 2.1:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {    let result = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)    // ... do something with `result`}

Swift 1(仅供参考):

let result = textField.text.bridgeToObjectiveC().stringByReplacingCharactersInRange(range, withString:string)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号