Smart Billionaire
Smart Billionaire

Reputation: 21

How to assign to the nsstring object a NSString with variables?

I have the following code:

long mins = 02; long secs = 35;
  NSString *allTime = [[NSString alloc]init];
    allTime = @"%i:%i",mins, secs ;

But it doesn't work, because when I try to display that nsstring object I got this: %i:%i Instead of that I want to get: 02:35

How to do that ? Thank you!

allTime = [NSString stringWithFormat:@"%l/%l", mins, secs];
for(Playlists *thePL in collection)
    {
        NSLog(@"===NEXT PLAYLIST===");
        NSLog(@"Name: %@", thePL.namePL);
        NSLog(@"Quantity of songs: %i", thePL.entries);
        NSLog(@"Total of time: %@",thePL.allTime);
    }

TOTAL OF TIME: l

Upvotes: 0

Views: 146

Answers (4)

user1492776
user1492776

Reputation: 280

first you should read the calss reference of NSString. second, you should use this code:

 NSString *allTime = [stringWithFormat:@"%d,:%d", mins, secs]

you can also to alloc the string and then initialize it by this way:

NSString *allTime = [[Nsstring alloc] init];
allTime = [NSString stringWithForat:@"%d:%d", mins, secs];

although, the best, the short and the better way is the first one. hope this help...

Upvotes: 0

AW101
AW101

Reputation: 1640

Try:

NSString *allTime = [NSString stringWithFormat:@"%d:%d",mins, secs];

Upvotes: 3

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

long mins = 02;
long secs = 35;   
NSString *allTime = [[NSString alloc] initWithFormat: @"%d:%d", mins, secs]; 

Upvotes: 4

WrightsCS
WrightsCS

Reputation: 50697

You need to use stringWithFormat, see the NSString Class Reference.

allTime = [NSString stringWithFormat:@"%d:%d",mins, secs];

Upvotes: 6

Related Questions