2016-10-01 96 views
0
class ViewController: UIViewController { 

@IBOutlet weak var NoSQLResultLabel: UILabel! 

var teste : [AWSDynamoDBObjectModel]? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    query { (response, error) in 
     if let erro = error { 
      self.NoSQLResultLabel.text = String(erro) 
      print("error: \(erro)") 
     } else if response?.items.count == 0 { 
      self.NoSQLResultLabel.text = String("0") 
      print("No items") 
     } else { 
      self.NoSQLResultLabel.text = String(response!.items) 
      print("success: \(response!.items)") 
     } 
    } 

} 

func query(completionHandler: (response: AWSDynamoDBPaginatedOutput?, error: NSError?) -> Void) { 
    let objectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper() 
    let queryExpression = AWSDynamoDBQueryExpression() 

    queryExpression.keyConditionExpression = "#userId = :userId" 
    queryExpression.expressionAttributeNames = ["#userId": "userId",] 
    queryExpression.expressionAttributeValues = [":userId": AWSIdentityManager.defaultIdentityManager().identityId!,] 

    objectMapper.query(UserLocations.self, expression: queryExpression, completionHandler: {(response: AWSDynamoDBPaginatedOutput?, error: NSError?) -> Void in 
     dispatch_async(dispatch_get_main_queue(), { 
      completionHandler(response: response, error: error) 
     }) 
    }) 
} 

} 

我創建了一個query函數,從數據庫中提取數據,然後在viewDidLoad中調用它。用swift提取AWS DynamoDB中的數據

當我運行模擬器它返回:

succeso: [<MySampleApp.UserLocations: 0x7fa25480f560> { 
    "_lastModifiedDate" = "2016-10-01 14:27:05 +0000"; 
    "_latitude" = "37.33233141"; 
    "_longitude" = "-122.0312186"; 
    "_userId" = "us-east-1:be2dfccb-caac-4b45-bd36-6fa8964e78d9"; 
    "_userLocationID" = "sendBox-userLocationID-500000"; 
}] 

現在我需要顯示的值分別

我嘗試這樣做:

print("sucesso: \(response!.items["_userId"])")

返回它的值_userId

但它表明我這個錯誤:

Cannot subscript a value of type [AWSDynamoDBObjectModel] with an index type String

誰能告訴我怎麼可以提取該數組的值?

回答

0

要訪問的項目和它們的屬性,你可以做以下

let userLocation = response?.items[0] as! UserLocations 
print("\(userLocation._userId!)")