在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:isair/JSONHelper开源软件地址:https://github.com/isair/JSONHelper开源编程语言:Swift 88.1%开源软件介绍:JSONHelperConvert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of! Latest version requires iOS 8+ and Xcode 7.3+ Table of Contents
InstallationCocoaPodsAdd the following line in your
CarthageAdd the following line to your Cartfile.
Then do The <-- OperatorThe If the right hand side value is nil or the conversion fails, and the left hand side variable is an optional, then nil is assigned to it. When the left hand side is non-optional, the current value of the left hand side variable is left untouched. Using this specification let's assume you have a dictionary response that you retrieved from some API with hex color strings in it, under the key var colors = [UIColor.blackColor(), UIColor.whiteColor()]
// Assume we have response { "colors": ["#aaa", "#b06200aa"] }
colors <-- response[colorsKey] Convertible ProtocolIf your type is a simple value-like type, adopting the Convertible protocol is the way to make your type work with the Example: struct Vector2D: Convertible {
var x: Double = 0
var y: Double = 0
init(x: Double, y: Double) {
self.x = x
self.y = y
}
static func convertFromValue<T>(value: T?) throws -> Self? {
guard let value = value else { return nil }
if let doubleTupleValue = value as? (Double, Double) {
return self.init(x: doubleTupleValue.0, y: doubleTupleValue.1)
}
throw ConversionError.UnsupportedType
}
} var myVector: Vector2D?
myVector <-- (1.0, 2.7) Deserializable ProtocolWhile you can basically adopt the Example: class User: Deserializable {
static let idKey = "id"
static let emailKey = "email"
static let nameKey = "name"
static let avatarURLKey = "avatar_url"
private(set) var id: String?
private(set) var email: String?
private(set) var name = "Guest"
private(set) var avatarURL = NSURL(string: "https://mysite.com/assets/default-avatar.png")
required init(dictionary: [String : AnyObject]) {
id <-- dictionary[User.idKey]
email <-- dictionary[User.emailKey]
name <-- dictionary[User.nameKey]
avatarURL <-- dictionary[User.avatarURLKey]
}
} var myUser: User?
user <-- apiResponse["user"] Serializable Protocol// Serialization is coming soon. I'll probably not add a new protocol and just rename and update the Deserializable protocol and turn it into a mixin. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论