mos*_*ic6 30 nsurl swift swift2
这是我在更新后遇到的一个有趣的问题 Swift 2.0
错误就if let url = URL.absoluteString行了
func myFormatCompanyMessageText(attributedString: NSMutableAttributedString) -> NSMutableAttributedString
{
// Define text font
attributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Montserrat-Light", size: 17)!, range: NSMakeRange(0, attributedString.length))
return attributedString
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
if let url = URL.absoluteString {
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(url)
}
}
return false
}
Run Code Online (Sandbox Code Playgroud)
and*_*n22 51
编译器告诉你不能使用a,if let因为它完全没必要.您没有任何打开的选项:URL不是可选的,并且该absoluteString属性也不是可选的.if let仅用于打开选项.如果要创建一个名为的新常量url,只需执行以下操作:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
let url = URL.absoluteString
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(url)
}
return false
}
Run Code Online (Sandbox Code Playgroud)
但是,旁注:命名参数URL和命名的局部常量url是令人困惑的.你最好这样:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(URL.absoluteString)
}
return false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53673 次 |
| 最近记录: |