rwb977
rwb977

Reputation: 9

New To Swift, Having Trouble Converting swift 2 to swift 5 manually

I used to mess around a little when I was younger and I built an app that I'm currently trying to fix and then improve upon but I'm having a few issues converting swift 2 to swift 5 and the app won't compile

Issue 1:

Cannot convert value of type (NSError) -> () to expected argument type ((Error) -> Void)?

referring to these two lines of code:

}) { (error:NSError) in
        print(error.localizedDescription)

Issue 2:

Value of type Any has no subscripts

Referring to these lines:

key = snapshot.key
itemRef = snapshot.ref

if let shareContent = snapshot.value!["content"] as? [[String:Any]] {
    content = shareContent
    
}
else{
    content = ""
}
if let shareUser = snapshot.value!["addedByUser"] as? [[String:Any]] {
    addedByUser = shareUser
}else{
    content = ""

Issue 3:

Type of expression is ambiguous without more context

FIRAuth.auth()?.signInWithEmail("", password: "", completion: { (user:FIRUser?, error:NSError?) in
            if error == nil {
                print(user?.email)

If anyone can help with ANY of these I would really really appreciate this

Upvotes: 0

Views: 94

Answers (1)

vadian
vadian

Reputation: 285072

Issue 1) and 3): Don't annotate the type, in Swift 3+ the errors have become a type conforming to Error

}) { error in
    print(error.localizedDescription)

regarding 3) look for the proper type in the Firebase documentation, it's not (FIRUser?, NSError?) anymore.

Issue 2): In Swift 3+ the compiler must know the static type of any subscripted object. If value is expected to be a dictionary you have to conditionally downcast it

if let sharedValue = snapshot.value as? [String:Any],
    let shareContent = sharedValue["content"] as? [[String:Any]] {
    content = shareContent
    
}

Upvotes: 3

Related Questions