我是一个新手 - 请不要讨厌。
该方法编译,但我不确定如何实际检索该方法返回的浮点值(即两点之间的距离)(或应该返回)。
-(float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b
{
//distance formula:
//sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
float resultDistance;
resultDistance = sqrt( pow((b.latitude - a.latitude), 2) + pow((b.longitude - a.longitude), 2));
return resultDistance;
}
//Somewhere else...
float theDistanceBetween;
//Below is incorrect:
theDistanceBetween = [findDistanceBetween: location1 and: location2];
由于
答案 0 :(得分:2)
因此,如果您的错误是self
未声明,则表示您尝试从声明它的类的上下文之外发送-findDistanceBetween:and:
。
当您执行[obj method]
之类的操作时,需要执行以下操作:
obj
是Objective-C类的一个实例。obj
的类实现了-method
。因此,如果您的邮件的收件人是self
,则表示:
您需要在类的实现的上下文中隐式声明self
。
您所在的课程必须与工具-findDistanceBetween:and:
相同。
方法不仅仅是可以在任何上下文中调用的函数的闪亮替代品。可以在实现它们的对象上调用它们(技术上不像“Objective-C”那样在类似Smalltalk的语言中“调用”,但这是另一次)。
我怀疑你也有更大的设计问题。什么样的对象-findDistanceBetween:and:
意味着发送给谁?如果它是一个实现更大的类的实用程序方法,那么它应该是一个类方法(+findDistanceBetween:and:
),因为它不需要知道任何特定的实例。但是,如果它是Coordinate
上的方法,那么它最好表示为-findDistanceTo:
,它将采用坐标参数。然后执行该操作会将提供的坐标参数与self
进行比较。
答案 1 :(得分:1)
findDistanceBetween:and:
是一个实例方法;这是你班级的一个特定实例可以做的事情。
所以你称之为:
theDistanceBetween = [self findDistanceBetween: location1 and: location2];
这意味着“将消息'findDistanceBetween:location1和:location2'发送到对象'self',并将结果存储到theDistanceBetween”。 self
仅表示当前对象;它是一个向自己发送消息的对象。
答案 2 :(得分:0)
尝试将-(float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b;
添加到.h文件
答案 3 :(得分:0)
findDistanceBetween:and:
块中是否定义了@implementation
?您的代码应该类似于
// in the .h file
@interface MyClass : NSObject
// declaration of the method
- (float)findDistanceInBetween:(Coordinate *)a and:(Coordinate *)b;
@end
// in the .m file
@implementation MyClass
// definition of the method
- (float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b {
return sqrt(powf(a.x - b.x, 2.f) + powf(a.y - b.y, 2.f));
}
@end
// then somewhere else that needs to calculate the difference:
// (assume coord_a and coord_b already exist)
// create an instance of MyClass
MyClass *myInstance = [[MyClass alloc] init];
// send the `findDistanceBetween:and:` message to the instance
float distance = [myInstance findDistanceBetween:coord_a and:coord_b];
// when you're done with the instance, you need to clean up
[myInstance release];
将这些类型的方法放在Coordinate类本身上可能更有意义,因此您可以执行以下操作:
Coordinate *coord_a = <get the coordinate from somewhere>;
Coordinate *coord_b = <get the coordinate from somewhere>;
float distance = [coord_a distanceFrom:coord_b];
float angle = [coord_a angleTo:coord_b];
不要担心任何人讨厌,我们在这一次都是新人。 :)
答案 4 :(得分:0)
正如Ferruccio所提到的,纬度和经度都是以度为单位的,你的方法应该是返回某种形式的距离。下面是一些计算距离以及2个CLLocationCoordinate2D之间的方法。
- (double)degreeToRadian:(double)degree{
return (degree * (M_PI/180.0));
}
- (double)radianToDegree:(double)radian{
return (radian *(180.0/M_PI));
}
-(double) distanceFromCordinate:(CLLocationCoordinate2D)fromCoord to:(CLLocationCoordinate2D)toCoord {
double radiusOfEarth = 6371.0;
double fromLongitude, fromLatitude, toLongitude, toLatitude;
double _deltaLongitude, _deltaLatitude;
double a, c;
fromLongitude = [self degreeToRadian:fromCoord.longitude];
fromLatitude = [self degreeToRadian:fromCoord.latitude];
toLongitude = [self degreeToRadian:toCoord.longitude];
toLatitude = [self degreeToRadian:toCoord.latitude];
_deltaLongitude = toLongitude - fromLongitude;
_deltaLatitude = toLatitude - fromLatitude;
a = (sin(_deltaLatitude/2) * sin(_deltaLatitude/2)) + ( cos(fromLatitude) * cos(toLatitude) * (sin(_deltaLongitude/2) * sin(_deltaLongitude/2)) );
c = 2 * atan2( sqrt(a), sqrt(1-a));
return (radiusOfEarth * c);
}
- (double)bearingFromCordinate:(CLLocationCoordinate2D)fromCoord to:(CLLocationCoordinate2D)toCoord{
double fromLatitude, toLatitude;
double _deltaLongitude;
double x, y;
double bearing;
fromLatitude = [self degreeToRadian:fromCoord.latitude];
toLatitude = [self degreeToRadian:toCoord.latitude];
deltaLongitude = [self degreeToRadian:(toCoord.longitude - fromCoord.longitude)];
y = sin(deltaLongitude) * cos(toLatitude);
x = (cos(fromLatitude) * sin(toLatitude)) - (sin(fromLatitude) * cos(toLatitude) * cos(deltaLongitude));
bearing = atan2(y,x);
return fmod(([self radianToDegree:bearing] + 360.0), 360.0) ;
}