Here is a fun issue I'm running into after updating to Swift 2.0
更新到Swift 2.0后,我遇到了一个有趣的问题
The error is on the if let url = URL.absoluteString line
错误在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
}
3 个解决方案
#1
36
The compiler is telling you that you can't use an if let
because it's totally unnecessary. You don't have any optionals to unwrap: URL
is not optional, and the absoluteString
property isn't optional either. if let
is used exclusively to unwrap optionals. If you want to create a new constant named url
, just do it:
编译器告诉你,你不能使用if let,因为它完全没有必要。您没有展开的选项:URL不是可选的,absoluteString属性也不是可选的。如果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
}
However, sidenote: having a parameter named URL
and a local constant named url
is mighty confusing. You might be better off like this:
但是,sidenote:有一个名为URL的参数和一个名为URL的本地常量非常令人困惑。你最好这样:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(URL.absoluteString)
}
return false
}
#2
2
Here is your working function:
这是你的工作职能:
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
}
In your function argument make your URL
optional.
在函数参数中,使URL可选。
And unwrap it this way:
然后这样展开:
if let url = URL?.absoluteString {
//your code.
}
#3
0
absoluteString
isn't an optional value, its just a String. You can check if the URL variable is nil
absoluteString不是一个可选的值,它只是一个字符串。您可以检查URL变量是否为nil
if let url = yourURLVariable {
// do your textView function
} else {
// handle nil url
}
#1
36
The compiler is telling you that you can't use an if let
because it's totally unnecessary. You don't have any optionals to unwrap: URL
is not optional, and the absoluteString
property isn't optional either. if let
is used exclusively to unwrap optionals. If you want to create a new constant named url
, just do it:
编译器告诉你,你不能使用if let,因为它完全没有必要。您没有展开的选项:URL不是可选的,absoluteString属性也不是可选的。如果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
}
However, sidenote: having a parameter named URL
and a local constant named url
is mighty confusing. You might be better off like this:
但是,sidenote:有一个名为URL的参数和一个名为URL的本地常量非常令人困惑。你最好这样:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(URL.absoluteString)
}
return false
}
#2
2
Here is your working function:
这是你的工作职能:
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
}
In your function argument make your URL
optional.
在函数参数中,使URL可选。
And unwrap it this way:
然后这样展开:
if let url = URL?.absoluteString {
//your code.
}
#3
0
absoluteString
isn't an optional value, its just a String. You can check if the URL variable is nil
absoluteString不是一个可选的值,它只是一个字符串。您可以检查URL变量是否为nil
if let url = yourURLVariable {
// do your textView function
} else {
// handle nil url
}