开发者

connecting three NSStrings

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;
    }开发者_如何学C
}

- (void) viewDidLoad {
    imageArray = [[NSArray alloc] initWithObjects:@"image1", @"image2", nil];
    imageCount = 0;
}


You could just do it in two steps:

NSString* partialUrl= [prefix stringByAppendingString:profileID];
NSString* fullUrl= [partialUrl stringByAppendingString:suffix];

Alternatively, you could use a format:

NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileID, suffix];


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:

NSArray *elementsInURL = [NSArray arrayWithObjects:prefix, profileID, suffix, nil];
NSString *combined = [elementsInURL componentsJoinedByString:@""];


You want + (id)stringWithFormat:(NSString *)format, …

Full docs are here


I'd handle the concatenation like this:

NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";

NSString *URL =  [[prefix stringByAppendingString:profileId] 
                          stringByAppendingString:suffix];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜