无法从DynamoDB表

时间:2017-12-08 07:52:43

标签: ios swift amazon-web-services null amazon-dynamodb

我有一个用Swift编写的iOS应用程序。在这个应用程序中,我正在尝试从DynamoDB表中加载一个项目,但由于某种原因,我无法加载任何项目,即使所有细节都是正确的。

我按照所有说明操作后使用CocoaPods安装了AWS SDK,以便正确安装它。我安装了以下库:AWSCognitoAWSCognitoIdentityProviderAWSDynamoDBAWSS3AWSSNS

之后,我在Info.plist文件中添加了我的AWS凭证:

<key>AWS</key>
<dict>
    <key>DynamoDBObjectMapper</key>
    <dict>
        <key>Default</key>
        <dict>
            <key>Region</key>
            <string>USEast1</string>
        </dict>
    </dict>
    <key>DynamoDB</key>
    <dict>
        <key>Default</key>
        <dict>
            <key>Region</key>
            <string>USEast1</string>
        </dict>
    </dict>
    <key>CredentialsProvider</key>
    <dict>
        <key>CognitoIdentity</key>
        <dict>
            <key>Default</key>
            <dict>
                <key>Region</key>
                <string>USEast1</string>
                <key>PoolId</key>
                <string>myPoolId</string>
            </dict>
        </dict>
    </dict>
</dict>

我创建了一个名为Contact.swift的映射文件:

import UIKit
import AWSDynamoDB

class Contact: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    var id: String?
    var name: String?

    class func dynamoDBTableName() -> String {
        return "Contacts"
    }

    class func hashKeyAttribute() -> String {
        return "id"
    }
}

然后,我在ViewController.swift方法中的viewDidLoad文件中添加了以下代码。代码应该从表中获取一个项目,保存其属性,并将其name属性设置为屏幕的标题:

import UIKit
import AWSDynamoDB

class ViewController: UIViewController {

    //MARK: Properties
    struct GlobalVars {
        static let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
        static let id: String = "id"

        static var name: String?
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        GlobalVars.dynamoDBObjectMapper.load(Contact.self, hashKey: GlobalVars.id, rangeKey: nil).continueWith(block: {(task: AWSTask<AnyObject>!) -> Any? in
            if let error = task.error as NSError? {
                print("The request failed. Error: \(error)")
            } else if let taskResult = task.result as? Contact {
                // Save the information
                GlobalVars.name = taskResult.name

                // Set title to the name
                self.navigationItem.title = GlobalVars.name!
            }
            return nil
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

问题是,当我尝试将GlobalVars.name变量分配给导航栏标题时,应用程序崩溃了。它崩溃时显示以下消息:Fatal error: Unexpectedly found nil while unwrapping an Optional value

我尝试打印taskResult.name,但它只是打印nil

但它没有向控制台打印任何东西。我已将以下代码添加到Info.plist文件中,但这也不起作用:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>amazonaws.com</key>
        <dict>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
      </dict>
      <key>amazonaws.com.cn</key>
      <dict>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
      </dict>
  </dict>

当我正在检查编辑器底部的变量区域时,我可以看到所有项目的属性都是nil

经过深入搜索,我发现了一些非常奇怪的东西。当我将表的名称更改为不存在的表时,我在控制台中收到以下消息:

The request failed. Error: Error Domain=com.amazonaws.AWSDynamoDBErrorDomain Code=7 "(null)" UserInfo={__type=com.amazonaws.dynamodb.v20120810#ResourceNotFoundException, message=Requested resource not found}

当我使用正确的表名运行它,但是使用错误的哈希键引用表中不存在的项时,我什么也得不到。它不会崩溃或将消息打印到控制台。它只是继续运行,就像我不要求DynamoDB表中的任何内容。

我该怎么办?我已经在互联网上搜索过但我找不到任何关于它的信息......你能帮我解决一下吗?

1 个答案:

答案 0 :(得分:0)

显然,由于推理@objc的Swift 4行为改变,我应该在@objcMembers文件中的类声明之前添加Contact.swift