尝试在Swift 3中使用AWS DynamoDB时出现很多错误 [英] Lots of errors when trying to use AWS DynamoDB with Swift 3

查看:114
本文介绍了尝试在Swift 3中使用AWS DynamoDB时出现很多错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是快速发展的新手,并且正在尝试整合后端。我认为AWS是完成我想完成的事情的好方法。目前,我只是想获取他们为您创建的示例项目文件,该文件有很多错误,令人难以置信。我已经意识到,AWS在Swift 2中创建了文件,因此在Swift 3中运行它们非常困难。

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.

我将代码转换为Swift 3在xCode中打开它,之后可能对行进行了30次更改,只是试图摆脱所有可能的错误。现在我被困住了。在某些行中,我只是不知道该如何解决。我将在下面列出一些内容,但是如果有人对解决此问题的最佳方法有任何建议,或者可以帮助我修复以下错误,则将不胜感激。

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)此处的问题是 model.classForCoder.responds(to:#selector(AWSDynamoDBModeling.rangeKeyAttribute))。错误显示无法调用非函数类型的值((Selector-> Bool)!

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) ==产生布尔值而不是预期的上下文结果类型'NSNumber'

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

3)问题出在 loadNextPage(completionHandler)行。 传递给不带参数的调用的参数

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)问题出在 objectMapper.load 上。 无法将类型'((AWSDynamoDBObjectModel ?, NSError?)-> Void'的值转换为期望的参数类型'(((AWSDynamoDBObjectModel ?, Error?)-> Void)?'

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)无法使用类型为'(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)最后,这些实例方法中的15种类似'getItemWithCompletionHandler'几乎与协议'Table'的可选要求'getItemWithCompletionHandler'匹配以及各种不同的方法名称。这些只是警告,但我建议将其设为私有或添加 @nonobjc ,但我不知道该怎么做。 / p>

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.

推荐答案

针对将来的Google员工的更新:
我设法将第一个更改为:

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)
        }
    }

数字2我很困惑,因为注释说返回真,对于奇数为假,但是函数返回一个NSNumber。我将其更改为返回Bool并修复了错误,但这似乎是他们会犯的一个奇怪的错误。我觉得那也会导致Swift 2中的错误。其余的我也都没想通...

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...

这篇关于尝试在Swift 3中使用AWS DynamoDB时出现很多错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆