【问题标题】:how to use NS_ENUM如何使用 NS_ENUM
【发布时间】:2019-06-13 04:26:56
【问题描述】:

我想使用 NS_ENUM。我在我的 .m 文件中编写了以下代码。现在当我的cellForRowAtIndexPath 被调用时。我得到了索引路径。现在对应于该索引路径,我想提取与之关联的字符串。例如对于索引路径 0,我想提取图像。

typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
{

    Images = 0,
    Videos = 1,
    Documents = 2,    
    Audios = 3

};

【问题讨论】:

    标签: ios objective-c enums


    【解决方案1】:

    在这种情况下,我们通常做的是将枚举中的最后一项始终保留为“count”或“last”。以您为例:

    typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
    {
        TABLE_SECTION_Images,
        TABLE_SECTION_Videos,
        TABLE_SECTION_Documents,
        TABLE_SECTION_Audios,
    
        TABLE_SECTION_Count
    };
    

    我们不指定值,因为这可能会破坏逻辑。但是您可以重新排序项目,也可以通过将项目放在“计数”之后来弃用它们。

    它的用法看起来像这样:

    @interface ViewController()<UITableViewDelegate, UITableViewDataSource>
    
    @end
    
    @implementation ViewController
    
    - (NSString *)tableSectionName:(TABLE_SECTION_ITEMS)item {
        switch (item) {
            case TABLE_SECTION_Images: return @"Images";
            case TABLE_SECTION_Videos: return @"Videos";
            case TABLE_SECTION_Documents: return @"Documents";
            case TABLE_SECTION_Audios: return @"Audios";
            case TABLE_SECTION_Count: return nil;
        }
    }
    
    - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return TABLE_SECTION_Count;
    }
    
    - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.textLabel.text = [self tableSectionName:indexPath.row];
        return cell;
    }
    
    @end
    

    所以行数只是TABLE_SECTION_Count,您可以自然地在NSInteger[self tableSectionName:indexPath.row] 中看到的枚举之间进行转换。

    当然,字符串的映射仍然需要像tableSectionName 那样完成。它不是闲置的,但现在更易于管理;

    当你添加一个新的枚举值(比如TABLE_SECTION_Documents2)时,你会在tableSectionName 中得到 n 个错误,你需要添加一个新的案例(或者更确切地说是一个与之相关的错误,说 Control may reach end of non -void 函数)。因此,作为开发人员,您必须像这样填写:

    - (NSString *)tableSectionName:(TABLE_SECTION_ITEMS)item {
        switch (item) {
            case TABLE_SECTION_Images: return @"Images";
            case TABLE_SECTION_Videos: return @"Videos";
            case TABLE_SECTION_Documents: return @"Documents";
            case TABLE_SECTION_Documents2: return @"Documents";
            case TABLE_SECTION_Audios: return @"Audios";
            case TABLE_SECTION_Count: return nil;
        }
    }
    

    这里的另一个好处是我们可以拥有两次“文档”。 而要弃用一个项目,我们需要做的就是将枚举移到计数之后:

    typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
    {
        TABLE_SECTION_Images,
        TABLE_SECTION_Videos,
        TABLE_SECTION_Documents2,
        TABLE_SECTION_Audios,
    
        TABLE_SECTION_Count, // Always keep last
    
        // Deprecated items
        TABLE_SECTION_Documents
    };
    

    现在无需更改任何代码,旧的“文档”将不会在您的表格视图中列出。不理想,但仍然很整洁。

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2012-12-14
      相关资源
      最近更新 更多