所以如果string不是NilLiteralConvertible ......一些字符串函数会返回什么? [英] So if string is not NilLiteralConvertible... what do some string functions return?

查看:66
本文介绍了所以如果string不是NilLiteralConvertible ......一些字符串函数会返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设以下代码:

  let url =http://%20abc
let urlString = url.stringByRemovingPercentEncoding!
if urlString!= nil {
println(done)
}

stringByRemovingPercentEncoding 应该返回一个可选的String。所以让我们打开它。现在当它实际上失败并且没有返回字符串时会发生什么?



字符串不是NilLiteralConvertible,因此下一行有编译器错误。我真的很困惑 - 所以如果我假设 url.stringByRemovingPercentEncoding 是一个未包装的,我该如何比较 urlString 可选的?显然它不适用于nil。



请注意我可以将urlString保留为可选值然后打开它等等。这不是重点。关键是这个确切的情况。非常感谢!

解决方案


  let url =http ://%20abc
let urlString = url.stringByRemovingPercentEncoding!
if urlString!= nil {
println(done)
}




我得到的错误是在!= ,其中说:


二元运算符!= 不能应用于 String 和 nil


这是有道理的。为什么我们甚至希望 String nil 之间使用任何比较运算符。 字符串 不能 nil



现在, url.stringByRemovingPercentEncoding 的返回类型为 String?,但我们隐式使用unwrapped可选,这意味着 urlString 将是 String 并且有一个值,否则我们会致命错误(意外发现 nil 展开可选项。)



如果我们删除我们的隐含解包运算符:

  let url =http://%20abc
let urlString = url.stringByRemovingPercentEncoding
if urlString!= nil {
println(done)
}



<现在代码非常高兴。而不是 String ,我们的变量 urlString 现在是 String?。我们可以使用!= 将任何可选项与 nil 进行比较,因为选项可以是



但也许最常用的 Swifty 写法如下:

  let url =http://%20abc
if let urlString = url.stringByRemovingPercentEncoding {
//用urlString做一些事情
println(完成)
}

在这种情况下, urlString 类型为 String ,因此我们不必打开它,但是 if 块当且仅当 if 块中使用 urlString ) > url.stringByRemovingPercentEncoding 返回非零。






如果我们记录,我实际上不会对 urlString 做任何事情,我们有以下两个选项:

  if url.stringByRemovingPercentEncoding!= nil {
println(do ne)
}

还有:

 如果让_ = url.stringByRemovingPercentEncoding {
println(done)
}


Let's assume the following code:

let url = "http://%20abc"
let urlString = url.stringByRemovingPercentEncoding!
if urlString != nil {
    println("done")
}

stringByRemovingPercentEncoding should return an optional String. So let's unwrap it. Now what happens when it actually 'fails' and doesn't return a string?

String is not a NilLiteralConvertible, therefore there is a compiler error on the next line. I am really confused here - so what should I compare the urlString with if I assume that url.stringByRemovingPercentEncoding is an unwrapped optional? Obviously it is not working with nil.

Please note I could leave the urlString as an optional value and then unwrap it, etc. That's not the point. The point is this exact case. Thanks a lot!

解决方案

let url = "http://%20abc"
let urlString = url.stringByRemovingPercentEncoding!
if urlString != nil {
    println("done")
}

The error I get there is on the !=, where it says:

Binary operator != cannot be applied to operands of the type String and nil

Which makes sense. Why would we even want to use any comparison operator between String and nil. A String cannot be nil.

Now, url.stringByRemovingPercentEncoding has a return type of String?, but we're using the implicitly unwrapped optional, which means that urlString will either be a String and have a value, or we'll get a fatal error (unexpectedly found nil unwrapping an optional).

If we remove our implicit unwrap operator:

let url = "http://%20abc"
let urlString = url.stringByRemovingPercentEncoding
if urlString != nil {
    println("done")
}

Now the code is perfectly happy. Instead of being a String, our variable, urlString is now a String?. And we can use != to compare any optional with nil because optionals can be nil!

But perhaps the most Swifty way of writing this looks like this:

let url = "http://%20abc"
if let urlString = url.stringByRemovingPercentEncoding {
    // do something with urlString
    println("done")
}

In this scenario, urlString is of type String, so we don't have to unwrap it, but the if block only enters (and we can only use the urlString within the if block) if and only if url.stringByRemovingPercentEncoding returns a non-nil.


And for the record, if we're not actually going to do anything with urlString, we have the following two options:

if url.stringByRemovingPercentEncoding != nil {
    println("done")
}

and also:

if let _ = url.stringByRemovingPercentEncoding {
    println("done")
}

这篇关于所以如果string不是NilLiteralConvertible ......一些字符串函数会返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆