swift中怎么样打出表情 我想用print("[高兴]") 这样就条出一个高兴的表情
// swift 并没有自带的函数,不过可以自己写一个。(需要调用Foundation库)
// 你需要自己定义一个类似于database 一类的字典,用于存储所有要替换的字符串和替换成的表情
import Foundation
var database = [
"高兴":"😀",
"点赞":"👍",
"描述":"对应的表情"
]
extension String{
mutating func emojiString(byReplacingCharactersInDictionary dictionary: Dictionary<String, String>) -> String{
var range = self.rangeOfString("")
for (key, emoji) in dictionary{
let combinedStr = "["+key+"]"
while true{
range = self.rangeOfString(combinedStr)
if let place = range{
self = self.stringByReplacingCharactersInRange(place, withString: emoji)
} else {
break
}
}
}
return self
}
}
var str = "我今天很[高兴],想要[点赞]"
print(str.emojiString(byReplacingCharactersInDictionary: database))