从目标C转换为Swift时需要声明



我正在尝试使用以下指南在Swift中编写的dynamoDb中进行表扫描:

http://docs.aws.amazon.com/mobile/sdkforios/developerguide/dynamodb_om.html

但它主要是用目标C编写的。我将它转换为Swift,但当我这样做时,我会得到一个"预期的声明错误"。

这是Obj C代码(有效):

AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper]; 
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
scanExpression.limit = @10;
[[dynamoDBObjectMapper scan:[Book class]
                 expression:scanExpression]
 continueWithBlock:^id(AWSTask *task) {
     if (task.error) {
         NSLog(@"The request failed. Error: [%@]", task.error);
     }
     if (task.exception) {
         NSLog(@"The request failed. Exception: [%@]", task.exception);
     }
     if (task.result) {
         AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
         for (Book *book in paginatedOutput.items) {
             //Do something with book.
         }
     }
     return nil;
 }];

Swift翻译(具有预期的声明错误第3行:scanExpression.limit=10;):

var dynamoDBObjectMapper: AWSDynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
var scanExpression: AWSDynamoDBScanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 10
dynamoDBObjectMapper.scan(Book.self, expression: scanExpression).continueWithBlock({(task: AWSTask) -> id in
    if task.error {
        print("The request failed. Error: [(task.error)]")
    }
    if task.exception {
        print("The request failed. Exception: [(task.exception)]")
    }
    if task.result {
        var paginatedOutput: AWSDynamoDBPaginatedOutput = task.result
        for book: Book in paginatedOutput.items {
            //Do something with book.
            //Do something with book.
        }
    }
    return nil
})

感谢您的帮助

您正在一个文件中编译此代码。不能将赋值作为文件中的顶级语句。

试试(使用惯用的Swift):

class Foo {
  var dynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
  var scanExpression = AWSDynamoDBScanExpression()
  func doScan () {
    scanExpression.limit = 10
    dynamoDBObjectMapper.scan(Book.self, expression: scanExpression).continueWithBlock {
      (task: AWSTask) -> id in
      if task.error {
        print("The request failed. Error: [(task.error)]")
      }
      if task.exception {
        print("The request failed. Exception: [(task.exception)]")
      }
      if task.result {
        var paginatedOutput: AWSDynamoDBPaginatedOutput = task.result
        for book: Book in paginatedOutput.items {
            //Do something with book.
            //Do something with book.
        }
      }
    return nil
    }
  }
}

或者用实现Swift脚本文件

#! /bin/env swift
// Swift statements
[EOF]

相关内容

  • 没有找到相关文章

最新更新