【发布时间】:2011-01-01 18:05:22
【问题描述】:
我正在动态构建一个 url,需要一种将 NSNumber 的 intValue 添加到其末尾的方法。
NSNumber *hatId = [NSNumber NumberWithInt:25]
NSMutableString* theUrlString = [[NSMutableString alloc] init];
[theUrlString appendFormat:@"http://www.website.com/SelectHat/%d", [hatid intValue]];
NSURL *url = [NSURL URLWithString:theUrlString];
完成上述操作后,简单的单元测试或 NSLog 将显示结果看起来有效
http://www.website.com/SelectHat/25
但我的问题是——以这种方式连接会改变 NSString,所以它不再像我期望的那样是一个有效的字符串吗?这也是构建需要来自 NSNumber 的 intValue 的字符串的“好”方法吗?
更新
我只是想确保 %d 内联没有问题,并且 NSString 符合我的预期。我遇到的问题有点复杂,所以我会把它留到另一个问题上。感谢您对 NSURL / NSNumber / int 的回复,因为我仍在学习很多关于 Objective-c 的知识
【问题讨论】:
-
也不需要使用
alloc/init。只需theUrlString = [NSString stringWithFormat: @"..... %d", hadId]。
标签: iphone objective-c concatenation