error after updating the Xcode to 7.0

不打扰是莪最后的温柔 提交于 2019-11-28 10:36:22

问题


I'm developing an iOS application in Swift. When I updated the Xcode to 7.0, I'm getting error in swiftyJSON.

 static func fromObject(object: AnyObject) -> JSONValue? {
    switch object {
    case let value as NSString:
        return JSONValue.JSONString(value as String)
    case let value as NSNumber:
        return JSONValue.JSONNumber(value)
    case let value as NSNull:
        return JSONValue.JSONNull
    case let value as NSDictionary:
        var jsonObject: [String:JSONValue] = [:]
        for (k:AnyObject, v:AnyObject) in value {// **THIS LINE- error: "Definition conflicts with previous value"**
            if let k = k as? NSString {
                if let v = JSONValue.fromObject(v) {
                    jsonObject[k] = v
                } else {
                    return nil
                }
            }
        }

What's the problem? Can you help, please?


回答1:


 for (k:AnyObject, v:AnyObject) in value { .. }

must be written in Swift 2 as

for (k, v) : (AnyObject, AnyObject) in value { .. }

From the Xcode 7 release notes:

Type annotations are no longer allowed in patterns and are considered part of the outlying declaration. This means that code previously written as:

var (a : Int, b : Float) = foo()

needs to be written as:

var (a,b) : (Int, Float) = foo()

if an explicit type annotation is needed. The former syntax was ambiguous with tuple element labels.

But in your case the explicit annotation is actually not needed at all:

for (k, v) in value { .. }

because NSDictionary.Generator is already defined as a generator returning (key: AnyObject, value: AnyObject) elements.



来源:https://stackoverflow.com/questions/32672279/error-after-updating-the-xcode-to-7-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!