Ios 属性在使用时显示为nil,但在使用前打印到调试控制台?
如果有任何其他可能与此问题相关的内容,请让我知道,以便我可以尝试提供更多详细信息。我大约一周了,我似乎找不到问题所在。这可能会让我想把电脑扔到墙上,但我似乎无法理解:(Ios 属性在使用时显示为nil,但在使用前打印到调试控制台?,ios,json,swift,google-maps,google-places-api,Ios,Json,Swift,Google Maps,Google Places Api,如果有任何其他可能与此问题相关的内容,请让我知道,以便我可以尝试提供更多详细信息。我大约一周了,我似乎找不到问题所在。这可能会让我想把电脑扔到墙上,但我似乎无法理解:( 这个问题似乎已经孤立到坐标变量,但是这个问题仍然可能与类是如何构造的有关,但我无法确定它们是如何构造的 我能够在发出请求之前将当前位置坐标打印到屏幕上,但在调用将发送api请求的方法时,JSON中出现以下错误 “状态”:“无效的请求” 谷歌文档声称: “无效的_请求通常表示缺少必需的查询参数(位置或半径)。” 我所经历的是:
- 这个问题似乎已经孤立到坐标变量,但是这个问题仍然可能与类是如何构造的有关,但我无法确定它们是如何构造的
- 我能够在发出请求之前将当前位置坐标打印到屏幕上,但在调用将发送api请求的方法时,JSON中出现以下错误
- 使用硬编码坐标,我能够进行搜索并成功返回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()
}
}
}
//所有类都有自己的swift文件
类ViewController:UIViewController、CLLocationManagerDelegate
{
设currentLocation=currentLocation()
let downloadData=downloadData()
重写func observeValueForKeyPath(键路径:String?,对象对象的类型:AnyObject?,更改:[String:AnyObject]?,上下文:UnsafeMutablePointer)
{
如果!发现了错配
{
让myLocation:CLLocation=将![NSKeyValueChangeNewKey]更改为!CLLocation
currentLocation.coordinate=myLocation.coordinate
currentLocation.latitude=myLocation.coordinate.latitude
currentLocation.longitude=myLocation.coordinate.longitude
didFindMyLocation=true
打印(currentLocation.coordinates)//正确打印坐标
downLoadData.downloadAllLocations({()->()在//请求中,我正在尝试进行输出,
//“状态”:“无效的请求”,
})
}
}
类CurrentLocation:NSObject,CLLocationManagerDelegate
{
重写init()
{
super.init()
}
var locationManager=CLLocationManager()
//罪犯!
变量纬度:CLLocationDegrees!
变量经度:CLLocationDegrees!
}
//在自己的swift文件中键入别名。
进口基金会
typealias DownLoadComplete=()->()
类下载数据:NSObject
{
设currentLocation=currentLocation()
重写init()
{
super.init()
}
func下载所有位置(已完成:下载完成)
{
//硬编码的坐标输出正确的JSON响应。那么为什么我不能从当前位置插入坐标呢???
让URL_搜索=”https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
让urlString=“\(URL\u搜索)位置=\(currentLocation.latitude),\(currentLocation.latitude)和半径=\(searchRadius)&类型=\(searchType)&键=\(API\u ServerKey)”
让url=NSURL(string:urlString)!
打印(url)//在currentLocation.latitude和currentLocation.longitude中输出nil、nil
请求(.GET,url).responseJSON{(response)->中的Void
开关响应。结果
{
成功案例:
如果let value=response.result.value
{
让json=json(值)
print(json)//如果插入硬编码的坐标,则输出json。如果尝试当前位置,则接收到无效的_请求错误。
如果let results=json[“results”].array
{
为了得到结果{
如果let places=result[“place”].string
{
self.places=place
}
}
}
}
案例。失败(let错误):
打印(错误)
}
已完成()
}
}
}
在发送请求(downLoadData.downLoadAllLocation[调用打印(url))之前,通过打印(currentLocation.coordinates)测试输出到调试控制台的输出是:
位置坐标2D(纬度:37.33233141,经度:-122.0312186)
…等等
这里的问题是,插入到搜索查询中的坐标显示为nil,nil?我尝试过处理代码。但是运气不好……它们被打印出来,但在搜索中却显示为nil,这似乎很奇怪。
DownloadData
类引用了一个currentLocation
变量(可能是CurrentLocation
的一个实例,但是这个变量没有在代码示例中的任何地方定义。我假设它是ViewController
的一个成员变量,在这种情况下,问题是:DownloadData
如何知道它
您可能需要将
currentLocation
传递到downloadData.downloadAllLocations()
作为参数。您试图构建什么?传递到url的硬编码坐标和其他值是什么?在查询str构造中使用的任何变量都是可选的吗?如果是,则可能作为可选(“…”)进入查询url因此,您可能会收到无效的请求。请尝试打印搜索查询url,看看传递给GET调用的实际url是什么?因此,我想这与原始问题相同。如果我能够实际打印(currentLocation.latitude)和打印(currentLocation.latitude),为什么它们为零就在我发送请求并将正确的坐标输出到调试控制台之前