【问题标题】:Is it possible to use a enum like UITableViewCellStyle as parameter of a method?是否可以使用像 UITableViewCellStyle 这样的枚举作为方法的参数?
【发布时间】:2010-10-26 09:16:05
【问题描述】:

我想要一个枚举作为我的函数的参数。这行得通吗?

(UIFont*) myMethodName:(UITableViewCellStyle) cellStyle {
    //...
    if (cellStyle == UITableViewCellStyleValue2)
        // ...
}

那我就这样调用方法

UIFont *resultFont = [self myMethodName:UITableViewCellStyleSubtitle];

只应允许以下参数: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

有可能吗?

【问题讨论】:

    标签: iphone objective-c cocoa-touch enums


    【解决方案1】:
    • 这行得通吗? → 是的

    • 只应允许以下参数: → 不,不能将输入限制为仅这些值,即

      UIFont *resultFont = [self myMethodName:12345];
      

      仍会编译(假设您没有使用 Objective-C++)。

    【讨论】:

      【解决方案2】:

      当然:

      typedef enum _MyType {
          type_a = -1,
          type_b = 0,
          type_c = 1,
      } MyType;
      
      ...
      
      - (void) someMethod:(MyType)type {
          if (type == type_a) ...
      }
      

      【讨论】:

        【解决方案3】:

        是的,有可能。

        (这感觉像是一个不必要的简短回答,但我想不出还有什么要补充的!)

        【讨论】:

        • 这就是我想知道的全部;)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 2013-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多