0

我想创建一个计数器,每次它满足我代码的 if 块中的条件时,它都会更新一个。为此,我实现了单音,而不是在 .h 文件中定义全局变量…………</p>

@interface MONObject : NSObject {
int *counterplus;
}
@property(nonatomic) int *counterplus;
+(MONbject*) sharedinstance;
@end

在 .m 文件中

static MON object * sharedinstance;
@implementation MONbject;
@synthesize counterplus;

+(MONbject*)sharedinstance
{
if(!sharedinstance){
sharedinstance=[[MONObject alloc]init];
}
return sharedinstance
}
-(MONObject*) int
{
self=[super init];
if(0!=self){
counterplus=0;
}
return self;
}
@end

我把上面的称为……</p>

if(condition){
[MONObject sharedinstance].counterplus++;
}

当我尝试使用 打印它的值时NSLog,程序停止并给出此错误“单步执行直到从函数 objc_msgSend 退出,该函数没有行号信息。警告远程故障回复:E37

我一点头绪都没有。我尝试通过将全局变量定义为 extern 并尝试递增来使用它。但它给出了同样的错误。有什么办法可以做到这一点?你能指出其中的错误吗?

4

2 回答 2

0

这将是 -

@property(nonatomic) int counterplus;

代替 -

@property(nonatomic) int *counterplus;

对于记录 int 值,请使用 -

NSLog(@"%d", counterplus);
于 2012-05-30T07:36:41.267 回答
0

应该是

@interface MONObject : NSObject {
int counterplus;
}
@property(nonatomic, assign) int counterplus;
+(MONbject*) sharedinstance;
@end

希望能帮助到你。快乐编码:)

于 2012-05-30T07:39:49.037 回答