gpt4 book ai didi

iOS 模拟器和 reverseGeocodeLocation

转载 作者:行者123 更新时间:2023-11-28 13:01:28 29 4
gpt4 key购买 nike

关于抓取位置,iOS 模拟器经常不工作是否正常?每当我运行模拟器时,这个应用程序的项目都会在 50% 的时间内崩溃……但我似乎无法在代码本身中查明问题。如果问题确实出在代码本身,谁能帮我找到问题所在?错误是“ fatal error :在展开可选值时意外发现 nil”,它说它发生在我的 refreshPost 函数中

eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation)

我将 Parse 用作此应用程序的一部分。另外,我有一个 viewDidAppear 来刷新“帖子”,这是解决这个问题的正确方法吗?非常感谢!

import UIKit
import Parse

class HomeTableViewController: UITableViewController, CLLocationManagerDelegate {

@IBOutlet weak var navigationBar: UINavigationItem!
@IBOutlet weak var menuButton: UIBarButtonItem!

@IBAction func cancelPost(segue: UIStoryboardSegue) {

}

var users = [String: String]()
var usernames = [String]()
var eventInfo = [String]()
var imageFiles = [PFFile]()
var usersLocation: String!

var locationManager: CLLocationManager!

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let userLocation: CLLocation = locations[0]


CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in

if error != nil {

print(error)

} else {

let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array

let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""

self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

menuButton.target = self.revealViewController()
menuButton.action = Selector("revealToggle:")

self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 250.0
}

override func viewDidAppear(animated: Bool) {

refreshPosts()

}


func refreshPosts() {

let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let users = objects {

self.users.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
self.eventInfo.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)

for object in users {

if let user = object as? PFUser {

self.users[user.objectId!] = user.username!

}
}
}

let eventsPostedQuery = PFQuery(className: "PostEvent")
eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation)
eventsPostedQuery.orderByDescending("createdAt")
eventsPostedQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let events = objects {
for event in events {
self.imageFiles.append(event["imageFile"] as! PFFile)
self.eventInfo.append(event["eventInfo"] as! String)
self.usernames.append(self.users[event["userId"] as! String]!)

self.tableView.reloadData()

}
}

})

})


}

最佳答案

你应该打电话

refreshPosts()

从这个完成 block 的 else block 内部:

CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in

if error != nil {

print(error)

} else {

let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array

let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""

self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
}
}

如仅在反向地理编码器完成且未返回错误时更新帖子。

关于iOS 模拟器和 reverseGeocodeLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33712974/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com