Indexing 将字符串转换为字符转换为整数

Indexing 将字符串转换为字符转换为整数,indexing,integer,character,decimal,Indexing,Integer,Character,Decimal,我试图获取一个字符串(@“12345”)和每个字符的提取器,并将其转换为十进制等效值 @“1”=1 @"2" = 2 等等 以下是我目前掌握的情况: ... [self ArrayOrder:@"1234"]; ... -(void)ArrayOrder(Nsstring *)Directions { NSString *singleDirections = [[NSString alloc] init]; //Loop Starts Here *singleDi

我试图获取一个字符串(@“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


 }

我一直收到类型错误。

您的代码的问题是
[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);
}
但是,这会向后穿过字符串

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