Abhinav
Abhinav

Reputation: 38162

define Vs static constant Vs NSString -- Memory point of view

What is the best bet between:

#define kYes @"Yes"

AND

static NSString *const kYes = @"Yes";

And

NSString *kYes = @"Yes";

from memory consumption point of view. Keeping in mind that there will be thousands of constants in the application.

Upvotes: 11

Views: 2620

Answers (1)

reflog
reflog

Reputation: 7645

From the memory stand point - static variable is better, since it's referenced once. The #define will insert the string into all the occurrences, by that - multiplying the memory usage... (that is unless GCC optimizing same constant string occurrences, which it actually might. in that case - there is no difference)

Upvotes: 11

Related Questions