无法从iOS上的DynamoDB检索项目

问题描述 投票:0回答:2

我有一个用Swift编写的iOS应用程序。 我正在尝试检索DynamoDB表中特定项的属性,但我得到一个错误。

AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Initialize the Amazon Cognito credentials provider
    let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1, identityPoolId:"identityPoolId")
    let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    // Override point for customization after application launch.
    return true
}

ViewController.swift:

import AWSDynamoDB

class ViewController: UIViewController {

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

        let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()

        dynamoDBObjectMapper.load(Book.self, hashKey: "7", rangeKey: nil).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask!) -> AnyObject! in
            if let error = task.error as NSError? {
                print("Error: \(error)")
            } else if let tableRow = task.result as? Book {
                self.navigationItem.title = tableRow.name
            }

            return nil
        })
    }
}

ViewController.swift我试图从表中获取该书的名称,并将其分配给导航栏标题。

Book.swift:

import AWSDynamoDB

class Book: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    var id: String?
    var name: String?

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

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

当我运行应用程序时,我在控制台上收到错误消息,来自if语句中的ViewController.swift:

Error: Error Domain=AWSMTLModelErrorDomain Code=1 "-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x618000244f50" UserInfo={AWSMTLModelThrownException=-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x618000244f50, NSLocalizedDescription=-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x618000244f50, NSLocalizedFailureReason=-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x618000244f50}

我试图通过使用应用程序添加项目来检查此问题是否与我的应用程序中的AWS配置有关。 我遵循AWS指南关于将项目保存到数据库,并且它有效,所以问题在于我如何尝试接收项目。

保存项目=工作 检索项目=不起作用

能帮我解决一下吗?

ios swift amazon-web-services amazon-dynamodb
2个回答
1
投票

显然,这是因为我设置了一个字符串集属性,如字符串数组:

var strings: [String]?

而不是像字符串集:

var strings: Set<String>? 

0
投票

您可能希望尝试这样:

import AWSDynamoDB

class Book: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

   @objc var id: String?
    var name: String?

   @objc class func dynamoDBTableName() -> String {
        return "Books"
    }

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

我和你有同样的问题。将项目保存到DynamoDB效果很好,但在从DynamoDB加载(读取)项目时它不起作用。找不到任何有用的资源。如果您找到任何解决方案,请告诉我们。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.