PFObject无法转换为自定义子类 [英] PFObject unable to be cast to custom subclass

查看:177
本文介绍了PFObject无法转换为自定义子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在最近更新swift编译器更新之前运行正常。我已经纠正了所有明显的错误,但由于某种原因,当我运行应用程序现在它崩溃在 for循环当尝试 [AnyObject ]

This code was working fine before the recent update to the swift compiler update. I have corrected all the obvious errors, but for some reason when I run the app now it crashes in the for in loop when trying to cast [AnyObject] to my custom Memory class.

日志和错误显示如下:

The log and error shows as follows:

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个正确的记忆,

So I know I'm retrieving 8 correct memories from Parse, and the current memory is correct...it just wont cast it.

代码:

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

        }
    } 


推荐答案

让我忙了几个小时 - 解析文档提到:

this one kept me busy some hours - Parse-Documentation mentions:


PFObject的强类型子类必须符合PFSubclassing协议, parse setApplicationId:clientKey:]。

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

通过调用 MyParseClass.registerSubclass > AppDelegate 我的问题已解决!

By calling MyParseClass.registerSubclass() in AppDelegate I got my issues fixed!

这篇关于PFObject无法转换为自定义子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆