CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Sep 2016 |
| SPMSupports SPM | ✗ |
Maintained by Suyeol Jeon.
Model framework for Swift.
Let’s assume that we use GitHub Issues API. It would be looked like this:
@objc enum GHState: Int, StringEnum {
case Open
case Closed
var rawValues: [Int: String] {
return [
GHState.Open.rawValue: "open",
GHState.Closed.rawValue: "closed",
]
}
}
class GHIssue: SwiftyModel {
var id: Int!
var URL: NSURL!
var HTMLURL: NSURL!
var number: Int!
var state: GHState = .Closed // enum must have a default value
var title: String!
var body: String!
var user: GHUser!
var labels: [GHLabel]?
var milestoneTitle: String?
override class func keyPathForKeys() -> [String: String]? {
return [
"URL": "url",
"HTMLURL": "html_url",
"milestoneTitle": "milestone.title",
]
}
override class func dateFormatterForKey(key: String) -> NSDateFormatter? {
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return dateFormatter
}
}Use CocoaPods. Minimum required version of CocoaPods is 0.36, which supports Swift frameworks.
Podfile
pod 'SwiftyModel'I recommend you to try CocoaSeeds, which uses source code instead of dynamic framework.
Seedfile
github 'SwiftyModel/SwiftyModel', '0.1.0', :files => 'SwiftyModel/SwiftyModel.swift'let issue = GHIssue(JSONDictionary)let issues = GHIssue.fromArray(JSONArray) as! [GHIssue]{
"id": 123,
"author": {
"id": 456,
"name": "Suyeol Jeon",
"nickname": "devxoul"
}
}class Post: SwiftyModel {
var id: Int!
var authorID: Int! // will be `456`
var authorNickname: String? // will be `devxoul`
override class func keyPathForKeys() -> [String: String]? {
return [
"authorID": "author.id",
"authorNickname": "author.nickname",
]
} let post = Post(JSONDictionary)
post.toDictionary() // will return JSON dictionarySwiftyModel fully supports Optional. No more NSNumber, and no more initialized properties.
class HealthData: SwiftyModel {
var birthyear: Int?
var weight: Float?
}
let health = HelathData(...)
if let weight = health.weight {
...
}Note: SwiftyModel isn’t compatible with Objective-C if you’re using primitive type optionals. (e.g.
Int?,Float!,Bool?) Because those are not converted to Objective-C.
SwiftyModel supports enums with limitation of Swift. There are two kinds of enums: SuperEnum and StringEnum. SuperEnum described integer enums and StringEnum describes string enums.
@objc attribute.Int with SuperEnum or StringEnum protocol.StringEnum must implement rawValues() function.Example of SuperEnum
@objc enum Direction: Int, SuperEnum {
case Up
case Down
case Left
case Right
}Example of StringEnum
@objc enum GHState: Int, StringEnum {
case Open
case Closed
var rawValues: [Int: String] {
return [
GHState.Open.rawValue: "open",
GHState.Closed.rawValue: "closed",
]
}
}You can define replationships between models.
class Post: SwiftyModel {
var author: User!
var comments: [Comment]?
}SwiftyModel is under MIT license. See the LICENSE file for more info.