连接三个 NSString
对了,我有三个 NSString,我想将它们组合在一起形成一个长 nsstring(用作 URL)。我使用了 stringByAppedingString,它可以让我将其中两个放在一起,但我不知道如何将三个放在一起。基本上我想要最终得到的是 http://graph.facebook.com/517418970/ picture?type=large 但我需要它们在三个单独的组件中,这样我就可以更改 URl 中的数字
@implementation FacebookPicturesViewController
- (IBAction) nextImagePush {
NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";
NSString *url = [prefix stringByAppendingString:suffix];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[imageView setImage:img];
imageCount++;
if (imageCount >= [imageArray count]){
imageCount = 0;
}
}
- (void) viewDidLoad {
imageArray = [[NSArray alloc] initWithObjects:@"image1", @"image2", nil];
imageCount = 0;
}
Right i have three NSStrings that i want to put together to make one long nsstring (to use as a URL). I have used stringByAppedingString which lets me put two of the together but i do not know how to put three together. Basically what i want to end up with is http://graph.facebook.com/517418970/picture?type=large but i need them in three separate components so i can change the number in the URl
@implementation FacebookPicturesViewController
- (IBAction) nextImagePush {
NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";
NSString *url = [prefix stringByAppendingString:suffix];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[imageView setImage:img];
imageCount++;
if (imageCount >= [imageArray count]){
imageCount = 0;
}
}
- (void) viewDidLoad {
imageArray = [[NSArray alloc] initWithObjects:@"image1", @"image2", nil];
imageCount = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需两步即可完成:
或者,您可以使用以下格式:
You could just do it in two steps:
Alternatively, you could use a format:
作为当您事先不知道需要组合多少个字符串时的通用解决方案,您可以将它们放入 NSArray 中并使用该数组将它们连接在一起。所以在这种情况下:
As a general solution for when you don't know ahead of time just how many strings you have to combine, you can stick them in an NSArray and use the array to join them together. So in this case:
您想要
+ (id)stringWithFormat:(NSString *)format, ...
完整文档为 此处
You want
+ (id)stringWithFormat:(NSString *)format, …
Full docs are here
我会像这样处理串联:
I'd handle the concatenation like this: