使用时显示为nil的属性,但在使用前打印到调试控制台?

时间:2016-02-15 21:51:06

标签: ios json swift google-maps google-places-api

如果还有其他任何内容可以链接到该问题,请告诉我,以便我可以尝试提供更多详细信息。我约一个星期,我似乎无法找到问题。它可能会让我想要把电脑放在墙上,但我似乎无法弄明白:(

  • 问题似乎已经被隔离到坐标变量,但问题仍然可能与类的构建方式有关,但我无法确定它们是如何形成的。

  • 我可以在发出请求之前将当前位置坐标打印到屏幕,但是在调用将发送api请求的方法时,我在JSON中收到以下错误

  

“status”:“INVALID_REQUEST”,

Google文档声称:

  

“INVALID_REQUEST通常表示缺少必需的查询参数(位置或半径)。”

我遇到的是:

  • 使用硬编码坐标,我可以进行搜索并成功返回JSON。

  • 我能够成功获取当前位置坐标。

  • 然而;我无法将当前位置坐标插入到搜索查询中。

这是一大堆代码,我相信只需要掌握这个问题。我感谢任何可能导致解决问题的建议:

// all classes have their own swift file

class ViewController: UIViewController, CLLocationManagerDelegate
{

let currentLocation = CurrentLocation()
let downloadData    = DownLoadData()

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{
    if !didFindMyLocation
    {
        let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
        currentLocation.coordinate = myLocation.coordinate
        currentLocation.latitude   = myLocation.coordinate.latitude
        currentLocation.longitude  = myLocation.coordinate.longitude

        didFindMyLocation = true

        print(currentLocation.coordinates) // prints coordinates correctly

        downLoadData.downloadAllLocations({ () -> () in  //request I am attempting to make that outputs,
        //"status" : "INVALID_REQUEST",
        })
    }
}

class CurrentLocation: NSObject, CLLocationManagerDelegate
{
override init()
{
    super.init()
}

var locationManager = CLLocationManager()

//THE CULPRITS!
var latitude   : CLLocationDegrees!
var longitude  : CLLocationDegrees!

}

// typealias in a swift file of its own.

import foundation

typealias DownLoadComplete = () -> ()


class DownloadData: NSObject
{
let currentLocation = CurrentLocation()

override init()
{
    super.init()
}

func downloadAllLocations(completed: DownloadComplete)
{
    //hardcoded coordinates output correct JSON Response. So Why Cant I Interpolate the coordinates from my current location???
    let URL_Search = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
    let urlString  = "\(URL_Search)location=\(currentLocation.latitude),\(currentLocation.longitude)&radius=\(searchRadius)&types=\(searchType)&key=\(API_ServerKey)"

    let url = NSURL(string: urlString)!
    print(url) // outputs nil,nil in the currentLocation.latitude and currentLocation.longitude spots
    Alamofire.request(.GET,url).responseJSON { (response) -> Void in

        switch response.result
        {
        case .Success:
            if let value = response.result.value
            {
                let json = JSON(value)
                print(json) // If hardcoded coordinates are inserted then I the json is outputted. If current location is attempted then I receive an INVALID_REQUEST error.

                if let results = json["results"].array
                {
                    for result in results {
                        if let places = result["place"].string
                        {
                            self.places = place
                        }
                    }
                }
            }
        case .Failure(let error):
            print(error)
        }

        completed()
     }
  }

}

在发送请求之前通过print(currentLocation.coordinates)测试输出(downLoadData.downLoadAllLocation [其中有一个打印(url)被调用])输出到调试控制台是:

  

CLLocationCoordinate2D(纬度:37.33233141,经度:-122.0312186)   https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=nil,nil   ...等

这里的问题是插入到搜索查询中的坐标显示为nil,nil?我试过操纵代码。但是没有运气......看起来很奇怪他们被打印但是在搜索中他们看起来是零。怎么可能??

1 个答案:

答案 0 :(得分:0)

DownloadData类引用currentLocation变量(可能是CurrentLocation的一个实例,但是此变量未在代码示例中的任何位置定义。我假设它是一个成员变量ViewController,在这种情况下,问题是:DownloadData如何了解它?

您可能需要将currentLocation传递给downloadData.downloadAllLocations()作为参数。