I'm trying to implement the example LoopBack iOS app in Swift
Create a LoopBack iOS app: part one
and I'm having some trouble translating from the ObjectiveC
- (void) getBooks
{
    //Error Block
    void (^loadErrorBlock)(NSError *) = ^(NSError *error){
        NSLog(@"Error on load %@", error.description);
    };
    void (^loadSuccessBlock)(NSArray *) = ^(NSArray *models){
        NSLog(@"Success count %d", models.count);
        self.tableData = models;
        [self.myTable reloadData];
    };
    //This line gets the Loopback model "book" through the adapter defined in AppDelegate
    LBModelRepository *allbooks = [[booksAppDelegate adapter] repositoryWithModelName:prototypeName];
    //Logic - Get all books. If connection fails, load the error block, if it passes, call the success block and pass allbooks to it.
    [allbooks allWithSuccess:loadSuccessBlock  failure:loadErrorBlock];
};
Here's my version
func getBooks() {
    var errorBlock = {
        (error: NSError!) -> Void in
        NSLog("Error on load %@", error.description)
    }
    var successBlock = {
        (models: NSArray!) -> Void in
        NSLog("Success count %d", models.count)
        self.tableData = models
        self.booksTable.reloadData()
    }
    // get the "book" model
    var allBooks: LBModelRepository = adapter.repositoryWithModelName(prototypeName)
    // get all books
    allBooks.allWithSuccess(successBlock, errorBlock)
}
but I get a compiler error on the call to allWithSuccess:
Cannot convert the expressions type 'Void' to type 'LBModelAllSuccessBlock!'
What am I missing?
UPDATE:
If I declare the success block as follows, it works:
    var successBlock = {
        (models: AnyObject[]!) -> () in
        self.tableData = models
        self.booksTable.reloadData()
    } 
                Thanks for the answer!!!!
If anyone is looking for the last version of Swift and LoopBack iOS SDK, it worked for me like this:
func getBooks() {
    // Error Block
    let errorBlock = {
        (error: NSError!) -> Void in
        NSLog("Error on load %@", error.description)
    }
    // Success Block
    let successBlock = {
        (models: [AnyObject]!) -> () in
        self.tableData = models
        self.myTable.reloadData()
    }
    // This line gets the Loopback model "book" through the adapter defined in AppDelegate
    let allBooks:LBPersistedModelRepository = AppDelegate.adapter.repositoryWithModelName(prototypeName, persisted: true) as! LBPersistedModelRepository
    // Logic - Get all books. If connection fails, load the error block, if it passes, call the success block and pass allbooks to it.
    allBooks.allWithSuccess(successBlock, failure: errorBlock)
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With