我正在尝试将此代码转换为Swift 2.2。但我收到此错误。
这是目标 - C代码
- (NSDictionary *)parseLatLonFile:(NSString *)fileName
{
NSMutableDictionary *ret = [NSMutableDictionary new];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName
ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *lines = [content componentsSeparatedByString:@"\n"];
for (NSString *line in lines) {
NSArray *parts = [line componentsSeparatedByString:@","];
NSString *latStr = parts[0];
NSString *lonStr = parts[1];
CLLocationDegrees latitude = [latStr doubleValue];
CLLocationDegrees longitude = [lonStr doubleValue];
// For this example, each location is weighted equally
double weight = 1;
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude
longitude:longitude];
MKMapPoint point = MKMapPointForCoordinate(location.coordinate);
NSValue *pointValue = [NSValue value:&point
withObjCType:@encode(MKMapPoint)];
ret[pointValue] = @(weight);
}
return ret;
}
这是我的转换代码
func parseLatLonFile(fileName: String) -> [NSObject : AnyObject] {
var ret = [NSObject : AnyObject]()
var path = NSBundle.mainBundle().pathForResource(fileName, ofType: "txt")!
var content = try! String(contentsOfFile: path, encoding:NSUTF8StringEncoding)
var lines = content.componentsSeparatedByString("\n")
for line: String in lines {
var parts = line.componentsSeparatedByString(",")
var latStr = parts[0]
var lonStr = parts[1]
var latitude = CDouble(latStr)!
var longitude = CDouble(lonStr)!
// For this example, each location is weighted equally
var weight: Double = 1
var location = CLLocation(latitude: latitude, longitude: longitude)
var point = MKMapPointForCoordinate(location.coordinate)
var pointValue = NSValue.value(point, withObjCType: !)
ret[pointValue!] = weight
}
return ret
}
我正在尝试使用此库来显示热图: https://github.com/dataminr/DTMHeatmap
我正在将此转换为Swift,因为我真的想了解这是如何工作的。
有任何帮助吗?如果在swift中有一些热图示例是可能的,欢迎所有帮助。我正在尝试获取kml数据并解析它。
答案 0 :(得分:1)
原始代码似乎是将MKMapPoint
(结构)实例转换为NSValue
(类)实例,以便将其用作字典键。
原因在于,在Objective-C中,字典键/值(和数组元素)只能是引用类型对象(类实例),而不是值类型对象(struct instance,int,double等... )。
对于Swift词典(和数组),没有这样的约束:你可以使用类型为的键的字典,因此不需要使用{{1} }。MKMapPoint
编辑:结果NSValue
不符合协议MKMapPoint
,因此您无法使用类型作为字典的键;我的错误。
您可以将Hashable
实例转换为字符串,然后使用它。例如:
MKMapPoint
答案 1 :(得分:1)
试用此代码。
func parseLatLonFile(_ fileName: String) -> [AnyHashable: Any] {
var ret = [AnyHashable: Any]()
let path: String? = Bundle.main.path(forResource: fileName, ofType: "txt")
let content = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let lines: [Any]? = content?.components(separatedBy: "\n")
for line: String in lines as! [String] {
let parts: [Any] = line.components(separatedBy: ",")
let latStr: String = parts[0] as! String
let lonStr: String = parts[1] as! String
let latitude: CLLocationDegrees = Double(latStr)!
let longitude: CLLocationDegrees = Double(lonStr)!
// For this example, each location is weighted equally
let weight: Double = 1
let location = CLLocation(latitude: latitude, longitude: longitude)
var point: MKMapPoint = MKMapPointForCoordinate(location.coordinate)
let type = NSValue(mkCoordinate: location.coordinate).objCType
let pointValue = NSValue(&point, withObjCType: type)
ret[pointValue] = (weight)
}
return ret
}