Just updated to swift 2.0 and I am having an error.
The error I am getting is: 'array' is unavailable: please construct an Array from your lazy sequence: Array(...)
My code is:
if let credentialStorage = session.configuration.URLCredentialStorage {
let protectionSpace = NSURLProtectionSpace(
host: URL!.host!,
port: URL!.port?.integerValue ?? 0,
`protocol`: URL!.scheme,
realm: URL!.host!,
authenticationMethod: NSURLAuthenticationMethodHTTPBasic
)
// ERROR------------------------------------------------↓
if let credentials = credentialStorage.credentialsForProtectionSpace(protectionSpace)?.values.array {
// ERROR------------------------------------------------↑
for credential: NSURLCredential in (credentials) {
components.append("-u \(credential.user!):\(credential.password!)")
}
} else {
if let credential = delegate.credential {
components.append("-u \(credential.user!):\(credential.password!)")
}
}
}
Would anyone know how to convert this line of code to be updated for Swift 2.0?
if let credentials = credentialStorage.credentialsForProtectionSpace(protectionSpace)?.values.array
As the error states, you should construct Array. Try:
if let credentials = (credentialStorage?.credentialsForProtectionSpace(protectionSpace)?.values).map(Array.init) {
//...
}
In Swift1.2, values on Dictionary<Key, Value> returns LazyForwardCollection<MapCollectionView<[Key : Value], Value>> type that has .array property returning Array<Value>.
In Swift2, values on Dictionary<Key, Value> returns LazyMapCollection<[Key : Value], Value>, and the .array property is abandoned because we can construct Array with Array(dict.values).
In this case, since credentialStorage?.credentialsForProtectionSpace(protectionSpace)?.values end up with Optional type, we cannot simply Array(credentialStorage?.cre...). Instead, if you want a Array, we should use map() on Optional.
But, in this particular case, you can use credentialStorage?.credentialsForProtectionSpace(protectionSpace)?.values as is.
Try:
if let credentials = credentialStorage?.credentialsForProtectionSpace(protectionSpace)?.values {
for credential in credentials {
//...
}
}
This works because LazyMapCollection conforms to SequenceType.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With