swift - PFObject 无法转换为自定义子类

标签 swift casting parse-platform

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

日志和报错如下:

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

        }
    } 

最佳答案

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

Strongly-typed subclasses of PFObject must conform to the PFSubclassing protocol and must call before [Parse setApplicationId:clientKey:] is called."

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

关于swift - PFObject 无法转换为自定义子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643612/

相关文章:

ios - 滚动时 UISwitch 在 Collection View 单元格中复制/奇怪的覆盖

ios - Alamofire "installation"不工作(嵌入式二进制文件中的红色文本)

c - 数组的平均值仅在转换为 float 时才能正确显示

ios - 使 PFQuery 只包含具有特定值的对象

pointers - 在 swift 中将 CFContextRef 转换为可变的 void 指针?

swift - 删除最后一行并同时加载多行

c - 如何将整数转换为 void 指针?

c# - 将对象强制转换为指定类的最佳做法

ios - Parse.com iOS SDK 崩溃 : PFSQLiteDatabase. m 第 178 行 -[PFSQLiteDatabase _executeQueryAsync:withArgumentsInArray:cachingEnabled:]

parse-platform - Parse Rest - 推送编码消息