Derek Shockey
Derek Shockey

Reputation: 1856

Is it possible to scope enums to a class in Objective-C?

I'm new to Objective-C and trying to figure out enums. Is there a way to scope an enum to a class so that the values could be used by another class? Something like this:

@interface ClassA {
    typedef enum {
        ACCEPTED,
        REJECTED
    } ClassAStatus;
}
@end

@interface ClassB {
    typedef enum {
        ACCEPTED,
        REJECTED
    } ClassBStatus;
}
@end

Though that does not work, obviously. Or is there a better way to do enums altogether?

Edit: I guess my wording wasn't clear, but I'm not asking how to declare enums. I'm aware that putting them at the top of the file works. I'm asking if there's a way to scope them so the values aren't global to the entire file.

Upvotes: 4

Views: 10249

Answers (3)

Fredrik Johansson
Fredrik Johansson

Reputation: 1301

Just want to add to @DrummerB's answer that I usually write like this. The namespace in camelCase, and then the constant in upper case.

typedef enum {
    ClassAStatus_ACCEPTED,
    ClassAStatus_REJECTED
} ClassAStatus;

Upvotes: 2

woz
woz

Reputation: 11004

Just put the enum right at the top of your .h, like Apple does in UITableView.h, for example:

//
//  UITableView.h
//  UIKit
//
//  Copyright (c) 2005-2012, Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIScrollView.h>
#import <UIKit/UISwipeGestureRecognizer.h>
#import <UIKit/UITableViewCell.h>
#import <UIKit/UIKitDefines.h>

typedef NS_ENUM(NSInteger, UITableViewStyle) {
    UITableViewStylePlain,                  // regular table view
    UITableViewStyleGrouped                 // preferences style table view
};

typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
    UITableViewScrollPositionNone,
    UITableViewScrollPositionTop,    
    UITableViewScrollPositionMiddle,   
    UITableViewScrollPositionBottom
};                // scroll so row of interest is completely visible at top/center/bottom of view

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
    UITableViewRowAnimationFade,
    UITableViewRowAnimationRight,           // slide in from right (or out to right)
    UITableViewRowAnimationLeft,
    UITableViewRowAnimationTop,
    UITableViewRowAnimationBottom,
    UITableViewRowAnimationNone,            // available in iOS 3.0
    UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
    UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you
};

You probably recognize some of these name, but you may not have been aware they were actually apart of a public enum in Apple's header files.

Upvotes: 2

DrummerB
DrummerB

Reputation: 40211

You have to prefix your public enums. Simply put the enum definition in the header of your class.

// ClassA.h
typedef enum {
    ClassAStatusAccepted,
    ClassAStatusRejected
} ClassAStatus;

@interface ClassA {
    ClassAStatus status;
}
@end


// ClassB.h
typedef enum {
    ClassBStatusAccepted,
    ClassBStatusRejected
} ClassBStatus;

@interface ClassB {
    ClassBStatus status;
}
@end

This is how Apple does it.

Or you could use the new style:

// UIView.h
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    UIViewAnimationCurveEaseInOut,         // slow at beginning and end
    UIViewAnimationCurveEaseIn,            // slow at beginning
    UIViewAnimationCurveEaseOut,           // slow at end
    UIViewAnimationCurveLinear
};

Upvotes: 8

Related Questions