我发现这有点反直觉。如果我想做相当于scanf并将该输入分配给变量或数组,我该怎么做,然后在objective-c中打印它。
答案 0 :(得分:-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);
}