Tag: enums

Print string representation of an enum, NSLog

I am trying to NSLog some enums I have. For example this piece of code prints the integer representation of the enum, but I want it to output the actual string name, in this case MON. How can I do that? #import <Foundation/Foundation.h> int main(void) { typedef enum { SUN, MON, TUES } DAYS; DAYS […]

Using enums as parameters in an external file in Objective-C?

I have an enum named RandomEnum in file foo.h: // foo.h typedef enum RandomEnum { ran_1 = 0, ran_2 } RandomEnum; In another file, bar.h, I’m trying to use RandomEnum as a parameter type: // bar.h #import “foo.h” @interface bar : NSObject {} -(RandomEnum)echo:(RandomEnum)ran; @end However, the compiler doesn’t seem to recognize RandomEnum. Is doing […]

Using enum types as properties in Objective C

I’m a veteran .NET developer making my first foray into Objective C programming. I’m having difficulty with a property of an enum type. Some context… I have an class header and enum like this: typedef enum { Open, Unavailable, Unknown } LocationStatus; @interface Location : NSObject { LocationStatus status; } @property (nonatomic) LocationStatus status; @end […]

Objective-C / C giving enums default values

I read somewhere about giving enums default values like so: typedef enum { MarketNavigationTypeNone = 0, MarketNavigationTypeHeirachy = 1, MarketNavigationTypeMarket = 2 } MarketNavigationLevelType; .. but i can’t remember the value of doing this. If i don’t give them default values – and then someone later on reorders the enum – what are the risks? […]

How to determine if undocumented value for NS_ENUM with Swift 1.2

For instance, following NS_Enum is defined… typedef NS_ENUM(NSInteger, Type) { TypeNone = 0, TypeA = 1, } var x = 2 if let type: Type = Type(rawValue: x) { // Swift 1.2 executes this block. } else { // Previous swift executes this block. } How can I determine if x is defined on NS_ENUM […]

How to use enums with bit flags

I have an enum declaration using bit flags and I cant exactly figure out on how to use this. enum { kWhite = 0, kBlue = 1 << 0, kRed = 1 << 1, kYellow = 1 << 2, kBrown = 1 << 3, }; typedef char ColorType; I suppose to store multiple colors in […]

Objective-C Enumeration, NS_ENUM & NS_OPTIONS

What’s the correct way to create an enumeration with a specific type in Objective-C? How does NS_ENUM and NS_OPTIONS work? NS_OPTIONS are used for masks, like NSAutoresizing? Thanks. Code from NSObjCRuntime.h #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type #define NS_OPTIONS(_type, _name) _type _name; enum : _type

enum values: NSInteger or int?

tl;dr Version How are the data types of an enum’s constants guaranteed to be NSUInteger instead of unsigned int when declaring an enum thusly: enum { NSNullCellType = 0, NSTextCellType = 1, NSImageCellType = 2 }; typedef NSUInteger NSCellType; The typedef to NSUInteger does not appear to be tied to the enum declaration in any […]

looping through enum values

Is it possible to loop through enum values in Objective-C?

Declaring and checking/comparing (bitmask-)enums in Objective-C

You know in Cocoa there is this thing, for example you can create a UIView and do: view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; I have a custom UIView with multiple states, which I have defined in an enum like this: enum DownloadViewStatus { FileNotDownloaded, FileDownloading, FileDownloaded }; For each created subview, I set its tag: subview1.tag […]