Sijil S Sijil S - 6 months ago 333
iOS Question

Could not cast value of type '__NSCFNumber' to 'NSString' in Swift 2

I am using CLLocationmanager to get the latitude and longitude from app delegate
by using the following code:

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)
}
}


While using the value from nsuserdefaults in map view by using the code

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
)


I am getting error "Could not cast value of type '__NSCFNumber' (0x1944fb9f8) to 'NSString'" at
latitude = (NSUserDefaults.standardUserDefaults().objectForKey("latitude") as? NSString)! as String

Answer

In AppDelegate use this code to save the latitude and longitude in NSUserDefaults like this

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)
    }
}

And for fetching the value use this code in the viewcontroller file

 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
    )


}