Zac*_*ert 0 iphone methods xcode 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)SetShot:(CGPoint *)point;
{
position.x = point->x;
position.y = point->y;
}
@end
Run Code Online (Sandbox Code Playgroud)
当我尝试调用该SetShot方法时,xcode给了我这个警告:
方法-SetShot未找到(返回类型默认为id)
这是电话:
//CustomImageView.m
#import "CustomImageView.h"
@class Shot;
@implementation CustomImageView
-(id) initWithCoder:(NSCoder *)aDecoder
{
self.userInteractionEnabled = YES;
return self;
}
-(void) setInteraction
{
self.userInteractionEnabled = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
Shot *shot;
[shot SetShot:point];
}
- (void)dealloc
{
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,调用该方法时会出现致命错误.这是为什么?
小智 5
您的代码中存在三个问题.首先,您需要在CustomImageView.m实现文件中导入Shot.h:
#import "Shot.h"
Run Code Online (Sandbox Code Playgroud)
而不是简单地向前声明Shot类:
@class Shot;
Run Code Online (Sandbox Code Playgroud)
当编译器看到一个前向声明时,它会意识到该类的存在,但还不知道它的属性,声明的属性或方法 - 特别是,它不知道它Shot有一个-SetPoint:实例方法.
其次,你没有创建一个实例Shot:
Shot *shot;
[shot SetShot:point];
Run Code Online (Sandbox Code Playgroud)
这只声明shot是指向Shot但没有分配/初始化的指针.你应该创建一个对象,即:
Shot *shot = [[Shot alloc] init];
Run Code Online (Sandbox Code Playgroud)
然后使用它:
[shot SetShot:point];
Run Code Online (Sandbox Code Playgroud)
而且,当你不再需要它时,释放它:
[shot release];
Run Code Online (Sandbox Code Playgroud)
虽然目前尚不清楚创建镜头,设置其观点然后释放它的好处是什么.除非您的代码是一个人为的例子,否则您可能想重新考虑这种行为.
此外,您的-SetPoint:方法有一个指向CGPoint参数的指针,但您传递的CGPoint值(即不是指针)参数:
// point is not a pointer!
CGPoint point = [touch locationInView:self];
Shot *shot;
[shot SetShot:point];
Run Code Online (Sandbox Code Playgroud)
我建议你完全放弃指针,即:
- (void)SetShot:(CGPoint)point;
{
position = point;
}
Run Code Online (Sandbox Code Playgroud)
并且可能使用声明的属性而不是手动实现的setter方法.
| 归档时间: |
|
| 查看次数: |
3553 次 |
| 最近记录: |