iOS模拟器在获取位置时是否经常不能正常工作?每当我运行模拟器时,我的这个应用程序的项目都会在50%的时间内崩溃...但我似乎无法在代码本身内查明问题。如果问题确实存在于代码本身,有人可以帮我找到问题吗?错误是“致命错误:在展开可选值时意外发现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()
}
}
})
})
}
答案 0 :(得分:2)
你应该致电
refreshPosts()
来自此完成块的else块内部:
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)
}
}
如果反向地理编码器完成并且没有返回错误,则仅更新帖子。