添加C-結構到NSDictionary報錯
C-struct是在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"];
第五行報錯:Sending 'MKCoordinateRegion' to parameter of incompatible type 'id'
請高手指教
不能直接添到dictionary,但是可以用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"];