Data Modelling Framework for JSON in Swift. Data model classes and instances can be easily created.
Bool is support.
Construct a model instance with a dictionary;
Update a model with a dictionary;
Convert a model to a dictionary.
Currently, SwiftJSONModel should be integrated manually. Follow these steps:
- Download the SwiftJSONModel repository as a zip file or clone it;
- Copy the SwiftJSONModel sub-folder into your Xcode project, and add it to your project.
Consider a JSON string like:
{
"isOpen" : true,
"shopId" : 200010000,
"shopName" : "Wisdom Bookshop",
"shopAddress" : "No 7, Fortune Rd."
}
To model this JSON, you should:
- Create a new class which inherit the SwiftJSONModel class;
- Declare properties with the name of the JSON keys:
class Shop: SwiftJSONModel {
var isOpen: Bool = false
var shopId: Int = 0
var shopName: String?
var shopAddress: String?
}
Currently, SwiftJSONModel can convert these types automatically in Swift:
Bool
, Int
, Float
, Double
, String
/ NSString
, NSNumber
, SwiftJSONModel
or its subclass, and Array
/ NSArray
. Remember that Bool, Int, Float, and Double properties cannot be optional
.
If you want to use properties of other types,you should add these properties into ignored list. See Ignored properties in detail.
3. To construct a model instance from JSON data, you can use following constructor:
convenience init(dictionary: [String : AnyObject])
For example:
if let JSONData: NSData = /* obtained by Internet */ {
if let JSONDictionary = try? NSJSONSerialization.JSONObjectWithData(JSONData, options: .MutableContainers) as? [String : AnyObject] {
let shop = Shop(dictionary: JSONDictionary!)
if shop.shopId == 200010000 {
shop.shopName? = "New Wisdom Bookshop"
// ...
}
}
}
####Model cascading (models including other models)
{ "isOpen" : true, "shopId" : 200010000, "shopName" : "Wisdom Bookshop", "shopAddress" : "No 7, Fortune Rd." "shopOwner" : { "ownerId" : 30001001 "ownerName" : "Alax Hackathon" "mobilephone" : "800-888-7777" } } |
class BookShop: Shop { |
You can specify the property to be ignored when converting by one of these following ways:
- Composite a subclass of
SwiftJSONModelIgnored
which has the property to ignore:
class BookShop: Shop {
var shopLogo: ShopLogo?
}
class ShopLogo: SwiftJSONModelIgnored {
var image: UIImage?
}
- Override the method of
func ignoredProperties() -> [String]
to return an array of classname string:
class BookShop: Shop {
var shopLogo: UIImage?
override func ignoredProperties() -> [String] {
return super.ignoredProperties() + ["shopLogo"]
}
}
Remember that in an inheritance system, you should call super.ignoredProperties()
and merge superclass's ignore list.
You should specify the generic types of array collections by method of func genericTypes() -> [String : String]
. The valid generic type can be: Bool
, Int
, Float
, Double
, String
/ NSString
, NSNumber
, SwiftJSONModel
or its subclass.
{ "isOpen" : true, "shopId" : 200010000, "shopName" : "Wisdom Bookshop", "shopAddress" : "No 7, Fortune Rd." "books" : [ { "name" : "Data Mining", "price" : 825 }, { "name" : "Data Analysis", "price" : 750 } ] } |
class BookShop: Shop { |
Computed properties are supported in SwiftJSONModel. You can add computed properties for a class as usual, but remember to make them ignored
:
class Shop: SwiftJSONModel {
var isOpen: Bool = false
var shopId: Int = 0
var shopName: String?
var shopAddress: String?
var isOldShop: Bool {
return shopId > 0 && shopId < 100000000
}
override func ignoredProperties() -> [String] {
return super.ignoredProperties() + ["isOldShop"]
}
}
Author: Zhu Yue
This code is distributed under the terms and conditions of the MIT license.