私はCocoaを新しく使いました。私が使用しているスタイルとコードが自分の目的に合ったものであることを確認したいと思います。Objective-Cコーディングスタイル(むしろセマンティクス)
具体的には、ヘッダー(a)
)には、@interface
以外の変数を設定した場合の影響はありますか?
第2に、インスタンス(点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
Btw、これはXcodeとは関係ありません。 –
Objective-CはまだCです。C言語を学ぶことをお勧めします。これは小さな言語です。 Kernighan&Ritchieによる* The C Programming Language *を参照してください。 (トピックに関する私の投稿:http://qualitycoding.org/objective-c-is-c/) –