Swift序列协议的Swift 2到3迁移

Swift序列协议的Swift 2到3迁移,swift,swift2,swift3,ios10,swift-protocols,Swift,Swift2,Swift3,Ios10,Swift Protocols,我正在尝试将此库()中的以下代码转换为与Swift 3兼容的代码 我很难弄清楚如何将Swift 2中使用的序列协议转换为Swift 3的正确版本。我找不到任何关于Swift 2序列协议与Swift 3序列协议更改的文档 下面是我目前已尽可能多地转换为Swift 3的代码 extension JSON : Sequence { public func generate()->AnyIterator<(AnyObject,JSON)> { switch _v

我正在尝试将此库()中的以下代码转换为与Swift 3兼容的代码

我很难弄清楚如何将Swift 2中使用的序列协议转换为Swift 3的正确版本。我找不到任何关于Swift 2序列协议与Swift 3序列协议更改的文档

下面是我目前已尽可能多地转换为Swift 3的代码

extension JSON : Sequence {
    public func generate()->AnyIterator<(AnyObject,JSON)> {
        switch _value {
        case let o as NSArray:
            var i = -1
            return AnyIterator {
                i=i+1
                if i == o.count { return nil }
                return (i as AnyObject, JSON(o[i]))
            }
        case let o as NSDictionary:
            var ks = Array(o.allKeys.reversed())
            return AnyIterator {
                if ks.isEmpty { return nil }
                if let k = ks.removeLast() as? String {
                    return (k as AnyObject, JSON(o.value(forKey: k)!))
                } else {
                    return nil
                }
            }
        default:
            return AnyIterator{ nil }
        }
    }
    public func mutableCopyOfTheObject() -> AnyObject {
        return _value.mutableCopy as AnyObject
    }
}
扩展JSON:Sequence{ public func generate()->AnyIterator{ 开关_值{ 作为NSArray的案例: 变量i=-1 返回任意迭代器{ i=i+1 如果i==o.count{返回nil} 返回(i作为AnyObject,JSON(o[i])) } case let o作为NSDictionary: var ks=Array(o.allKeys.reversed()) 返回任意迭代器{ 如果ks.isEmpty{return nil} 如果让k=ks.removeLast()作为?字符串{ 返回(k作为AnyObject,JSON(o.value(forKey:k)!) }否则{ 归零 } } 违约: 返回任意迭代器{nil} } } public func mutableCopyOfObject()->AnyObject{ 返回_value.mutableCopy作为AnyObject } } 我在细节中得到的错误在所附的图片中

如果您想使用它,整个代码对于JSON库来说相当简短。以下是:

//
//  json.swift
//  json
//
//  Created by Dan Kogai on 7/15/14.
//  Copyright (c) 2014 Dan Kogai. All rights reserved.
//
import Foundation
/// init
public class JSON {
    public let _value:AnyObject
    /// unwraps the JSON object
    public class func unwrap(obj:AnyObject) -> AnyObject {
        switch obj {
        case let json as JSON:
            return json._value
        case let ary as NSArray:
            var ret = [AnyObject]()
            for v in ary {
                ret.append(unwrap(obj: v as AnyObject))
            }
            return ret as AnyObject
        case let dict as NSDictionary:
            var ret = [String:AnyObject]()
            for (ko, v) in dict {
                if let k = ko as? String {
                    ret[k] = unwrap(obj: v as AnyObject)
                }
            }
            return ret as AnyObject
        default:
            return obj
        }
    }
    /// pass the object that was returned from
    /// NSJSONSerialization
    public init(_ obj:Any) { self._value = JSON.unwrap(obj: obj as AnyObject) }
    /// pass the JSON object for another instance
    public init(_ json:JSON){ self._value = json._value }
}
/// class properties
extension JSON {
    public typealias NSNull = Foundation.NSNull
    public typealias NSError = Foundation.NSError
    public class var null:NSNull { return NSNull() }
    /// constructs JSON object from data
    public convenience init(data:NSData) {
        var err:NSError?
        var obj:Any?
        do {
            obj = try JSONSerialization.jsonObject(
                with: data as Data, options:[])
        } catch let error as NSError {
            err = error
            obj = nil
        }
        self.init(err != nil ? err! : obj!)
    }
    /// constructs JSON object from string
    public convenience init(string:String) {
        let enc:String.Encoding = String.Encoding.utf8
        self.init(data: string.data(using: enc)! as NSData)
    }
    /// parses string to the JSON object
    /// same as JSON(string:String)
    public class func parse(string:String)->JSON {
        return JSON(string:string)
    }
    /// constructs JSON object from the content of NSURL
    public convenience init(nsurl:NSURL) {
        var enc:String.Encoding = String.Encoding.utf8
        do {
            let str = try NSString(contentsOf:nsurl as URL, usedEncoding:&enc.rawValue)
            self.init(string:str as String)
        } catch let err as NSError {
            self.init(err)
        }
    }
    /// fetch the JSON string from NSURL and parse it
    /// same as JSON(nsurl:NSURL)
    public class func fromNSURL(nsurl:NSURL) -> JSON {
        return JSON(nsurl:nsurl)
    }
    /// constructs JSON object from the content of URL
    public convenience init(url:String) {
        if let nsurl = NSURL(string:url) as NSURL? {
            self.init(nsurl:nsurl)
        } else {
            self.init(NSError(
                domain:"JSONErrorDomain",
                code:400,
                userInfo:[NSLocalizedDescriptionKey: "malformed URL"]
                )
            )
        }
    }
    /// fetch the JSON string from URL in the string
    public class func fromURL(url:String) -> JSON {
        return JSON(url:url)
    }
    /// does what JSON.stringify in ES5 does.
    /// when the 2nd argument is set to true it pretty prints
    public class func stringify(obj:AnyObject, pretty:Bool=false) -> String! {
        if !JSONSerialization.isValidJSONObject(obj) {
            let error = JSON(NSError(
                domain:"JSONErrorDomain",
                code:422,
                userInfo:[NSLocalizedDescriptionKey: "not an JSON object"]
                ))
            return JSON(error).toString(pretty: pretty)
        }
        return JSON(obj).toString(pretty: pretty)
    }
}
/// instance properties
extension JSON {
    /// access the element like array
    public subscript(idx:Int) -> JSON {
        switch _value {
        case _ as NSError:
            return self
        case let ary as NSArray:
            if 0 <= idx && idx < ary.count {
                return JSON(ary[idx])
            }
            return JSON(NSError(
                domain:"JSONErrorDomain", code:404, userInfo:[
                    NSLocalizedDescriptionKey:
                    "[\(idx)] is out of range"
                ]))
        default:
            return JSON(NSError(
                domain:"JSONErrorDomain", code:500, userInfo:[
                    NSLocalizedDescriptionKey: "not an array"
                ]))
        }
    }
    /// access the element like dictionary
    public subscript(key:String)->JSON {
        switch _value {
        case _ as NSError:
            return self
        case let dic as NSDictionary:
            if let val:Any = dic[key] { return JSON(val) }
            return JSON(NSError(
                domain:"JSONErrorDomain", code:404, userInfo:[
                    NSLocalizedDescriptionKey:
                    "[\"\(key)\"] not found"
                ]))
        default:
            return JSON(NSError(
                domain:"JSONErrorDomain", code:500, userInfo:[
                    NSLocalizedDescriptionKey: "not an object"
                ]))
        }
    }
    /// access json data object
    public var data:AnyObject? {
        return self.isError ? nil : self._value
    }
    /// Gives the type name as string.
    /// e.g.  if it returns "Double"
    ///       .asDouble returns Double
    public var type:String {
        switch _value {
        case is NSError:        return "NSError"
        case is NSNull:         return "NSNull"
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":              return "Bool"
            case "q", "l", "i", "s":    return "Int"
            case "Q", "L", "I", "S":    return "UInt"
            default:                    return "Double"
            }
        case is NSString:               return "String"
        case is NSArray:                return "Array"
        case is NSDictionary:           return "Dictionary"
        default:                        return "NSError"
        }
    }
    /// check if self is NSError
    public var isError:      Bool { return _value is NSError }
    /// check if self is NSNull
    public var isNull:       Bool { return _value is NSNull }
    /// check if self is Bool
    public var isBool:       Bool { return type == "Bool" }
    /// check if self is Int
    public var isInt:        Bool { return type == "Int" }
    /// check if self is UInt
    public var isUInt:       Bool { return type == "UInt" }
    /// check if self is Double
    public var isDouble:     Bool { return type == "Double" }
    /// check if self is any type of number
    public var isNumber:     Bool {
        if let o = _value as? NSNumber {
            let t = String(cString:o.objCType)
            return  t != "c" && t != "C"
        }
        return false
    }
    /// check if self is String
    public var isString:     Bool { return _value is NSString }
    /// check if self is Array
    public var isArray:      Bool { return _value is NSArray }
    /// check if self is Dictionary
    public var isDictionary: Bool { return _value is NSDictionary }
    /// check if self is a valid leaf node.
    public var isLeaf:       Bool {
        return !(isArray || isDictionary || isError)
    }
    /// gives NSError if it holds the error. nil otherwise
    public var asError:NSError? {
        return _value as? NSError
    }
    /// gives NSNull if self holds it. nil otherwise
    public var asNull:NSNull? {
        return _value is NSNull ? JSON.null : nil
    }
    /// gives Bool if self holds it. nil otherwise
    public var asBool:Bool? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":  return Bool(o.boolValue)
            default:
                return nil
            }
        default: return nil
        }
    }
    /// gives Int if self holds it. nil otherwise
    public var asInt:Int? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return nil
            default:
                return Int(o.int64Value)
            }
        default: return nil
        }
    }
    /// gives Int32 if self holds it. nil otherwise
    public var asInt32:Int32? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return nil
            default:
                return Int32(o.int64Value)
            }
        default: return nil
        }
    }
    /// gives Int64 if self holds it. nil otherwise
    public var asInt64:Int64? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return nil
            default:
                return Int64(o.int64Value)
            }
        default: return nil
        }
    }
    /// gives Float if self holds it. nil otherwise
    public var asFloat:Float? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return nil
            default:
                return Float(o.floatValue)
            }
        default: return nil
        }
    }
    /// gives Double if self holds it. nil otherwise
    public var asDouble:Double? {
        switch _value {
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return nil
            default:
                return Double(o.doubleValue)
            }
        default: return nil
        }
    }
    // an alias to asDouble
    public var asNumber:Double? { return asDouble }
    /// gives String if self holds it. nil otherwise
    public var asString:String? {
        switch _value {
        case let o as NSString:
            return o as String
        default: return nil
        }
    }
    /// if self holds NSArray, gives a [JSON]
    /// with elements therein. nil otherwise
    public var asArray:[JSON]? {
        switch _value {
        case let o as NSArray:
            var result = [JSON]()
            for v:Any in o { result.append(JSON(v)) }
            return result
        default:
            return nil
        }
    }
    /// if self holds NSDictionary, gives a [String:JSON]
    /// with elements therein. nil otherwise
    public var asDictionary:[String:JSON]? {
        switch _value {
        case let o as NSDictionary:
            var result = [String:JSON]()
            for (ko, v): (Any, Any) in o {
                if let k = ko as? String {
                    result[k] = JSON(v)
                }
            }
            return result
        default: return nil
        }
    }
    /// Yields date from string
    public var asDate:NSDate? {
        if let dateString = _value as? String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
            return dateFormatter.date(from: dateString) as NSDate?
        }
        return nil
    }
    /// gives the number of elements if an array or a dictionary.
    /// you can use this to check if you can iterate.
    public var count:Int {
        switch _value {
        case let o as NSArray:      return o.count
        case let o as NSDictionary: return o.count
        default: return 0
        }
    }
    public var length:Int { return self.count }
    // gives all values content in JSON object.
    public var allValues:JSON{
        if(self._value.allValues == nil) {
            return JSON([])
        }
        return JSON(self._value.allValues)
    }
    // gives all keys content in JSON object.
    public var allKeys:JSON{
        if(self._value.allKeys == nil) {
            return JSON([])
        }
        return JSON(self._value.allKeys)
    }
}
extension JSON : Sequence {
    public func generate()->AnyIterator<(AnyObject,JSON)> {
        switch _value {
        case let o as NSArray:
            var i = -1
            return AnyIterator {
                i=i+1
                if i == o.count { return nil }
                return (i as AnyObject, JSON(o[i]))
            }
        case let o as NSDictionary:
            var ks = Array(o.allKeys.reversed())
            return AnyIterator {
                if ks.isEmpty { return nil }
                if let k = ks.removeLast() as? String {
                    return (k as AnyObject, JSON(o.value(forKey: k)!))
                } else {
                    return nil
                }
            }
        default:
            return AnyIterator{ nil }
        }
    }
    public func mutableCopyOfTheObject() -> AnyObject {
        return _value.mutableCopy as AnyObject
    }
}

extension JSON : CustomStringConvertible {
    /// stringifies self.
    /// if pretty:true it pretty prints
    public func toString(pretty:Bool=false)->String {
        switch _value {
        case is NSError: return "\(_value)"
        case is NSNull: return "null"
        case let o as NSNumber:
            switch String(cString:o.objCType) {
            case "c", "C":
                return o.boolValue.description
            case "q", "l", "i", "s":
                return o.int64Value.description
            case "Q", "L", "I", "S":
                return o.uint64Value.description
            default:
                switch o.doubleValue {
                case 0.0/0.0:   return "0.0/0.0"    // NaN
                case -1.0/0.0:  return "-1.0/0.0"   // -infinity
                case +1.0/0.0:  return "+1.0/0.0"   //  infinity
                default:
                    return o.doubleValue.description
                }
            }
        case let o as NSString:
            return o.debugDescription
        default:
            let opts = pretty ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization.WritingOptions()
            if let data = (try? JSONSerialization.data(
                withJSONObject: _value, options:opts)) as NSData? {
                    if let result = NSString(
                        data:data as Data, encoding:String.Encoding.utf8.rawValue
                        ) as? String {
                            return result
                    }
            }
            return "YOU ARE NOT SUPPOSED TO SEE THIS!"
        }
    }
    public var description:String { return toString() }
}

extension JSON : Equatable {}
public func ==(lhs:JSON, rhs:JSON)->Bool {
    // print("lhs:\(lhs), rhs:\(rhs)")
    if lhs.isError || rhs.isError { return false }
    else if lhs.isLeaf {
        if lhs.isNull   { return lhs.asNull   == rhs.asNull }
        if lhs.isBool   { return lhs.asBool   == rhs.asBool }
        if lhs.isNumber { return lhs.asNumber == rhs.asNumber }
        if lhs.isString { return lhs.asString == rhs.asString }
    }
    else if lhs.isArray {
        for i in 0..<lhs.count {
            if lhs[i] != rhs[i] { return false }
        }
        return true
    }
    else if lhs.isDictionary {
        for (k, v) in lhs.asDictionary! {
            if v != rhs[k] { return false }
        }
        return true
    }
    fatalError("JSON == JSON failed!")
}
//
//json.swift
//json
//
//由Dan Kogai于2014年7月15日创作。
//版权所有(c)2014 Dan Kogai。版权所有。
//
进口基金会
///初始化
公共类JSON{
公共let_值:AnyObject
///打开JSON对象
公共类函数展开(obj:AnyObject)->AnyObject{
开关obj{
case let json作为json:
返回json.\u值
作为NSArray的案例:
var ret=[AnyObject]()
对于v,在ary中{
ret.append(展开(对象:v作为任意对象))
}
将ret作为AnyObject返回
case let dict as NSDictionary:
var ret=[String:AnyObject]()
对于dict中的(ko,v){
如果设k=ko为?字符串{
ret[k]=展开(对象:v作为任意对象)
}
}
将ret作为AnyObject返回
违约:
返回obj
}
}
///传递从返回的对象
///NSJ串行化
public init(j:Any){self._value=JSON.unwrap(obj:obj as AnyObject)}
///将JSON对象传递给另一个实例
public init(json:json){self._value=json._value}
}
///类属性
扩展JSON{
公共类型
公共类型NSRROR =基础
公共类var null:NSNull{return NSNull()}
///从数据构造JSON对象
公共便利初始化(数据:NSData){
变量错误:n错误?
有吗?
做{
obj=尝试JSONSerialization.jsonObject(
使用:数据作为数据,选项:[])
}将let错误捕获为NSError{
错误
obj=零
}
self.init(err!=nil?err!:obj!)
}
///从字符串构造JSON对象
公共便利初始化(string:string){
让enc:String.Encoding=String.Encoding.utf8
self.init(数据:string.data(使用:enc)!作为NSData)
}
///将字符串解析为JSON对象
///与JSON相同(string:string)
公共类func parse(string:string)->JSON{
返回JSON(string:string)
}
///从NSURL的内容构造JSON对象
公共便利初始化(nsurl:nsurl){
变量enc:String.Encoding=String.Encoding.utf8
做{
让str=try NSString(contentsOf:nsurl作为URL,usedEncoding:&enc.rawValue)
self.init(字符串:str作为字符串)
}以讹传讹{
self.init(err)
}
}
///从NSURL获取JSON字符串并对其进行解析
///与JSON相同(nsurl:nsurl)
公共类func fromNSURL(nsurl:nsurl)->JSON{
返回JSON(nsurl:nsurl)
}
///从URL的内容构造JSON对象
公共便利初始化(url:String){
如果让nsurl=nsurl(字符串:url)作为nsurl{
self.init(nsurl:nsurl)
}否则{
self.init(n错误(
域:“JSONErrorDomain”,
代码:400,
用户信息:[NSLocalizedDescriptionKey:“格式错误的URL”]
)
)
}
}
///从字符串中的URL获取JSON字符串
公共类func fromURL(url:String)->JSON{
返回JSON(url:url)
}
///做ES5中JSON.stringify的工作。
///当第二个参数设置为true时,它会打印出来
公共类func stringify(obj:AnyObject,pretty:Bool=false)->String{
if!JSONSerialization.isValidJSONObject(obj){
let error=JSON(n错误(
域:“JSONErrorDomain”,
代码:422,
userInfo:[NSLocalizedDescriptionKey:“不是JSON对象”]
))
返回JSON(error).toString(pretty:pretty)
}
返回JSON(obj).toString(pretty:pretty)
}
}
///实例属性
扩展JSON{
///访问类似元素的数组
公共下标(idx:Int)->JSON{
开关_值{
案例作为错误:
回归自我
作为NSArray的案例:
如果0 JSON{
开关_值{
案例作为错误:
回归自我
案例让dic作为NSDictionary:
如果让val:Any=dic[key]{返回JSON(val)}
返回JSON(n错误(
域:“JSONErrorDomain”,代码:404,用户信息:[
NSLocalizedDescriptionKey:
“[\”\(键)\“]未找到”