无法从iOS上的DynamoDB检索项目

时间:2017-05-01 12:55:56

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

我有一个用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指南将项目保存到数据库中,并且它有效,所以问题在于我如何尝试接收项目。

保存项目= 工作
检索项目= 无效

你能帮我解决一下吗?

2 个答案:

答案 0 :(得分:1)

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

var strings: [String]?

而不是像字符串集:

var strings: Set<String>? 

答案 1 :(得分: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加载(读取)项目时它不起作用。找不到任何有用的资源。如果您找到任何解决方案,请告诉我们。感谢。