简体   繁体   English

斯威夫特:表达类型不明确,没有更多背景?

[英]Swift: Type of Expression is ambiguous without more context?

After I query the Parse database I get an error with the following code: 查询Parse数据库后,出现以下代码错误:

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. 如果我将Int32替换为String,则可以正常工作。但是,我的解析数据库中的Zipcode是数字而不是字符串。 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. 澄清:您无法在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. 当你施放到它的工作原理的原因String ,但休息时你投来Int32似乎是usersZipCodeString类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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