gpt4 book ai didi

ios - 无法在 Swift 2 中将类型 '__NSCFNumber' 的值转换为 'NSString'

转载 作者:可可西里 更新时间:2023-11-01 00:59:21 34 4
gpt4 key购买 nike

我正在使用 CLLocationmanager 从 app delegate 获取纬度和经度通过使用以下代码:

  class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate{


var window: UIWindow?

var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
initLocationManager();
return true
}

func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
CLLocationManager.locationServicesEnabled()
locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if ((error) != nil) {
if (seenError == false) {
seenError = true
print(error)
}
}
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
var coord = locationObj.coordinate
NSUserDefaults.standardUserDefaults().setObject(coord.latitude, forKey: "latitude")



NSUserDefaults.standardUserDefaults().setObject(coord.longitude, forKey: "longitude")
NSUserDefaults.standardUserDefaults().synchronize()
print(coord.latitude)
print(coord.longitude)
}
}

在使用代码在 map View 中使用来自 nsuserdefaults 的值时

   latitude = (NSUserDefaults.standardUserDefaults().objectForKey("latitude") as? NSString)! as String
longitude = (NSUserDefaults.standardUserDefaults().objectForKey("longitude") as? NSString)! as String
let latitude1 = (latitude as NSString).doubleValue
let longitude1 = (longitude as NSString).doubleValue
let location = CLLocationCoordinate2D(
latitude: latitude1,
longitude: longitude1
)

我在 latitude = (NSUserDefaults.standardUserDefaults().objectForKey("latitude") as?NSString) 收到错误消息“无法将‘__NSCFNumber’ (0x1944fb9f8) 类型的值转换为‘NSString’”!作为字符串

最佳答案

在 AppDelegate 中使用此代码像这样在 NSUserDefaults 中保存纬度和经度

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locationFixAchieved == false) {
locationFixAchieved = true
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
NSUserDefaults.standardUserDefaults().setDouble(coord.latitude, forKey: "latitude")
NSUserDefaults.standardUserDefaults().setDouble(coord.longitude, forKey: "longitude")
NSUserDefaults.standardUserDefaults().synchronize()
print(coord.latitude)
print(coord.longitude)
}
}

为了获取值,请在 View Controller 文件中使用此代码

 override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
let latitude2 = NSUserDefaults.standardUserDefaults().objectForKey("latitude")
let longitude2 = NSUserDefaults.standardUserDefaults().objectForKey("longitude")
let latitude1 = latitude2!.doubleValue
let longitude1 = longitude2!.doubleValue
let location = CLLocationCoordinate2D(
latitude: latitude1,
longitude: longitude1
)


}

关于ios - 无法在 Swift 2 中将类型 '__NSCFNumber' 的值转换为 'NSString',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591915/

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