【问题标题】:Is it possible to scope enums to a class in Objective-C?是否可以将枚举范围限定为 Objective-C 中的类?
【发布时间】:2012-11-20 19:25:20
【问题描述】:

我是 Objective-C 的新手,并试图找出枚举。有没有办法将枚举范围限定为一个类,以便另一个类可以使用这些值?像这样的:

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

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

虽然这显然行不通。还是有更好的方法来做枚举?

编辑:我想我的措辞不清楚,但我不是在问如何声明枚举。我知道将它们放在文件的顶部是可行的。我在问是否有办法确定它们的范围,以便这些值对整个文件来说不是全局的。

【问题讨论】:

  • ObjectiveC 在命名空间方面做得不好......这就是为什么在类名前加前缀的做法如此普遍。

标签: objective-c enums


【解决方案1】:

您必须为您的公共枚举添加前缀。只需将枚举定义放在类的标题中即可。

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

@interface ClassA {
    ClassAStatus status;
}
@end


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

@interface ClassB {
    ClassBStatus status;
}
@end

这就是苹果的做法。

或者您可以使用新样式:

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

【讨论】:

    【解决方案2】:

    只需将enum 放在 .h 的顶部,就像 Apple 在 UITableView.h 中所做的那样,例如:

    //
    //  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
    };
    

    您可能认识其中一些名称,但您可能不知道它们实际上是 Apple 头文件中公共 enum 的一部分。

    【讨论】:

    • 仅供参考,原始问题使用 NS_ENUM 的较旧语法,而此答案使用较新的语法。有关新语法和功能的更多信息,请阅读 Mattt Thompson 的 NS_ENUM & NS_OPTIONS 博客文章。
    【解决方案3】:

    只是想添加到我通常这样写的@DrummerB 的答案。 camelCase 中的命名空间,然后是大写的常量。

    typedef enum {
        ClassAStatus_ACCEPTED,
        ClassAStatus_REJECTED
    } ClassAStatus;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 2019-01-24
      • 2014-12-20
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      相关资源
      最近更新 更多