我发现这有点违反直觉。如果我想做等效的 scanf 并将该输入分配给变量或数组,我该怎么做,然后在 Objective-c 中打印它。
问问题
96 次
1 回答
-1
您可以使用 NSMutableArray 并循环 NSString 测试中的每个字符,然后使用我已经测试过它..
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSString *str = [NSString stringWithFormat:@"masdasdada"];
int length = [str length];
for (int i = 0 ; i < length; i++) {
NSLog(@"%hu",[str characterAtIndex:i]);
unichar utf8char = [str characterAtIndex:i];
char chars[2];
int len = 1;
if (utf8char > 127) {
chars[0] = (utf8char >> 8) & (1 << 8) - 1;
chars[1] = utf8char & (1 << 8) - 1;
len = 2;
} else {
chars[0] = utf8char;
}
NSString *string = [[NSString alloc] initWithBytes:chars
length:len
encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
[arr addObject:[NSString stringWithFormat:@"%@",string]];
}
for (NSMutableString *s in arr) {
NSLog(@"%@",s);
}
于 2012-07-19T11:41:30.650 回答