2013-09-30 36 views
0

我想要一个字符串(@“12345”)和提取器每个单独的字符并转换为它的十进制等效。将字符串转换为字符到整数

@ “1”= 1 @ “2”= 2 等

以下是我有这么远:

... 
[self ArrayOrder:@"1234"]; 
... 

-(void)ArrayOrder(Nsstring *)Directions 
{ 



    NSString *singleDirections = [[NSString alloc] init]; 

    //Loop Starts Here 

    *singleDirection = [[Directions characterAtIndex:x] intValue]; 

    //Loop ends here 


} 

我已经收到类型错误。

回答

0

您的代码的问题是[Directions characterAtIndex:x]返回一个unichar,这是一个Unicode字符。

相反,你可以使用NSRange和子让每个号码出字符串的:

NSRange range; 
range.length = 1; 
for(int i = 0; i < Directions.length; i++) { 
    range.location = i; 
    NSString *s = [Directions substringWithRange:range]; 
    int value = [s integerValue]; 
    NSLog(@"value = %d", value); 
} 

另一种方法是使用/ 10% 10去从单独的字符串中的每个号码。如:

NSString* Directions = @"1234"; 
int value = [Directions intValue]; 
int single = 0; 
while(value > 0) { 
    single = value % 10; 
    NSLog(@"value is %d", single); 
    value /= 10; 
} 

然而,经过您的字符串倒退。