OGeek|极客世界-中国程序员成长平台

标题: ios - DynamoDB 保存到数据库 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:47
标题: ios - DynamoDB 保存到数据库

我是 AWS 的新手,我正在尝试使用带有 News 表的 aws 示例将数据保存到我的数据库中。

我将此功能连接到主 Storyboard 按钮:

 @IBAction func addButton(_ sender: Any) {

    let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()

    //Create data object using data models you downloaded from Mobile Hub
    let newsItem: News = News();

    // Use AWSIdentityManager.default().identityId here to get the user identity id.
    newsItem._userId = "us-east-1:74c8f7ce-244b-4476-963e-0dcb3216f406"
    newsItem._articleId = "0123"
    newsItem._title = "Banana"
    newsItem._author = "Logan"
    newsItem._content = "Should I stay or should I go now?"
    newsItem._category = "Food"


    //Save a new item
    dynamoDbObjectMapper.save(newsItem, completionHandler: {
        (error: Error?) -> Void in

        if let error = error {
            print("Amazon DynamoDB Save Error: \(error)")
            return
        }
        print("An item was saved.")
    })

} 

但是当我按下按钮时,我得到: mazon DynamoDB 保存错误:Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)"UserInfo={__type=com.amazon.coral.validate#ValidationException, message=Supplied AttributeValue 为空,必须只包含一个支持的数据类型}

我的新闻字段是:

       override class func jsonKeyPathsByPropertyKey() -> [AnyHashable: Any] {
    return [
           "_userId" : "userId",
           "_articleId" : "articleId",
           "_author" : "author",
           "_category" : "category",
           "_content" : "content",
           "_title" : "title",
    ]
}



Best Answer-推荐答案


我遇到了同样的问题,例如,我解决了在 News() 的怪异变量中添加 @objc 的问题

class News: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
    @objc var id: String?
    @objc var type: String?
    @objc var cc: String?
}

如果您添加 @objc 强制包装到 NS 对象,这是 aws mobile sdk 的错误...

关于ios - DynamoDB 保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46776381/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4