将mapView.userLocation.location.coordinate.longitude保存到sqlite数据库中

bil*_*zan 1 sqlite objective-c mkmapview

我想将从 mapView.userLocation.location.coordinate.longitude 返回的值插入到我的 sqlite3 数据库中。我不确定它的类型。我不能这样做:

sqlite3_bind_text(stmt, 1, [mapView.userLocation.location.coordinate.longitude floatValue], -1, nil);
Run Code Online (Sandbox Code Playgroud)

我猜它会给出“无法转换为指针类型”错误,因为经度不是浮点数。

数据库表中字段类型为FLOAT。如何将经度值转换为浮点数?

多谢。

小智 5

MKUserLocation 的文档显示位置属性的类型为 CLLocation。CLLocation 的文档显示坐标属性的类型为 CLLocationCooperative2D。

核心位置数据类型参考显示 CLLocationCooperative2D 是一个包含纬度和经度的结构,每个纬度和经度的类型均为 CLLocationDegrees。

在同一页面上,CLLocationDegrees 显示为只是 的另一个名称double。所以纬度和经度都是 double 类型。

因此,不要使用sqlite3_bind_text,而是使用sqlite3_bind_double

sqlite3_bind_double(stmt, 1, mapView.userLocation.location.coordinate.longitude);
Run Code Online (Sandbox Code Playgroud)

要加载该值,您可以使用以下内容:

double longitude = sqlite3_column_double(stmt, column_index_here);
Run Code Online (Sandbox Code Playgroud)