我很难弄清楚如何将Swift 2中使用的Sequence协议转换为Swift 3的正确版本.我找不到任何关于Swift 2 Sequence协议变化的文档与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库来说相当短.它在下面:
//// Json.swift// Json//// Created by Dan Kogai on 7/15/14.// copyright (c) 2014 Dan Kogai. All rights reserved.//import Foundation/// initpublic 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 propertIEsextension 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 propertIEsextension 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:@R_301_6939@,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",userInfo:[ NSLocalizedDescriptionKey: "[\"\(key)\"] not found" ])) default: return JsON(NSError( domain:"JsONErrorDomain",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(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","s": return o.int64Value.description case "Q","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!")}在Swift 3中,generate()已重命名为makeIterator().更改函数名称应该可以解决问题. (请注意,其他名称也已更改,例如AnyGenerator→AnyIterator,但看起来已经在您的代码中处理过了.)
此更改已作为SE-0006: Apply API Guidelines to the Standard Library的一部分实施.
总结以上是内存溢出为你收集整理的快速序列协议的Swift 2到3迁移全部内容,希望文章能够帮你解决快速序列协议的Swift 2到3迁移所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)