'array' is unavailable: please construct an Array from your lazy sequence: Array(...) error
Asked Answered
P

2

5

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

Phenomenal answered 5/9, 2015 at 1:40 Comment(4)
What version is your xcode?Got
It works in my playground.Got
@Got this is version 7 beta 6Phenomenal
@Phenomenal trying to change values.array to values.elementsGlyn
K
9

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.

Koffler answered 5/9, 2015 at 3:10 Comment(1)
(dict.keys).map(Array.init) did not work for me (I ended up with Element inference errors) but [MyType](dict.keys) worked (found here: https://mcmap.net/q/807811/-39-array-39-is-unavailable-please-construct-an-array-from-your-lazy-sequence-array)Ens
M
0

Use the initializer in Swift 2.0

guard let values = credentialStorage?.credentialsForProtectionSpace(protectionSpace)?.values else { return }
let credentials = Array<NSURLCredential>(values)
for credential in credentials {
    // `credential` will be a non-optional of type `NSURLCredential`
}
Mavilia answered 5/7, 2016 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.