Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么xcode会给我一个“;“未找到方法”;错误_Iphone_Xcode_Methods_Call - Fatal编程技术网

Iphone 为什么xcode会给我一个“;“未找到方法”;错误

Iphone 为什么xcode会给我一个“;“未找到方法”;错误,iphone,xcode,methods,call,Iphone,Xcode,Methods,Call,我有一个名为Shot的对象,它是UIIMageView的子类 // Shot.h #import <Foundation/Foundation.h> @interface Shot : UIImageView { CGPoint position; } - (void)SetShot:(CGPoint *)point; @end // Shot.m #import "Shot.h" @implementation Shot - (void)SetSh

我有一个名为
Shot
的对象,它是
UIIMageView
的子类

//  Shot.h

#import <Foundation/Foundation.h>


@interface Shot : UIImageView {
    CGPoint position; 
}
- (void)SetShot:(CGPoint *)point;
@end


//  Shot.m


#import "Shot.h"


@implementation Shot

- (void)SetShot:(CGPoint *)point;
{
    position.x = point->x;
    position.y = point->y;

}

@end
当我运行该程序时,调用该方法时会出现致命错误。为什么会这样?

#import "Shot.h"
而不是:

@class Shot;

您尚未创建
快照的实例
;您仅创建了具有以下内容的指针:

Shot *shot;
您必须分配并初始化它:

Shot *shot = [[Shot alloc] init];

有时还必须导入头文件
Shot.h

代码中有三个问题。首先,您需要在CustomImageView.m实现文件中导入Shot.h:

#import "Shot.h"
不要简单地向前声明
快照
类:

@class Shot;
当编译器看到一个正向声明时,它会意识到该类的存在,但还不知道它的属性、声明的属性或方法——特别是,它不知道
Shot
有一个
-设定点:
实例方法

其次,您不是在创建快照的实例:

Shot *shot;
[shot SetShot:point];
这只声明
shot
是指向
shot
的指针,但没有分配/初始化。您应该创建一个对象,即:

Shot *shot = [[Shot alloc] init];
然后使用它:

[shot SetShot:point];
当您不再需要它时,请释放它:

[shot release];
虽然还不清楚创建一个镜头,设定它的点,然后释放它有什么好处。除非您的代码是一个人为的示例,否则您可能需要重新考虑这种行为

另外,您的
-SetPoint:
方法有一个指向
CGPoint
参数的指针,但您正在传递一个
CGPoint
值(即,不是指针)参数:

// point is not a pointer!
CGPoint point = [touch locationInView:self];
Shot *shot;
[shot SetShot:point];
我建议您将指针全部放下,即:

- (void)SetShot:(CGPoint)point;
{
    position = point;    
}

并且可能使用一个而不是手动实现的setter方法。

anon的答案是直接的问题,但这也是一个重要的错误。感谢您的回复。非常感谢!我不知道你能这么做。。。我的错。我以为你必须这样分配。非常感谢。你的回答对我帮助很大!我强烈建议你接受Bavariance解释得很好的答案。
- (void)SetShot:(CGPoint)point;
{
    position = point;    
}