overriding CBLModel designated initializer in Swift

203 views
Skip to first unread message

Julian Paas

unread,
Sep 15, 2014, 4:37:37 PM9/15/14
to mobile-c...@googlegroups.com
Does anyone know how to override initWithDocument on CBLModel subclasses? I have tried as:

init(document: CBLDocument!) {

    super.init(document)
  }  


But it tells me "use of undeclared type CBLDocument". CBLDocument is definitely not unknown I use it in plenty of other swift classes. It only seems to be in my CBLModel subclass that it is unknown. I get the same result if I declare


  var x: CBLDocument?



Robbie Bubble

unread,
Sep 21, 2014, 7:18:45 AM9/21/14
to mobile-c...@googlegroups.com
Did you included CBLDocument.h in your bridging header?

Julian Paas

unread,
Sep 23, 2014, 9:57:05 PM9/23/14
to mobile-c...@googlegroups.com
I'm doing this in my bridging header which includes all of the CBL headers. But to be sure I did actually try explicitly adding CBLDocument.h but it didn't help.
#import <CouchbaseLite/CouchbaseLite.h>


Karel-Jan Van Haute

unread,
Oct 14, 2014, 9:28:52 AM10/14/14
to mobile-c...@googlegroups.com
I'm having the same problem. Did you found the reason why you get the error?

Julian Paas

unread,
Oct 14, 2014, 9:38:42 AM10/14/14
to mobile-c...@googlegroups.com
No I never figured it out. I worked around it by creating a factory method on my class for creating new instances that initializes the object the way I wanted to. Kinda lame, but it works.

Karel-Jan Van Haute

unread,
Oct 15, 2014, 8:26:45 AM10/15/14
to mobile-c...@googlegroups.com
Can You share the method?

Julian Paas

unread,
Oct 15, 2014, 9:13:13 AM10/15/14
to mobile-c...@googlegroups.com
It looks something like...

  class func newInstance(team: Team!) -> Game
   
// Use the designated initializer
    let game = Game(newDocumentInDatabase: team.database)

   
// Do whatever inititalization you want to do here. In my case I needed to do some initialization based on the team argument

   
// If you want to return a saved instance, you can save it first
   
var
error: NSError?
    game
.save(&error)
    if (error != nil) {
      DDLog.logError("Error creating game for \(team): \(error!)")
     
return nil
   
} else {
      DDLog.logInfo("Created game: \(game)"
    }

 return game

}





On Wednesday, October 15, 2014 8:26:45 AM UTC-4, Karel-Jan Van Haute wrote:
Can You share the method?

On Tuesday, October 14, 2014 3:38:42 PM UTC+2, Julian Paas wrote:
No I never figured it out. I worked around it by creating a factory method on my class for creating new instances that initializes the object the way I wanted to. Kinda lame, but it works.

On Tuesday, October 14, 2014 9:28:52 AM UTC-4, Karel-Jan Van Haute wrote:It lo

James Nocentini

unread,
Nov 4, 2014, 4:45:29 PM11/4/14
to mobile-c...@googlegroups.com
I'm not sure what the problem is but declaring a typealias and using that in the CBLModel subclass doesn't throw the compiler error.

typealias CBLDoc = CBLDocument

class Profile: CBLModel {
   
override init!(document: CBLDoc) {
       
super.init(document: document)
   
}
}

Jens Alfke

unread,
Nov 4, 2014, 4:52:59 PM11/4/14
to mobile-c...@googlegroups.com
Pasin figured it out (it's a weird naming issue in a protocol method) and checked in a fix yesterday.
It's a small patch and probably easy to cherry-pick onto any recent release.

—Jens
Reply all
Reply to author
Forward
0 new messages