4

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;

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So the anonymous enum is only declaring symbolic names for constant vales (similar to #define) and the typedef is related to the enum by the naming convention only (UIViewAutoresizing___)? I think that makes sense
@Keith: Yes, thats correct. The compiler enforcement of passing the right types to the enum isn't desired in these cases, it even is counter intuitive. In cases were it is desired, Apple uses a typedefed enum.
3

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

1 Comment

Err, its vice versa. An int on 32 bit system, a long on 64 bit systems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.