I still haven't figured out how to make this work in Swift. Here's some 
more complete examples that show how I've tried to do it








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"

  }

}

That declares the model object. Here is an example that will look up the 
objects and try to access the properties. When it tries to access the 
property named itemArray, it causes a fatal error.









  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 Monday, September 15, 2014 4:43:44 PM UTC-4, Julian Paas wrote:
>
> I have a CBLModel with an array of objects that implement CBLJSONEncoding, 
> but every time I try to access the array I get
>
> "fatal error: NSArray element failed to match the Swift Array Element type"
>
> I have tried to implement a class method to return the class for this 
> array as follows, but it never gets called. Have I implemented the method 
> incorrectly, or is there something else I am missing?
>
>   @NSManaged var gamePlayerStats: [PlayerStats]
>
>   class func gamePlayerStatsItemClass() -> PlayerStats.Type {
>     return PlayerStats.self
>   }
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mobile-couchbase/f97d8521-d104-4b30-95d2-4226156609c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to