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

Properties appearing as nil when used but print to debug console prior to use?

如果有任何其他问题可能与此问题有关,请告诉我,以便我尝试提供更多详细信息。我大约一周了,但似乎找不到问题所在。这可能会让我想把电脑扔到墙上,但我似乎想不通:(

"status" : "INVALID_REQUEST",

Google 文档声称:

"INVALID_REQUEST generally indicates that a required query parameter (location or radius) is missing."

我遇到的是:

这是一段很好的代码,我相信这是解决问题所需要的全部。我感谢任何可能导致解决问题的建议:

// 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[调用了 print(url)])输出到调试控制台是:

CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.0312186) https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=nil,nil ...etc

这里的问题是插入到搜索查询中的坐标显示为 nil,nil?我试过操纵代码。但运气不好……它们被打印出来似乎很奇怪,但随后在搜索中它们显示为零。怎么可能??

DownloadData class 指的是一个 currentLocation 变量(大概是 CurrentLocation 的一个实例,但是这个变量没有在你的代码示例中的任何地方定义。我假设它是 ViewController 的成员变量,在这种情况下,问题是:DownloadData 如何知道它?

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