无法在 Swift 2 中将“__NSCFNumber”类型的值转换为“NSString”
Posted
技术标签:
【中文标题】无法在 Swift 2 中将“__NSCFNumber”类型的值转换为“NSString”【英文标题】:Could not cast value of type '__NSCFNumber' to 'NSString' in Swift 2 【发布时间】:2016-06-02 12:29:45 【问题描述】:我正在使用 CLLocationmanager 从应用程序委托中获取纬度和经度 通过使用以下代码:
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)
通过使用代码在地图视图中使用 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)! as String
处出现错误“无法将类型 '__NSCFNumber' (0x1944fb9f8) 的值转换为 'NSString'”
【问题讨论】:
coord.latitude
是 double
,而不是 string
。因此,当您将其保存到 NSUserDefaults
时,它会转换为 NSNumber
。所以这应该可以解决问题:let latitude1 = (latitude as NSNumber).doubleValue
您将值保存为数字并尝试将其读回字符串。到底是什么问题?
感谢问题已解决..
【参考方案1】:
在 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)
为了获取值,请在 viewcontroller 文件中使用此代码
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
)
【讨论】:
【参考方案2】:NSUserDefaults.standardUserDefaults().objectForKey("longitude") 是一个 NSNumber,所以你只需要这样做:
let latitude = NSUserDefaults.standardUserDefaults().objectForKey("latitude")
let longitude = NSUserDefaults.standardUserDefaults().objectForKey("longitude")
let latitude1 = latitude.doubleValue
let longitude1 = longitude.doubleValue
更多,首先你可以像这样设置用户默认值
let latitude: CLLocationDegrees = 1.0
NSUserDefaults.standardUserDefaults().setDouble("latitude", forKey: "latitude")
【讨论】:
它显示Cannot assign value of type 'AnyObject?'输入“字符串”以上是关于无法在 Swift 2 中将“__NSCFNumber”类型的值转换为“NSString”的主要内容,如果未能解决你的问题,请参考以下文章
无法在getDocuments querysnapshot(Swift)中将变量分配给Firebase数据[重复]