将objective-c typedef转换为其等效的字符串 [英] Convert objective-c typedef to its string equivalent

查看:33
本文介绍了将objective-c typedef转换为其等效的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在我的 .h 文件中声明了一个 typedef:

typedef enum {JSON,XML,原子,RSS格式类型;

我想构建一个将 typedef 的数值转换为字符串的函数.例如,如果消息 [self toString:JSON] 被发送;它会返回JSON".

函数看起来像这样:

-(NSString *) toString:(FormatType)formatType {//这里需要帮助返回 [];}

顺便说一句,如果我尝试这种语法

[self toString:FormatType.JSON];

将 typedef 值传递给该方法时,出现错误.我错过了什么?

解决方案

这实际上是一个 C 问题,并非特定于 Objective-C(它是 C 语言的超集).C 中的枚举表示为整数.因此,您需要编写一个函数,该函数返回给定枚举值的字符串.有很多方法可以做到这一点.一个字符串数组,这样枚举值可以用作数组或映射结构(例如 NSDictionary)的索引,将枚举值映射到字符串工作,但我发现这些方法不如使转换显式的函数清晰(和数组方法,尽管如果您的枚举值不是从 0 开始连续的,经典的 C 方法是危险的).像这样的事情会起作用:

- (NSString*)formatTypeToString:(FormatType)formatType {NSString *result = nil;开关(格式类型){案例JSON:结果 = @"JSON";休息;案例 XML:结果 = @"XML";休息;案例原子:结果 = @"原子";休息;案例RSS:结果 = @"RSS";休息;默认:[NSException raise:NSGenericException format:@"Unexpected FormatType."];}返回结果;}

您关于枚举值正确语法的相关问题是您只使用该值(例如 JSON),而不是 FormatType.JSON 语法.FormatType 是一种类型,枚举值(例如 JSONXML 等)是您可以分配给该类型的值.>

Assuming that I have a typedef declared in my .h file as such:

typedef enum {
  JSON,
  XML,
  Atom,
  RSS
} FormatType;

I would like to build a function that converts the numeric value of the typedef to a string. For example, if the message [self toString:JSON] was sent; it would return 'JSON'.

The function would look something like this:

-(NSString *) toString:(FormatType)formatType {
  //need help here
  return [];
}

Incidentally, if I try this syntax

[self toString:FormatType.JSON];

to pass the typedef value to the method, I get an error. What am I missing?

解决方案

This is really a C question, not specific to Objective-C (which is a superset of the C language). Enums in C are represented as integers. So you need to write a function that returns a string given an enum value. There are many ways to do this. An array of strings such that the enum value can be used as an index into the array or a map structure (e.g. an NSDictionary) that maps an enum value to a string work, but I find that these approaches are not as clear as a function that makes the conversion explicit (and the array approach, although the classic C way is dangerous if your enum values are not continguous from 0). Something like this would work:

- (NSString*)formatTypeToString:(FormatType)formatType {
    NSString *result = nil;

    switch(formatType) {
        case JSON:
            result = @"JSON";
            break;
        case XML:
            result = @"XML";
            break;
        case Atom:
            result = @"Atom";
            break;
        case RSS:
            result = @"RSS";
            break;
        default:
            [NSException raise:NSGenericException format:@"Unexpected FormatType."];
    }

    return result;
}

Your related question about the correct syntax for an enum value is that you use just the value (e.g. JSON), not the FormatType.JSON sytax. FormatType is a type and the enum values (e.g. JSON, XML, etc.) are values that you can assign to that type.

这篇关于将objective-c typedef转换为其等效的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆