Lots of errors when trying to use AWS DynamoDB with Swift 3

穿精又带淫゛_ 提交于 2019-12-24 00:43:39

问题


I'm new to swift development and am trying to incorporate a backend. I figured AWS would be a good way to accomplish what I want to get done. I'm currently just trying to get the sample project file that they create for you working, and it has so many errors it's incredible. What I've realized is that AWS creates the files in Swift 2, and so running them in Swift 3 is quite hard.

I converted the code to Swift 3 when I opened it in xCode and have made maybe 30 changes to lines after that, just trying to get rid of all the errors which I could. And now I'm stuck. There are certain lines where I just don't know what to do in order to fix it. I'll list a few below, but if anyone has any tips on the best way to go about this or can help me fix the errors below, I would greatly appreciate it.

1) The issue here is with model.classForCoder.responds(to: #selector(AWSDynamoDBModeling.rangeKeyAttribute)). The error says "Cannot call value of non function type ((Selector -> Bool)!"

func produceOrderedAttributeKeys(_ model: AWSDynamoDBObjectModel) -> [String] {
    let keysArray = Array(model.dictionaryValue.keys)
    var keys = keysArray as! [String]
    keys = keys.sorted()

    if (model.classForCoder.responds(to: #selector(AWSDynamoDBModeling.rangeKeyAttribute))) {
        let rangeKeyAttribute = model.classForCoder.rangeKeyAttribute!()
        let index = keys.index(of: rangeKeyAttribute)
        if let index = index {
            keys.remove(at: index)
            keys.insert(rangeKeyAttribute, at: 0)
        }
    }

2) "== Produces Bool not the expected contextual result type 'NSNumber'"

class func randomSampleBOOL() -> NSNumber {
    // If random number is even number then return true, false for odd numbers
    return self.randomNumber() % 2 == 0
}

3) The issue is with the loadNextPage(completionHandler) line. "Argument passed to call that takes no arguments"

func loadMoreResults() {
    loading = true
    paginatedOutput?.loadNextPage(completionHandler: {(error: NSError?) -> Void in
        if error != nil {
            print("Failed to load more results: \(error)")
            DispatchQueue.main.async(execute: {
                self.showAlertWithTitle("Error", message: "Failed to load more more results: \(error?.localizedDescription)")
            })
        }
        else {
            DispatchQueue.main.async(execute: {
                self.results!.append(contentsOf: self.paginatedOutput!.items)
                self.tableView.reloadData()
                self.loading = false
            })
        }
    })
}

4) The issue is with the objectMapper.load. Cannot convert value of type '(AWSDynamoDBObjectModel?, NSError?) -> Void' to expected argument type '((AWSDynamoDBObjectModel?, Error?) -> Void)?'

 func getItemWithCompletionHandler(_ completionHandler: @escaping (_ response: AWSDynamoDBObjectModel?, _ error: NSError?) -> Void) {
    let objectMapper = AWSDynamoDBObjectMapper.default()
    objectMapper.load(Orders.self, hashKey: "demo-email-3", rangeKey: 1111500000, completionHandler: {(response: AWSDynamoDBObjectModel?, error: NSError?) -> Void in
        DispatchQueue.main.async(execute: {
            completionHandler(response, error)
        })
    })
}

5) Cannot invoke 'scan' with an argument list of type '(Orders.Type, expression: AWSDynamoDBScanExpression, (AWSDynamoDBPaginatedOutput?, NSError?) -> ())'

let objectMapper = AWSDynamoDBObjectMapper.default()
    let scanExpression = AWSDynamoDBScanExpression()
    scanExpression.filterExpression = "begins_with(#email, :email)"
    scanExpression.expressionAttributeNames = ["#email": "email"]
    scanExpression.expressionAttributeValues = [":email": "demo-"]

    objectMapper.scan(Orders.self, expression: scanExpression) { (response: AWSDynamoDBPaginatedOutput?, error: NSError?) in

6) Finally there are like 15 of these Instance method 'getItemWithCompletionHandler' nearly matches optional requirement 'getItemWithCompletionHandler' of protocol 'Table' with various different method names and such. These are just warnings, but I'm given the recommendation to either make it private or add @nonobjc, and I don't know which I should do, if either.


回答1:


Update for future Googlers: I managed to solve the first one by changing it to:

if let rangeKeyAttribute = model.classForCoder.rangeKeyAttribute?() {
        let index = keys.index(of: rangeKeyAttribute)
        if let index = index {
            keys.remove(at: index)
            keys.insert(rangeKeyAttribute, at: 0)
        }
    }

Number 2 I was confused about because the comment says "return true, false for odd numbers" but the function returns an NSNumber. I changed it to return Bool and that fixed the error, but that seems like a weird mistake they would have made. I feel like that would cause errors in Swift 2 as well. The rest I haven't figured out either...



来源:https://stackoverflow.com/questions/41402926/lots-of-errors-when-trying-to-use-aws-dynamodb-with-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!