Swift中的StrongLoop Loopback示例 [英] StrongLoop Loopback example in Swift
本文介绍了Swift中的StrongLoop Loopback示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Swift中实现示例LoopBack iOS应用程序
I'm trying to implement the example LoopBack iOS app in Swift
我有一些从ObjectiveC转换的麻烦
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];
};
这是我的版本
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)
}
但是在调用allWithSuccess时出现编译器错误:
but I get a compiler error on the call to allWithSuccess:
无法将表达式'Void'转换为'LBModelAllSuccessBlock'字样。
Cannot convert the expressions type 'Void' to type 'LBModelAllSuccessBlock!'
我缺少什么?
更新:
如果我按如下方式声明成功块,则可行:
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!!!!
如果有人在寻找Swift的最新版本和LoopBack iOS SDK,它对我有用:
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)
}
这篇关于Swift中的StrongLoop Loopback示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文