@NSManaged var gamePlayerStats: [PlayerStats]
class func gamePlayerStatsItemClass() -> PlayerStats.Type { return PlayerStats.self }
class PlayerStats : NSObject, CBLJSONEncoding { required init(JSON jsonObject: AnyObject!) {
... }
func encodeAsJSON() -> AnyObject! {
.... }
}
On Sep 16, 2014, at 7:04 AM, Julian Paas <julia...@osaaru.com> wrote:Have there been any thoughts/discussions about a native Swift API?
class Team: CBLModel {
@NSManaged var name: String
@NSManaged var type: String
class func teamsByName() -> CBLQuery {
let query = databaseMain.viewNamed(kCblViewTeamsByName).createQuery()
return query
}
// Because class variables are not supported yet.
class func typeName() -> String {
return "Team"
}
override func willSave(changedPropertyNames: NSSet!) {
self.setValue(Team.typeName(), ofProperty: "type")
}
}
import Foundation
class ArrayItem : NSObject, CBLJSONEncoding {
let holding: Int
init(holding: Int) {
self.holding = holding
}
required init(JSON jsonObject: AnyObject!) {
self.holding = 0
}
func encodeAsJSON() -> AnyObject! {
return ["holding": self.holding]
}
}
class TestModel : CBLModel {
@NSManaged var itemArray: [ArrayItem]
@NSManaged var simpleArray: [Int]
class func all() -> CBLQuery {
let query = databaseMain.viewNamed("allTestModels").createQuery()
return query
}
class func itemArrayItemClass() -> AnyClass {
return ArrayItem.self
}
convenience init() {
self.init(newDocumentInDatabase: databaseMain)
itemArray = [ArrayItem]()
simpleArray = [Int]()
type = "TestModel"
}
}
func runModelTests() {
var error: NSError?
let queryEnumerator = TestModel.all().run(&error)
assert(error == nil, "Unable to query all TestModels")
while let row = queryEnumerator.nextRow() {
let testModel = TestModel(forDocument: row.document) as TestModel
println("properties: \(testModel.document.properties)")
println("simpleArray: \(testModel.simpleArray)")
// Here's where we access the array of objects and get...
// fatal error: NSArray element failed to match the Swift Array Element type
println("itemArray: \(testModel.itemArray)")
testModel.deleteDocument(&error)
assert(error == nil, "Unable to delete TestModel")
}
let testModel = TestModel()
testModel.simpleArray.append(0)
testModel.simpleArray.append(1)
testModel.itemArray.append(ArrayItem(holding: 10))
testModel.itemArray.append(ArrayItem(holding: 11))
testModel.save(&error)
assert(error == nil, "Unable to create test model")
println("Created TestModel: \(testModel.document.properties)")
}
On Sep 15, 2015, at 3:30 PM, adam wilson <adam.el...@gmail.com> wrote:
Has there been any progress on this? I'm in the process of building an iOS CBLite app with Swift and hit this issue with CBLModel and @dynamic properties...