[英]Extracting data in AWS DynamoDB with swift
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中将其调用。
当我运行模拟器时,它返回以下内容:
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
内的_userId
但是它显示了这个错误:
Cannot subscript a value of type [AWSDynamoDBObjectModel] with an index type String
谁能告诉我如何提取此数组的值?
要访问项目及其属性,您可以执行以下操作
let userLocation = response?.items[0] as! UserLocations
print("\(userLocation._userId!)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.