Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Concatenate strings in iOS

Hey Stone Preston Amit Bijlani Ben Jakuben Pasan Premaratne

I know i can use following code to access photos:

NSString *photoURL = [[NSBundle mainBundle] pathForResource:@"photo" ofType:@"png"];

however, if i have many photos, which are named photo_1, photo_2, photo_3 etc, how can i add the _1, _2 etc. using a method?

in javascript, to concatenate, one can use the plus sign, like for example

var photoURL = "photo"+"_1";
// photoURL = photo_1

but in iOS/Objective-C one must do it differently right? i cant just write the following, right?

// addNumber is the method to determine the number
NSString *photoURL = [[NSBundle mainBundle] pathForResource:@"photo"+addNumber ofType:@"png"];

also: would i use the getter method or the setter method to determine the number? the getter i presume, since i am trying to GET a value, right?

i hope this super long question did not cause any trouble...

As always, thanks so very much for your help! :)

Cheers, pascal

3 Answers

concatenation is a bit trickier in objective C, and really I think the easiest way to accomplish what you want is to use a for loop and a formatted string and a mutable array to hold your URLs using something like this

NSMutableArray *photoURLs = [[NSMutableArray alloc] init];
for (int i = 1; i < 10; i++) {

    NSString *path = [NSString stringWithFormat:@"photo_%d", i];
    NSString *photoURL = [[NSBundle mainBundle] pathForResource:path ofType:@"png"];

    [photoURLs addObject:photoURL];

}

You win :'(

haha well you posted first so you won that. mine solution is a bit shorter but they both do the same thing essentially.

what about having an array of the photo names? and using the location in the array in the current method you are using?

e.g:

-(NSArray*)photoArray{

    NSMutableArray *PhotoArray;
    for (int i=0; i++; i<15){
        NSString *photoString = [NSString stringWithFormat:@"photo_%d", i];
        [PhotoArray addObject:photoString];
    }
    return PhotoArray;
} //this will return a NSArray of all of the photo name strings you require.(note: Not a mutable one.)

-(NSArray*)URLArray{

    NSMutableArray *urlArray;
    for (int i=0; i++; i<15){
        NSString *urlString = [[NSBundle mainBundle] pathForResource:[mPhotoArray objectAtIndex:i] ofType:@"png"];
        [URLArray addObject:urlString];
    }
    return urlArray;
} //This will return an array of all of the required photo urls!


//or combine into one function:

-(NSArray*)URLArray(int x){

    NSMutableArray *PhotoArray;
    for (int i=0; i++; i<x){
        NSString *photoString = [NSString stringWithFormat:@"photo_%d", i];
        [PhotoArray addObject:photoString];
    }

    NSMutableArray *urlArray;
    for (int i=0; i++; i<x){
        NSString *urlString = [[NSBundle mainBundle] pathForResource:[mPhotoArray objectAtIndex:i] ofType:@"png"];
        [URLArray addObject:urlString];
    }
    return urlArray;
}

//this would return an array with a given number of URL's

Thanks a lot guys!! :)