'queryForTable'在Swift中不能返回'nil' [英] 'queryForTable' cannot return 'nil' In Swift

查看:144
本文介绍了'queryForTable'在Swift中不能返回'nil'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PFQueryTableViewController 作为ParseUI的一部分,以基于当前用户的地理位置加载对象表。我见过其他几个(较旧的)论坛帖子(如 here )详细说明如果像 currentLocation:PFGeoPoint这样的值, queryForTable 函数应该返回nil。 是零。然后等待 viewDidLoad 中的后台进程获取 PFGeoPoint loadObjects() code>,因此再次调用 queryForTable 函数。



我看到其他人说Swift的ParseUI库可能没有 queryForTable 函数,允许返回零。

  var currentLocation :PFGeoPoint? = nil 

覆盖func viewDidLoad(){
if currentLocation == nil {
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint:PFGeoPoint?,error:NSError?) - >如果错误== nil {
print(得到用户的当前位置!),则在
中无效
self.currentLocation = geoPoint
print(使用用户位置重新加载表)
self.loadObjects()
}
}
}
}

覆盖func queryForTable() - > PFQuery {

if self.currentLocation!= nil {
print(根据用户的位置生成查询!)
let query = PFQuery(className:self.parseClassName!)
query.whereKey(taskLocation,nearGeoPoint:self.currentLocation!,withinMiles:50)

if(self.objects?.count == 0)
{
query.cachePolicy = PFCachePolicy.CacheThenNetwork
}

返回查询
}

print(用户当前位置未知)
return nil



$ b显然,由于函数不是(注意问号):

 覆盖func queryForTable() - > PFQuery? {
...
}

我尝试的解决方法是返回 PFQuery()代替nil,但是我相信它在 之后返回 $ viewDidLoad self.loadObjects()。我看到的行为是带有单元格结果的表格的瞬间闪烁,然后是空表格。



以下是Google小组关于这个问题的讨论的链接,Hector Ramos说它可以与Objective-C但不是Swift(......)?我正在运行最新版本的ParseUI(1.1.6)。



Swift中还有一个选项可以实现吗?如果不是,那么什么时候?



如果这不是一个选项,那么人们尝试成功的一些解决方法是什么?

解决方案

我其实已经想出了这个问题 - 与代码本身无关。显然,当你在iOS模拟器上设置了一个假的位置时,它也会在你使用插入的真实iOS设备时强制执行不同的位置。我的结果没有显示出来,因为合法地不是一个带有 PFGeoPoint 靠近我的位置(因为它认为我在伦敦!)

无论如何,故事的寓意在于确保您始终知道您的预设位置在模拟器和实体iOS设备中。



PS - 上述代码在我的位置设置正确时工作。


I am using PFQueryTableViewController as part of ParseUI to load a table of objects based on the currentUser's geolocation. I have seen several other (older) forum posts (like here) detailing that the queryForTable function should return nil if a value like currentLocation:PFGeoPoint? is nil. Then wait on the background process in viewDidLoad to get the PFGeoPoint and loadObjects(), thus calling the queryForTable function again.

I am seeing others say that Swift's ParseUI libraries may not have a queryForTable function that allows nil returns.

var currentLocation:PFGeoPoint? = nil

override func viewDidLoad() {
    if currentLocation == nil {
        PFGeoPoint.geoPointForCurrentLocationInBackground {
            (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
            if error == nil {
                print("Got the user's current location!")
                self.currentLocation = geoPoint
                print("Reloading table using user's location")
                self.loadObjects()
            }
        }
    }
}

override func queryForTable() -> PFQuery {

    if self.currentLocation != nil {
        print("Generating query based on user's location!")
        let query = PFQuery(className:self.parseClassName!)
        query.whereKey("taskLocation", nearGeoPoint: self.currentLocation!, withinMiles: 50)

        if(self.objects?.count == 0)
        {
            query.cachePolicy = PFCachePolicy.CacheThenNetwork
        }

        return query
    }

    print("User's current location is not known")
    return nil
}

Obviously, this fails to build because the function is not (note the question mark):

override func queryForTable() -> PFQuery? {
...
}

My attempted workaround was to return PFQuery() instead of nil, but I believe it returns after the viewDidLoad self.loadObjects(). The behavior I see is a momentary flash of a table with cell results, and then an empty table.

Here is a link to the Google Group discussion about this very issue, with Hector Ramos saying that it works with Objective-C but not Swift (...yet?). I'm running the latest ParseUI as of this posting (1.1.6).

Is there an option yet to do this in Swift? If not, when?

If this isn't an option, what are some workarounds people have tried successfully?

解决方案

I actually figured this issue out - nothing to do with the code itself. Apparently, when you set a fake location on the iOS Simulator, it also enforces that different location when you use a real iOS device plugged in. My results were not showing up because there legitimately wasn't an object with a PFGeoPoint near my location (because it thought I was in London!)

Anyway, moral of the story is to make sure you always know your preset location in both Simulator and physical iOS devices.

PS - The code above does work when my location is set correctly.

这篇关于'queryForTable'在Swift中不能返回'nil'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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