投射失败可能与Pointer有关

时间:2015-05-03 15:52:05

标签: ios swift parse-platform subclassing pfquery

SCENARIO

该方案包含2个类“操作和位置”

这两个类如下所述相关

Operation{
   storedAt : Date
   type -> OperationType (Pointer)
   place -> Places (Pointer)
}

Places {
   title : String
   type -> OperationType (Pointer)
   location -> PFGeoPoint
} 

我想做什么只是检索用户附近的地方。为此我用过:

func getNearPlaces(location: CLLocation){
    if location.isZero() {
        return
    }

    var placeQuery = PFQuery(className: "Places")
    println("getNearPlaces location  \(location)")
    placeQuery.whereKey("location", nearGeoPoint: PFGeoPoint(location:location), withinKilometers: 2)
    placeQuery.whereKey("type", equalTo: detailItem.type)
    placeQuery.limit = 15

    placeQuery.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
        if error != nil {
            println("error: \(error)")
            return
        }

        println("results: \(objects?.count)")
        if let objs = objects as? [PFObject] {
            println("casting to [PFObject] ok")
        }

        if let objs = objects as? [Places] {
            println("casting to [Places] ok")
                self.places = objs
        }
        self.updateLocationOnMap()
    }
}

其中 detailItem 操作

的子类

现在重点!如果Operations类的 NO 位置指针列在所有行的类上被赋值,则一切正常。

results: Optional(3)
casting to [PFObject] ok
casting to [Places] ok

接下来,我使用[Places]值之一分配了detailItem.place,我继续使用app。一切都没问题。

当我再次启动应用程序并且detailItem.place被赋值时,这是我的输出:

results: Optional(3)
casting to [PFObject] ok

施放到[地方]失败

有人可以解释一下为什么吗?是什么关系di parse或swift?有什么我做错了吗?

我添加了一些细节:

因此,如何在Parse.com Doc上定义:

class Places:PFObject,PFSubclassing{

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
}

static func parseClassName() -> String {
    return "Places"
}

@NSManaged var title:String
@NSManaged var type:OperationsType
@NSManaged var location:PFGeoPoint

}

低于从上述PFQuery检索到的对象的输出:

[<Places: 0x7fb94dc9e9d0, objectId: yFDNHinajo, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94d587ad0, latitude: 95.497511, longitude: 19.224867>";
    title = tamoilled;
    type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}, 
<Places: 0x7fb94a84b2f0, objectId: zlh3CMVu0t, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94dc63f20, latitude: 95.498064, longitude: 19.226219>";
    title = "new Place";
    type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}, 
<Places: 0x7fb94dc9f8f0, objectId: ymY7SlA3Is, localId: (null)> 
{
    location = "<PFGeoPoint: 0x7fb94dce4e70, latitude: 95.510635, longitude: 19.217392>";
    title = "Agip 2";
   type = "<OperationsType: 0x7fb94844a2f0, objectId: 2IPOWNo1lM>";
}]

解析iOS sdk 1.7.2,Xcode 6.3.1

on didFinishLaunchingWithOptions

ParseCrashReporting.enable();
Parse.enableLocalDatastore()
Parse.setApplicationId("...", clientKey:"...");

更新

我替换了

println("results: \(objects?.count)")
if let objs = objects as? [PFObject] {
    println("casting to [PFObjects] ok \(objs)" )
}

self.places = []
for result in objects! {
    println("object \(result)")
    if let place  = result as? Places {
        println("place ok")
        self.places.append(place)
    }
}

结果是,即使看起来是正确的,也不会将对应于detailItem.place的Places对象转换为Places。 (专注于第一本字典。它没有跟着'放置好')

object <Places: 0x7ff5a0f64be0, objectId: yFDNHinajo, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a312c720, latitude: 95.497511, longitude: 19.224867>";
title = tamoilled;
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}
object <Places: 0x7ff5a0fbcdc0, objectId: zlh3CMVu0t, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a3121f30, latitude: 95.498064, longitude: 19.226219>";
title = "new Place";
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}  
place ok
object <Places: 0x7ff5a3724320, objectId: ymY7SlA3Is, localId: (null)> {
location = "<PFGeoPoint: 0x7ff5a31b8450, latitude: 95.510635, longitude: 19.217392>";
title = "Agip 2";
type = "<OperationsType: 0x7ff5a0edd520, objectId: 2IPOWNo1lM>";
}
place ok

更新2

我尝试在启动应用上执行相同的查询。没有错误和演员工作正常 我现在没有更多的资源。我认为这可能是来自Parse.com框架的错误。或者在我的场景中我不知道的事情(应该很简单)

1 个答案:

答案 0 :(得分:1)

<强>解决

通过新的Parse更新,他们建议添加显式

Places.registerSubclass()

Parse.setApplicationId()