我是 Cocoa 的新手,我想确保我使用的样式和代码的格式适合我的目的。
特别是在标题中(在标记处a)
)将变量设置在外部有什么影响@interface
?
其次,使用实例中的变量(在点b)
)而不在类声明中注册它会产生什么影响?
头文件:
#import <UIKit/UIKit.h>
///////////// a) Is this good use?
int myint;
/////////////
@interface InstancecheckViewController : UIViewController
- (IBAction)plusone:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *counting;
@end
执行:
#import "InstancecheckViewController.h"
@interface InstancecheckViewController ()
@end
@implementation InstancecheckViewController
@synthesize counting;
///////////////////// b) is this good use?
- (void)resetit {
myint = 0;
}
/////////////////////
- (IBAction)plusone:(id)sender {
myint ++;
if (myint >10){
[self resetit];
}
NSString* myNewString = [NSString stringWithFormat:@"%d", myint];
counting.text = myNewString;
}
@end
编辑
感谢大家的评论。我想我现在已经正确地重新定义了 .h 中的实例和整数
@interface instancecheckViewController : UIViewController
{
@private
int myint;
}
- (IBAction)plusone:(id)sender;
- (void)resetIt;
@end