2017-10-16 165 views
1

我是AWS的新手,我試圖使用新聞表的aws示例將數據保存到我的數據庫。DynamoDB保存到數據庫

我連這個功能主要故事板按鈕:

@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 Save Error: Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" UserInfo={__type=com.amazon.coral.validate#ValidationException, message=Supplied AttributeValue is empty, must contain exactly one of the supported datatypes}

我的新聞領域是:

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

哪些相關dynamoDB表必填字段? –

回答

1

我同樣的問題,並且我解決了在News()中eery變量中添加@objc,例如

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

如果添加@objc迫使換到NS對象,這是AWS的移動SDK中的錯誤...

+0

這真的幫了我。 Upvoting。謝謝! – Macondo2Seattle