枚举NSString的最佳方式 [英] Best way to enum NSString
问题描述
猜猜我犯了一个错误。
这是上面提到的新功能:
typedef枚举语言:NSUInteger {
ObjectiveC,
Java,
Ruby,
Python,
Erlang
}语言;
它只是Xcode 4.4中的枚举的新语法,但是我觉得我们可以这么愚蠢将NSUInteger交换为NSString。
所以这里是我发现的方式:
a href =http://longweekendmobile.com/2010/12/01/not-so-nasty-enums-in-objective-c/> http://longweekendmobile.com/2010/12/01/not-令人讨厌的enum-in-objective-c /
//将其放在.h文件中, @interface块外
typedef枚举{
JPG,
PNG,
GIF,
PVR
} kImageType;
#define kImageTypeArray @JPEG,@PNG,@GIF,@PowerVR,nil
...
// Place这个在.m文件里面的@implementation块
//将枚举转换成字符串
- (NSString *)的方法imageTypeEnumToString :((kImageType))enumVal
{
NSArray * imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
return [imageTypeArray objectAtIndex:enumVal];
}
//从NSStrings的NSArray获取int值的方法
- (kImageType)imageTypeStringToEnum :( NSString *)strVal
{
NSArray * imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
NSUInteger n = [imageTypeArray indexOfObject:strVal];
if(n <1)n = JPG;
return(kImageType)n;
}
FYI。第二个示例代码的原始作者为枚举处理创建了一个类别。只是添加到你自己的NSArray类定义的东西。
@interface NSArray(EnumExtensions)
- (NSString *)stringWithEnum:(NSUInteger)enumVal;
- (NSUInteger)enumFromString:(NSString *)strVal default:(NSUInteger)def;
- (NSUInteger)enumFromString:(NSString *)strVal;
@end
@implementation NSArray(EnumExtensions)
- (NSString *)stringWithEnum:(NSUInteger)enumVal
{
return [self objectAtIndex:enumVal];
}
- (NSUInteger)enumFromString:(NSString *)strVal默认值:(NSUInteger)def
{
NSUInteger n = [self indexOfObject:strVal];
if(n == NSNotFound)n = def;
return n;
}
- (NSUInteger)enumFromString:(NSString *)strVal
{
return [self enumFromString:strVal default:0];
}
@end
Im digging for ways to enum objc object such as NSString, I remember there a new feature in a version of Xcode4+ which offering a new way to enum , but not clearly. Anyone know that?
OK, I answered myself. Guess I make a mistake.
This is the new feature I mentioned above:
typedef enum Language : NSUInteger{
ObjectiveC,
Java,
Ruby,
Python,
Erlang
}Language;
It's just a new syntax for enum in Xcode 4.4, but I'm so foolish to think we can exchange "NSUInteger" to "NSString".
So here is the way I found that works:
http://longweekendmobile.com/2010/12/01/not-so-nasty-enums-in-objective-c/
// Place this in your .h file, outside the @interface block
typedef enum {
JPG,
PNG,
GIF,
PVR
} kImageType;
#define kImageTypeArray @"JPEG", @"PNG", @"GIF", @"PowerVR", nil
...
// Place this in the .m file, inside the @implementation block
// A method to convert an enum to string
-(NSString*) imageTypeEnumToString:(kImageType)enumVal
{
NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
return [imageTypeArray objectAtIndex:enumVal];
}
// A method to retrieve the int value from the NSArray of NSStrings
-(kImageType) imageTypeStringToEnum:(NSString*)strVal
{
NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
NSUInteger n = [imageTypeArray indexOfObject:strVal];
if(n < 1) n = JPG;
return (kImageType) n;
}
FYI. The original author of the second example code created a category for enum handling. Just the thing for adding to your very own NSArray class definition.
@interface NSArray (EnumExtensions)
- (NSString*) stringWithEnum: (NSUInteger) enumVal;
- (NSUInteger) enumFromString: (NSString*) strVal default: (NSUInteger) def;
- (NSUInteger) enumFromString: (NSString*) strVal;
@end
@implementation NSArray (EnumExtensions)
- (NSString*) stringWithEnum: (NSUInteger) enumVal
{
return [self objectAtIndex:enumVal];
}
- (NSUInteger) enumFromString: (NSString*) strVal default: (NSUInteger) def
{
NSUInteger n = [self indexOfObject:strVal];
if(n == NSNotFound) n = def;
return n;
}
- (NSUInteger) enumFromString: (NSString*) strVal
{
return [self enumFromString:strVal default:0];
}
@end
这篇关于枚举NSString的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!