Keith
Keith

Reputation: 2628

C enum definition in iOS framework class

I am browsing the UIKit framework header files and I see a lot of instances where an anonymous enum is defined followed by a seemingly related typedef. Can someone explain what is going on here?

Does the UIViewAutoresizing type somehow (implicitly) refer to the enum declared in the previous statement? How would you refer to that enum type?

enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

Upvotes: 4

Views: 2545

Answers (2)

v1Axvw
v1Axvw

Reputation: 3054

I think you've only got one thing wrong here: NSUInteger is not an objective-c object it is an unsigned int on 32-bit systems and an unsigned long on 64-bit systems. So actually this is what's going on:

typedef unsigned int UIViewAutoresizing;

or

typedef unsigned long UIViewAutoresizing;

For more reference I add this:

#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

Source: CocoaDev

Upvotes: 3

JustSid
JustSid

Reputation: 25318

The thing is that those are flags intended to use as bit mask, which leads to problems with enums. For example, if it would look like this:

typedef enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
} UIViewAutoresizing;

And you would call setAutoresizingMask: on a view with UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight, the compiler will complain and you must explicitly typecast it back to the UIViewAutoresizing type. The NSUInteger however can take the bit mask.

Beside that, everything that lef2 said about NSUInteger not being an ObjC object.

Upvotes: 3

Related Questions