我正在尝试使用DynamoDB表,并已成功连接到它,但无法读取它。当我尝试时,我收到以下错误:
因未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因是:'_ datePublished不是ProjectName.Article的属性。'
这是我的文章类/模型:
class Article: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
var _articleSource: String?
var _articleUrl: String?
var _datePublished: String?
var _headline: String?
var _imageURL: String?
var _rating: String?
var _listingId: String?
class func dynamoDBTableName() -> String {
return "tableName"
}
class func hashKeyAttribute() -> String {
return "_listingId"
}
class func rangeKeyAttribute() -> String {
return "_articleUrl"
}
override class func jsonKeyPathsByPropertyKey() -> [AnyHashable: Any] {
return [
"_articleSource" : "articleSource",
"_articleUrl" : "articleUrl",
"_datePublished" : "datePublished",
"_headline" : "headline",
"_imageURL" : "imageURL",
"_rating" : "rating",
"_listingId" : "listingId",
]
}
}
我在ViewDidLoad
中调用的函数来读取DB中的文章:
func readArticle() {
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
// Create data object
let article: Article = Article();
dynamoDbObjectMapper.load(
Article.self,
hashKey: "2018-03-17T08:50:30+00:00",
rangeKey: "https://www.example.com",
completionHandler: {
(objectModel: AWSDynamoDBObjectModel?, error: Error?) -> Void in
if let error = error {
print("Amazon DynamoDB Read Error: \(error)")
return
}
print("Article:\n \(article)")
})
}
我做错了什么? datePublished存在于DB上的该条目中,并在模型中定义
答案 0 :(得分:1)
检查datePublished是否是数据库中的String或Date类型。如果类型不匹配,那么您将出错。
答案 1 :(得分:0)
事实证明,AWS SDK并不完全兼容Swift 4。在swift 4中,你必须在对象模型类定义之前放置@objcMembers,如:
@objcMembers class Article: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
}