NSString To NSNumber
所以这个问题困扰我太久了。我有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int i = [[alertView textFieldAtIndex:0].text intValue]; NSNumber *number = [NSNumber numberWithInt:[[alertView textFieldAtIndex:0].text integerValue]]; int number = [[dict objectForKey:@"integer"] intValue]; NSLog(@"text = %@", [alertView textFieldAtIndex:0].text); NSString *alertText = [alertView textFieldAtIndex:0].text; NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterNoStyle]; NSNumber * myNumber = [f numberFromString:[alertView textFieldAtIndex:0].text]; NSNumber *number = @([alertText intValue]); NSString *string = @"54"; NSNumber *number = @([string intValue]); NSLog(@"here we are: %i", number); |
一旦看到这个,
1 2 3 4 5 | NSString *string = @"123"; NSNumber *aNum = [NSNumber numberWithInteger: [string integerValue]]; NSLog(@"%@",aNum);//NSString to NSNumber NSInteger number=[string intValue]; NSLog(@"%i",number);//NSString to NSInteger |
1 2 3 | NSString *string = @"54"; NSNumber *number = @([string intValue]); NSLog(@"here we are: %i", number); |
请尝试使用以下方法:
1 | NSLog(@"here we are: %@", number); |
因为您正在转换为nsnumber(对象)。应该在nslog语句中使用对象说明符%@。
@Aaron Wojnowski,
如果需要,可以使用
1 2 3 | NSNumberFormatter *formatString = [[NSNumberFormatter alloc] init]; stringValue = textfield.text; NSNumber *reqNumber = [formatString numberFromString:stringValue]; |
这是一个整数和使用nsnumber文本的示例。如果字符串包含浮点,也可以使用
1 2 | NSString *string = @"54"; NSNumber *number = @([string intValue]); |