简体   繁体   中英

Swift: Type of Expression is ambiguous without more context?

After I query the Parse database I get an error with the following code:

if error == nil {
            // The find succeeded.
            print("Successfully retrieved \(objects!.count) zip codes.", terminator: "")
            // Do something with the found objects
            if let zipCodes = objects! as? [PFObject] {
                if zipCodes.contains({ $0["Zipcode"] as? Int32 == usersZipCode}) { **<-----THIS IS WHERE THE ERROR IS**
                    print("your in!") // transition to the new screen
                    self.performSegueWithIdentifier("beginSignUp", sender: self)
                }
                else {
                    self.messageLabelNotAvail.text = "Unfortunately, Patch is not available in your area or you have not typed in a correct US Zip Code."
                }
            }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)", terminator: "")
        }
    }
}

If i replace Int32 to String, it works fine.. But my Zipcode in my parse database is a Number not a String. What am I doing wrong?

Instead of:

if zipCodes.contains({ $0["Zipcode"] as? Int32 == usersZipCode}) { 
   //Rest of Code
}

Try:

if let target = Int32(usersZipCode) 
   where zipCodes.contains({ $0["Zipcode"] as? Int32 == target}) { 
     //Rest of Code
}

Clarification: You can't compare things of different types in Swift. The reason it works when you cast to String but breaks when you cast to Int32 seems to be that usersZipCode is of String type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM