- 型情報がない
- ヘッダーファイルインポート条件によっては、名前衝突により開発者が意図しないファイルにおいても定数置換が行われてしまう危険性がある
実際のところ、私は下記のように定数定義を行っている。
外部に公開する定数
- extern constを付与
- 定数にはプレフィックスとしてクラス名をつける(名前衝突を避けるため)
内部でのみ使用する定数
- static constを付与
- 定数にはプレフィックスとしてkをつける(よく見かけるパターン)
コーディング例
以下は、BLGClassAクラスにおいて外部に公開する定数を定義、BLGClassBクラスにおいて内部で使用する定数を定義、して利用する場合の例。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
@interface BLGClassA : NSObject | |
extern const NSInteger BLGClassAIntegerConstant; | |
extern NSString *const BLGClassAStringConstant; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "BLGClassA.h" | |
const NSInteger BLGClassAIntegerConstant = 3; | |
NSString *const BLGClassAStringConstant = @"hogePublic"; | |
@interface BLGClassA () | |
@end | |
@implementation BLGClassA | |
// You can omit init method beacuse this is implicitly implemented. | |
//- (instancetype)init { | |
// if (self = [super init]) { | |
// } | |
// return self; | |
//} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
@interface BLGClassB : NSObject | |
- (void)doSomething; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "BLGClassB.h" | |
#import "BLGClassA.h" | |
static const NSInteger kLocalIntegerConstant = 30; | |
static NSString *const kLocalStringConstant = @"fooLocal"; | |
@interface BLGClassB () | |
@end | |
@implementation BLGClassB | |
// You can omit init method beacuse this is implicitly implemented. | |
//- (instancetype)init { | |
// if (self = [super init]) { | |
// } | |
// return self; | |
//} | |
- (void)doSomething | |
{ | |
NSLog(@"Local Constants: %ld %@", (long)kLocalIntegerConstant, kLocalStringConstant); | |
NSLog(@"Imported Constants: %ld %@", (long)BLGClassAIntegerConstant, BLGClassAStringConstant); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
#import "BLGClassB.h" | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
BLGClassB *b = [BLGClassB new]; | |
[b doSomething]; | |
// Local Constants: 30 fooLocal | |
// Imported Constants: 3 hogePublic | |
} | |
return 0; | |
} |
動作確認
コンパイル/リンクして実行。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ clang -o BlogspotConstantSample -Wmost main.m BLGClassA.m BLGClassB.m -framework Foundation | |
$ ./BlogspotConstantSample | |
Local Constants: 30 fooLocal | |
Imported Constants: 3 hogePublic |
0 件のコメント:
コメントを投稿