这个函数/变量是如何工作的?

时间:2021-05-07 16:48:07

I'm using the following code to retrieve NSURLCredentials:

我使用以下代码检索NSURLCredentials:

let credentials: NSURLCredential? = {
    let loginProtectionSpace = NSURLProtectionSpace(host: host, port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
    let credentials = NSURLCredentialStorage.sharedCredentialStorage().defaultCredentialForProtectionSpace(loginProtectionSpace)
    return credentials
}()

This is called when the user opens the app. The credentials returned are nil. Then I set these credentials and try to print credentials out again and it's still nil. However, if I restart the app, the printed credentials are there. What's going on here?

当用户打开应用程序时,就会调用它。返回的凭证是nil。然后我设置这些凭证并尝试再次打印凭证,它仍然是nil。但是,如果我重新启动应用程序,打印的凭证就在那里。这是怎么回事?

2 个解决方案

#1


1  

This is a lazy variable. The code gets executed once when you first access the property. After that the initially returned value is "remembered" and returned on future calls.

这是一个懒惰变量。当您第一次访问属性时,代码将执行一次。之后,最初返回的值将被“记住”并在以后的调用中返回。

If you set the credentials yourself in the NSURLCredentialStorage then on the next start of the app the first access of the property once again executes the code and retrieves the stored credentials. Note that during the run where you first set the credentials up the actual 3 lines of code retrieving the credentials from the storage are not executed a second time and therefore during that run of the app the property still is nil while there actually is a value in the storage. A similar thing will happen if you modify the existing credentials - during the run where you change them, the credentials will still hold a reference to the previous ones.

如果您自己在NSURLCredentialStorage中设置了凭证,那么在应用程序的下一个启动时,该属性的第一次访问将再次执行代码并检索存储的凭据。注意,在运行实际,首先设置凭证3行代码从存储检索凭证不执行一次,因此在运行的应用程序属性仍然是零虽然实际上是存储一个值。如果您修改了现有的凭据,也会发生类似的情况——在您修改它们的运行期间,凭据仍然保留对以前凭据的引用。

If you want to be able to requery the store, you should either

如果你想要对商店有要求,你也应该这样做。

  • create a function for this instead of a lazy variable
  • 为此创建一个函数,而不是惰性变量
  • or take a look at this answer: Re-initialize a lazy initialized variable in Swift
  • 或者看看这个答案:用Swift重新初始化一个延迟初始化的变量

#2


0  

I suspect it's because credentials is immutable. The code block gets executed once to assign credentials its value. Which is why it has the credentials after you restarted the app.

我怀疑这是因为凭证是不可变的。代码块被执行一次,以分配凭证的值。这就是为什么在你重新启动应用程序后它会有凭证。

#1


1  

This is a lazy variable. The code gets executed once when you first access the property. After that the initially returned value is "remembered" and returned on future calls.

这是一个懒惰变量。当您第一次访问属性时,代码将执行一次。之后,最初返回的值将被“记住”并在以后的调用中返回。

If you set the credentials yourself in the NSURLCredentialStorage then on the next start of the app the first access of the property once again executes the code and retrieves the stored credentials. Note that during the run where you first set the credentials up the actual 3 lines of code retrieving the credentials from the storage are not executed a second time and therefore during that run of the app the property still is nil while there actually is a value in the storage. A similar thing will happen if you modify the existing credentials - during the run where you change them, the credentials will still hold a reference to the previous ones.

如果您自己在NSURLCredentialStorage中设置了凭证,那么在应用程序的下一个启动时,该属性的第一次访问将再次执行代码并检索存储的凭据。注意,在运行实际,首先设置凭证3行代码从存储检索凭证不执行一次,因此在运行的应用程序属性仍然是零虽然实际上是存储一个值。如果您修改了现有的凭据,也会发生类似的情况——在您修改它们的运行期间,凭据仍然保留对以前凭据的引用。

If you want to be able to requery the store, you should either

如果你想要对商店有要求,你也应该这样做。

  • create a function for this instead of a lazy variable
  • 为此创建一个函数,而不是惰性变量
  • or take a look at this answer: Re-initialize a lazy initialized variable in Swift
  • 或者看看这个答案:用Swift重新初始化一个延迟初始化的变量

#2


0  

I suspect it's because credentials is immutable. The code block gets executed once to assign credentials its value. Which is why it has the credentials after you restarted the app.

我怀疑这是因为凭证是不可变的。代码块被执行一次,以分配凭证的值。这就是为什么在你重新启动应用程序后它会有凭证。