加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift学习笔记 - Unicode转码

发布时间:2020-12-14 06:50:42 所属栏目:百科 来源:网络整理
导读:现在用swift写了一个新项目,发现String和NSString还是有很大不同的,在Unicode转码上遇到了问题,我写了一个扩展,把以前OC的转码用swift重写了一遍,发现也能用,下面把我用swift做Unicode转码的代码贴在这里,亲测好用~~ extension String { var unicodeS

现在用swift写了一个新项目,发现String和NSString还是有很大不同的,在Unicode转码上遇到了问题,我写了一个扩展,把以前OC的转码用swift重写了一遍,发现也能用,下面把我用swift做Unicode转码的代码贴在这里,亲测好用~~

extension String {
    var unicodeStr:String {
        let tempStr1 = self.stringByReplacingOccurrencesOfString("u",withString: "U")
        let tempStr2 = tempStr1.stringByReplacingOccurrencesOfString(""",withString: """)
        let tempStr3 = """.stringByAppendingString(tempStr2).stringByAppendingString(""")
        let tempData = tempStr3.dataUsingEncoding(NSUTF8StringEncoding)
        var returnStr:String = ""
        do {
            returnStr = try NSPropertyListSerialization.propertyListWithData(tempData!,options: .Immutable,format: nil) as! String
        } catch {
            print(error)
        }
        return returnStr.stringByReplacingOccurrencesOfString("rn",withString: "n")
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读