AWS DynamoDB Swift – Inserting Values

To insert/save items in a DynamoDB using swift. (I will be using the example based here, translated to use swift) You can find the source files here: Github

Here is what my table looks like:


You first have to define a Mapping Class. Basically this is where you define what values from the table you need to insert values to:

//class that gets the objects from db
class DDBTableRow :AWSDynamoDBObjectModel ,AWSDynamoDBModeling  {
    var ISBN:String?
    var Title:String?
    var Author:String?
    class func dynamoDBTableName() -> String! {
        return "Books"
// if we define attribute it must be included when calling it in function testing...
    class func hashKeyAttribute() -> String! {
        return "ISBN"
    class func ignoreAttributes() -> Array<AnyObject>! {
        return nil
    //MARK: NSObjectProtocol hack
    //Fixes Does not conform to the NSObjectProtocol error
    override func isEqual(object: AnyObject?) -> Bool {
        return super.isEqual(object)
    override func `self`() -> Self {
        return self

Once you have the class implementation, then you can begin saving values. Here I created a function called insertValues. I created a few textfield outlets to allow user input and also included the static values commented out.

func insertValues(){
        //static values 
        //    let newBook = DDBTableRow.self()
        //    newBook.ISBN = "3333"
        //    newBook.Author = "Ray Jay"
        //    newBook.Title = "Less go"
        //values from a text field
        let newBook = DDBTableRow.self()      
        newBook.ISBN = self.isbnField.text
        newBook.Title = self.titleField.text
        newBook.Author = self.authorField.text
        //saving it
        let insertValues = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
        .continueWithBlock({ (task: AWSTask!) -> AnyObject! in
            if ((task.error) != nil) {
                print("Error: \(task.error)")
            if ((task.result) != nil){
                NSLog("Something happened")
            return nil

If you get this error message:

session task failed with error: Error Domain=NSURLErrorDomain Code=-1200 “An SSL error has occurred and a secure connection to the server cannot be made.” UserInfo={_kCFStreamErrorCodeKey=-9824, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x7fbcb38103c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 “(null)” UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9824, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9824}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=, NSErrorFailingURLStringKey=, _kCFStreamErrorDomainKey=3}

Add the following to info.plist:


Adding this disables App transport security that was implemented in iOS9. See more info here.

Leave a Reply

Your email address will not be published. Required fields are marked *