简体   繁体   English

基于值数组将 NSString 转换为 NSInteger 的最佳方法是什么?

[英]Whats the best way to convert an NSString to an NSInteger based on an array of values?

I want to convert characters into integers based on predetermined values, for example:我想根据预定值将字符转换为整数,例如:

a = 0 
b = 1
c = 2
d = 3

etc... ETC...

Right now I'm doing it with an If/Else If, I just want to know if there is a faster/better way I should be doing it because the list of conversions may get quite long.现在我正在用 If/Else If 做这件事,我只是想知道是否有更快/更好的方法我应该这样做,因为转换列表可能会变得很长。

Here's what I'm using now:这是我现在正在使用的:

-(NSInteger)ConvertToInt:(NSString *)thestring {
    NSInteger theint;
    if([thestring isEqualToString:@"a"] == YES){
        theint = 0;
    } else if ([thestring isEqualToString:@"b"] == YES){
        theint = 1;
    } //etc...

    return theint;
}

This works fine, but as I said, if it makes more sense can I create an array with all the key/values then just run through that to return the integers?这工作正常,但正如我所说,如果它更有意义,我可以创建一个包含所有键/值的数组然后运行它以返回整数吗?

Please provide examples as I'm a beginner with Objective C/iOS.请提供示例,因为我是 Objective C/iOS 的初学者。 I come from Web languages.我来自 Web 种语言。

Thanks!谢谢!

EDIT: Thanks for the help everyone.编辑:感谢大家的帮助。 I used taskinoors answer but I replaced the NSDictionary which was giving error messages with this:我使用了 taskinoors answer 但我用这个替换了给出错误消息的 NSDictionary :

NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:0], @"a",
        [NSNumber numberWithInt:1], @"b",
        [NSNumber numberWithInt:2], @"c", nil];
unichar ch = [thestring characterAtIndex:0];
theint = ch - 'a';

Note that, 'a' with a single quote is character a , not string "a" .请注意,带单引号'a'是字符a ,而不是字符串"a"

If the values are not regular like your example then you can store all predefined values into a dictionary.如果值不像您的示例那样规则,那么您可以将所有预定义值存储到字典中。 For example:例如:

"a" = 5;
"b" = 1;
"c" = 102;

NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
    [NSNumber numberWithInt:1], [NSNumber numberWithInt:102], nil];
NSArray *keys = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];

theint = [[dic valueForKey:thestring] intValue];

If you wanted to keep some flexibility in what strings map to what integers, and your integers run from 0 to n-1 where you have n unique items in the array, you could do something like this:如果您想在哪些字符串 map 和哪些整数之间保持一定的灵活性,并且您的整数从 0 到 n-1,其中数组中有 n 个唯一项,您可以这样做:

-(NSInteger)ConvertToInt:(NSString *)thestring {
    NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", @"d", nil];
    NSInteger theint = [arr indexOfObject:thestring];
    return theint;
}

Now this will build the array each time, which would be very inefficient, the optimal way would be to build the array once in your class, and then just use a reference to that array with the indexOfObject method call.现在这将每次构建数组,这将是非常低效的,最佳方法是在 class 中构建一次数组,然后通过 indexOfObject 方法调用使用对该数组的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM