Created
August 8, 2011 20:17
-
-
Save mikeash/1132620 to your computer and use it in GitHub Desktop.
Namespaced constants in C
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/Foundation.h> | |
// .h file | |
struct MyConstantsStruct | |
{ | |
NSString *foo; | |
NSString *bar; | |
int baz; | |
}; | |
extern const struct MyConstantsStruct MyConstants; | |
// .m file | |
const struct MyConstantsStruct MyConstants = { | |
.foo = @"foo", | |
.bar = @"bar", | |
.baz = 42 | |
}; | |
// user | |
int main(int argc, char **argv) | |
{ | |
[NSAutoreleasePool new]; | |
NSLog(@"%@ %@ %d", MyConstants.foo, MyConstants.bar, MyConstants.baz); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ARC does allow it, but unfortunately you must put
__unsafe_unretained
before all of the object pointer struct members to make ARC happy.