【问题标题】:PFObject unable to be cast to custom subclassPFObject 无法转换为自定义子类
【发布时间】:2015-04-15 07:02:17
【问题描述】:

在最近更新 swift 编译器更新之前,此代码运行良好。我已经纠正了所有明显的错误,但是由于某种原因,当我现在运行应用程序时,它在尝试将 [AnyObject] 转换为我的自定义 Memory 类时在 for in loop 中崩溃。

日志和错误显示如下:

Has connectivity
Successfully retrieved 8 memories.
current memory: <Memories: 0x170120960, objectId: AO3EvPFob4, localId: (null)> {
ACL = "<PFACL: 0x174047e30>";
Date = "3915-04-08 05:00:00 +0000";
Guests = 4;
Title = test4;
}
Could not cast value of type 'PFObject' (0x100130bc0) to 'MemoryVault.Memory' (0x10012fa00).

所以我知道我正在从 Parse 检索 8 个正确的记忆,并且当前的记忆是正确的......它只是不会投射它。

代码:

    if (NetworkValidator.hasConnectivity()){ // Has internet connectivity
        println("Has connectivity")
        Memory.memoryQuery().findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                // The find succeeded.
                 println("Successfully retrieved \(objects!.count) memories.")

                Memory.unpinAllInBackground(self.memories, block: {
                    (success: Bool, error: NSError?) -> Void in

                    // Clear current list
                    self.memories = [Memory]()

                    // Do something with the found objects
                    for memory in objects! {
                        println("current memory: \(memory)")
                        let currentMemory: Memory = memory as! Memory

                        // Save to localDataStore
                        currentMemory.pinInBackground()
                        println("\(currentMemory.memoryTitle) from Parse")

                        // Append to current
                        self.memories.append(currentMemory)
                    }
                })

                // Notify tableView to reload data
                self.memoriesTableView.reloadData()
            } else {

                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo)")
            }

        }
    } 

【问题讨论】:

    标签: swift casting parse-platform


    【解决方案1】:

    这个让我忙了几个小时 - Parse-Documentation 提到:

    PFObject 的强类型子类必须符合 PFSubclassing 协议,并且必须在调用 [Parse setApplicationId:clientKey:] 之前调用。”

    通过在 AppDelegate 中调用 MyParseClass.registerSubclass(),我的问题得到了解决!

    【讨论】:

    • 哇,谢谢!我刚刚添加了一个新模型并花了几个小时试图解决这个问题。我完全忘记了我在 AppDelegate 中的函数,我必须在每个模型上调用 registerSubclass 以避免这个问题。我希望我们能尽快摆脱这种骇人听闻的解决方法!
    • 是否需要在子类的类定义中添加PFSubclassing
    猜你喜欢
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多