LoginSignup
18
17

More than 5 years have passed since last update.

objective-cで (const修飾子で) 配列を定数としたいとき

Posted at

例として、文字列定数を定数配列にしてみる。

ヘッダ

Const.h
extern NSString *const SOME_CONSTANT_ARRAY[];

extern NSUInteger const SIZE_OF_SOME_CONSTANT_ARRAY;

実装

Const.m
NSString *const SOME_CONSTANT_ARRAY[] = { @"hoge", @"fuga", @"piyo" };

NSUInteger const SIZE_OF_SOME_CONSTANT_ARRAY = 3;

使いたいとき

SomeExternalClass.m
NSArray *array = [[NSArray alloc] init];
for(int i = 0; i < SIZE_OF_SOME_CONSTANT_ARRAY; i++)
    [array addObject:SOME_CONSTANT_ARRAY[i]];

// arrayに対する処理・・・

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17