iOS模拟器和反向定位
iOS模拟器在抓取位置时不经常工作,这正常吗?每当我运行模拟器时,我这个应用程序的项目总是有50%的时间崩溃……但我似乎无法在代码本身中找出问题所在。如果问题确实存在于代码本身,有人能帮我找到问题吗?该错误为“致命错误:在展开可选值时意外发现nil”,并表示它发生在我的refreshPost函数的第行iOS模拟器和反向定位,ios,swift,parse-platform,Ios,Swift,Parse Platform,iOS模拟器在抓取位置时不经常工作,这正常吗?每当我运行模拟器时,我这个应用程序的项目总是有50%的时间崩溃……但我似乎无法在代码本身中找出问题所在。如果问题确实存在于代码本身,有人能帮我找到问题吗?该错误为“致命错误:在展开可选值时意外发现nil”,并表示它发生在我的refreshPost函数的第行 eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation) 我正在使用解析作为此应用程序的一部分。另外,我有一个视图显
eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation)
我正在使用解析作为此应用程序的一部分。另外,我有一个视图显示刷新“帖子”,这是正确的方法吗?非常感谢
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()
从该完成块的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)
}
}
如中所述,仅当反向地理编码器完成且未返回错误时才更新post。ok。。这是swift中可选对象的展开问题。我建议使用断点来获取异常,或者您可以在xcode中添加异常断点来获取崩溃位置。非常感谢!到目前为止,它还没有崩溃,因为实施了这一变化。非常感谢!!