Nicholas Muir
Nicholas Muir

Reputation: 3104

Expected Declaration when converting from Objective C to Swift

I am trying to table scan in dynamoDb written in Swift using this guide:

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

But it is written mostly in objective C. I converted it to Swift but when I do I am getting a "expected declaration error".

This is the Obj C code (This works):

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 translation (Has a expected declaration error Line 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
})

Thanks for your help

Upvotes: 2

Views: 145

Answers (1)

GoZoner
GoZoner

Reputation: 70145

You are compiling this code in a file. You can't have assignments as top-level statements in a file.

Try something like (using idiomatic 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
    }
  }
}

or implement a Swift script file with:

#! /bin/env swift

// Swift statements

[EOF]

Upvotes: 1

Related Questions