如何将 NSNumber 转换为 NSString
所以我有一个带有 NSNumber
和 NSString
的 NSArray
“myArray”。我需要它们在另一个 UIView
中,所以我像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"];
subjectText 有效。 但是我怎样才能从中获取 NSNumber
呢? (我实际上需要它们作为字符串......) 我会将 NSString
从 NSNumber
转换出来,如下所示: NSString *blah = [NSNumber intValue]
。但我不知道如何在上面的代码中设置它......
So I have an NSArray
"myArray" with NSNumber
s and NSString
s. I need them in another UIView
so i go like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"];
The subjectText works.
But how can I get the NSNumber
s out of it? (I actually need them as strings...)
I would convert a NSString
out of a NSNumber
like this:NSString *blah = [NSNumber intValue]
. But I don't know how to set it up in the code above...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试:
Try:
你可以这样做:
You can do it with:
或尝试
NSString *string = [NSString stringWithFormat:@"%d", [NSNumber intValue], nil];
or try
NSString *string = [NSString stringWithFormat:@"%d", [NSNumber intValue], nil];
有趣的是,如果 NSNumber 成为字符串的一部分,它会自动转换为字符串。我认为没有记录。
尝试以下操作:
它将打印:
Works on those Mac and iOS
This one does not work:
The funny thing is that NSNumber converts to string automatically if it becomes a part of a string. I don't think it is documented.
Try these:
It will print:
Works on both Mac and iOS
This one does not work:
在 Swift 中你可以这样做
In Swift you can do like this
在 Swift 3.0
我们可以获取 String 中的数字值。
In Swift 3.0
We can get the number value in String.