Iphone 消息表达式中的传递值参数未定义
我正在开发一个iPhone应用程序,我在方法中得到了警告:Iphone 消息表达式中的传递值参数未定义,iphone,objective-c,argument-passing,Iphone,Objective C,Argument Passing,我正在开发一个iPhone应用程序,我在方法中得到了警告: NSNumber *latitudeValue; NSNumber *longitudeValue; [self obtainLatitude:latitudeValue longitude:longitudeValue]; 方法声明如下: - (void) obtainLatitude:(NSNumber *)latitudeValue longitude:(NSNumber *)longitudeValue { NSN
NSNumber *latitudeValue;
NSNumber *longitudeValue;
[self obtainLatitude:latitudeValue longitude:longitudeValue];
方法声明如下:
- (void) obtainLatitude:(NSNumber *)latitudeValue longitude:(NSNumber *)longitudeValue {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
[f release];
}
正如你们所看到的,我试图计算latitudeValue和longitudeValue,调用actainlation:longitude:但我做错了什么
如何修复该错误?您确实是在按值传递指针,因此当您重新分配它们时,这在方法内部生效。一种替代方法是执行以下操作:
- (void) obtainLatitude:(NSNumber **)latitudeValue longitude:(NSNumber **)longitudeValue {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
*latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
*longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
[f release];
}
那么,您的呼叫将如下所示:
NSNumber *latitudeValue;
NSNumber *longitudeValue;
[self obtainLatitude:&latitudeValue longitude:&longitudeValue];
Elfred的答案是可行的,但对于非NSError**参数,通过引用传递是非常少见的。同样,坐标——一般来说,数值——最典型地存储在结构中的常规旧C类型中,因为相对而言,NSNumber是一个相当大的开销。如果有几十个、一百个或几千个坐标,对其中的几个来说没什么大不了的 比如:
struct MyLocation {
CGFloat latitude;
CGFloat longitude;
};
typedef struct MyLocation MyLocation;
然后:
在iPhone/Cocoa程序中,类似于上述内容更为典型
正如Dave指出的,您真的不需要为此定义自己的类型。使用+1或使用CLLocationCoordinate2D,这与使用double而不是float几乎是一样的。我使用NSNumber检查latitude.text是否为有效数字。如果您知道另一种检查方法,我会使用它。就个人而言,我只会使用一个数字格式化程序来转换为NSNumber,然后将其转换为CLLocationCoordinate2D中使用的标量类型。
- (MyLocation) mapCoordinates {
MyLocation parsedLocation;
parsedLocation.latitude = ....;
parsedLocation.longitude = ....;
return parsedLocation;
}