2014-06-17 15 views
5

Tôi đang cố gắng để thực hiện các ví dụ ứng dụng loopback iOS trong SwiftStrongLoop Loopback dụ trong Swift

Create a LoopBack iOS app: part one

và tôi đang gặp một số rắc rối dịch từ 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]; 
}; 

Dưới đây là của tôi phiên bản

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

nhưng tôi gặp lỗi trình biên dịch khi gọi đến llWithSuccess:

Không thể chuyển đổi loại biểu thức 'Void' thành 'LBModelAllSuccessBlock!'

Tôi đang thiếu gì?

UPDATE:

Nếu Tôi tuyên bố khối thành công như sau, nó hoạt động:

var successBlock = { 
     (models: AnyObject[]!) ->() in 
     self.tableData = models 
     self.booksTable.reloadData() 
    } 
+0

thử định nghĩa của bạn 'var's như các chức năng với 'từ khóa func' như:' func errorBlock() {} ' – Jack

+0

bạn có làm việc này không? – ilan

+0

Bạn có thể muốn kiểm tra ví dụ nhanh này mà tôi đã tạo: https://github.com/kgoedecke/loopback-swift-user-example –

Trả lời

1

Cảm ơn cho câu trả lời !!!!

Nếu bất cứ ai đang tìm kiếm phiên bản mới nhất của Swift và loopback iOS SDK, nó làm việc cho tôi như thế này:

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) 
} 
Các vấn đề liên quan