将 Xcode 更新为 7.0 后出错
error after updating the Xcode to 7.0
我正在 Swift 开发一个 iOS 应用程序。
当我将 Xcode 更新到 7.0 时,我在 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
}
}
}
有什么问题吗?你能帮忙吗?
for (k:AnyObject, v:AnyObject) in value { .. }
必须写成Swift2如
for (k, v) : (AnyObject, AnyObject) in value { .. }
来自 Xcode 7 发行说明:
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.
但在您的情况下,实际上根本不需要显式注释:
for (k, v) in value { .. }
因为NSDictionary.Generator
已经定义为生成器
返回 (key: AnyObject, value: AnyObject)
个元素。
我正在 Swift 开发一个 iOS 应用程序。 当我将 Xcode 更新到 7.0 时,我在 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
}
}
}
有什么问题吗?你能帮忙吗?
for (k:AnyObject, v:AnyObject) in value { .. }
必须写成Swift2如
for (k, v) : (AnyObject, AnyObject) in value { .. }
来自 Xcode 7 发行说明:
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.
但在您的情况下,实际上根本不需要显式注释:
for (k, v) in value { .. }
因为NSDictionary.Generator
已经定义为生成器
返回 (key: AnyObject, value: AnyObject)
个元素。