对,我有三个NSStrings,我想把它们放在一起制作一个长的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;
}
答案 0 :(得分:4)
你可以分两步完成:
NSString* partialUrl= [prefix stringByAppendingString:profileID];
NSString* fullUrl= [partialUrl stringByAppendingString:suffix];
或者,您可以使用以下格式:
NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileID, suffix];
答案 1 :(得分:1)
作为一种通用的解决方案,当您提前知道需要组合多少个字符串时,可以将它们粘贴在NSArray中并使用该数组将它们连接在一起。所以在这种情况下:
NSArray *elementsInURL = [NSArray arrayWithObjects:prefix, profileID, suffix, nil];
NSString *combined = [elementsInURL componentsJoinedByString:@""];
答案 2 :(得分:0)
您想要+ (id)stringWithFormat:(NSString *)format, …
完整文档为here
答案 3 :(得分:0)
我会像这样处理串联:
NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";
NSString *URL = [[prefix stringByAppendingString:profileId]
stringByAppendingString:suffix];