加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:23:36 所属栏目:百科 来源:网络整理
导读:在最近更新 swift编译器更新之前,此代码工作正常.我已经纠正了所有明显的错误,但由于某些原因,当我运行应用程序时,它在for循环中崩溃时尝试将[AnyObject]强制转换为我的自定义Memory类. 日志和错误显示如下: Has connectivitySuccessfully retrieved 8 memo
在最近更新 swift编译器更新之前,此代码工作正常.我已经纠正了所有明显的错误,但由于某些原因,当我运行应用程序时,它在for循环中崩溃时尝试将[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(),我解决了我的问题!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读