【发布时间】:2015-05-03 15:52:04
【问题描述】:
场景
场景包含 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
的子类现在重点!如果 Operations 类具有 NO 放置指针列,则所有行的类都可以正常工作。
results: Optional(3)
casting to [PFObject] ok
casting to [Places] ok
接下来,我为 detailItem.place 分配了 [Places] 值之一,并继续使用 app.place。一切都很好。
当我再次启动该应用程序并且 detailItem.place 被评估时,这是我的输出:
results: Optional(3)
casting to [PFObject] ok
投射到 [Places] 失败
有人能解释一下为什么吗?它与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
关于 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,即使它看起来是正确的! (重点放在第一本字典上。后面没有'place ok')
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 框架的错误。或者在我的场景中我不知道的东西(应该很简单)
【问题讨论】:
-
有人可以支持我吗?
-
您应该在答案块中添加您的答案,而不是作为对问题的编辑。然后,接受你自己的答案。
标签: ios swift parse-platform subclassing pfquery