Search code examples
objective-cnsstringnsarray

how could i combine NSString with NSArray


I have NSString*url ,and i want to combine with NSArray of number to load the picture ,i struggle for many days still doesn't work anyone know how to do it

p.s I want to replace NSString %@ with number of array

This is my NSString *url = http://flicksbank.console360.net/images/%@/default.jpg

And this is my number from NSArray :

(

    42,
    47,
    56,
    65,
    97,
    128,
    277,
    278,
    312,
    313,
    518,
    522,
    523,
    526
),
    (
    42,
    89,
    522
),
    (
    89,
    312,
    313
),
    (
    89,
    522
),
    (
    91,
    317
),
    (
    98
),
    (
    317,
    518,
    523,
    525,
    526
),
    (
    329
),
    (
    332
)

Solution

  • you can reach numbers with two for loops:

    for(NSArray *numbers in yourMainArray)
    {
         for(NSNumber *n in numbers)
         {
             NSString *urlString = [NSString stringWithFormat:@"http://flicksbank.console360.net/images/%d/default.jpg", [n intValue]];
         }
    
    }
    

    or more general solution for NSString and NSNumber

        for(NSArray *numbers in yourMainArray)
        {
             for(id n in numbers)
             {
                 if([n isKindOfClass:[NSNumber class]])
                 {
                     NSString *urlString = [NSString stringWithFormat:@"http://flicksbank.console360.net/images/%d/default.jpg", [n intValue]];
                 }
                  if([n isKindOfClass:[NSString class]])
                 {
                     NSString *urlString = [NSString stringWithFormat:@"http://flicksbank.console360.net/images/%@/default.jpg", n];
                 }
             }
    
        }