我的代码工作正常,我想要它在模拟器中的确切方式.但是,当我把它放在真实设备上并转到该位置时,locationManager(..., didEnterRegion region: CLRegion)不会被调用.我不认为它是在代码中的错误,因为当我进入和离开的模拟器两个位置didExitRegion和didEnterRegion被调用,而不是真正的设备上.我正在使用Swift 2.0 Xcode 7 beta 5中的iPhone 5S进行测试,在大型开放区域内具有良好的小区覆盖.我也尝试过不同的半径.我的简化代码如下:
import UIKit
import CoreLocation
import MapKit
class GeoLapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var manager: CLLocationManager?
var location: CLLocationCoordinate2D!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager?.delegate = self;
manager?.desiredAccuracy = kCLLocationAccuracyBest
self.mapView.delegate = self
mapView.mapType = MKMapType.Satellite
manager?.startUpdatingLocation()
let currRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), radius: 20, identifier: "Laplocation")
manager?.startMonitoringForRegion(currRegion)
let newAnotation = MKPointAnnotation() …Run Code Online (Sandbox Code Playgroud) 这是我的VC代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
mapView.showsUserLocation = (status == .AuthorizedAlways)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
Run Code Online (Sandbox Code Playgroud)
我还在plist文件中添加了NSLocationWhenInUseUsageDescription.我有CoreLocation和MapKit框架,任何想法为什么这不起作用?它不会在地图上显示用户位置,也不会打印出用户的坐标.在堆栈溢出时没有在网上找到任何东西.
我有一个名为struct的结构Jar,我想将它们的数组保存到NSUserDefaults.这是jar结构代码:
struct Jar {
let name: String
let amount: Int
init(name: String, amount: Int){
self.name = name
self.amount = amount
}
}
Run Code Online (Sandbox Code Playgroud)
我相信我需要将它转换为NSObject才能保存它.(因为您无法将结构直接保存到NSUserDefaults).我的问题是: 如何将结构数组转换为NSObject?以及如何将NSObject转换回结构数组.