我在将C-结构添加到NSDictionary时遇到了问题。
C结构是MapKit.h上的MKCoordinateRegion。
这份声明是
typedef struct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;CLLocationCoordinate2D的声明是
typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;MKCoordinateSpan是相同的。
现在,我想将MKCoordinateRegion添加到NSDictionary。
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(40.723128, -74.000694);
MKCoordinateSpan span = MKCoordinateSpanMake(1.0, 1.0);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
[param setObject:region forKey:@"region"];5行有错误。
错误消息是“正在将'MKCoordinateRegion‘发送到不兼容类型’id‘的参数”
谢谢。
发布于 2013-01-03 22:14:19
您不能将结构直接放入字典中,但可以使用NSValue将其包装为可以添加的方式。
typedef struct {
float real;
float imaginary;
} ImaginaryNumber;
ImaginaryNumber miNumber;
miNumber.real = 1.1;
miNumber.imaginary = 1.41;
NSValue *miValue = [NSValue value: &miNumber
withObjCType:@encode(ImaginaryNumber)];
[param setObject:miValue forKey:@"region"];发布于 2013-01-03 22:10:17
尝试将结构转换为NSData
NSData *data = [NSData dataWithBytes:®ion length:sizeof(MKCoordinateRegion)];
[param setObject:data forKey:@"region"];https://stackoverflow.com/questions/14140469
复制相似问题