<分区>
我有一串链接,但是里面有中文。
然后我的应用就会崩溃。
如何防止url是否包含中文,能正常显示链接。
var youTextLabel = UILabel()
var message = "https://zh.wikipedia.org/wiki/斯蒂芬·科里"
let linkAttributes = [NSLinkAttributeName: NSURL(string: message)!, //This get error!!
NSForegroundColorAttributeName: UIColor.blue,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue] as [String : Any]
let attributedString = NSMutableAttributedString(string: message)
let urlCharacterCount = message.characters.count
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlCharacterCount))
youTextLabel.attributedText = attributedString
错误信息:
fatal error: unexpectedly found nil while unwrapping an Optional value