StrongLoop Loopback example in Swift

限于喜欢 提交于 2019-12-09 06:22:08

问题


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()
    } 

回答1:


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)
}


来源:https://stackoverflow.com/questions/24268398/strongloop-loopback-example-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!