0

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

1
  • 1
    Please don't edit your questions to add new questions to them — that isn't how Stack Overflow works. If you want to ask a new question, you should do it as a new question. And please take care to ask your questions clearly. You appear to have both an allTime local variable and a property allTime belonging to the Playlist class. If that isn't actually the case, you'll need to post your real code with context, because it's fiendishly hard to debug heavily abridged code. Commented Jul 10, 2012 at 20:54

4 Answers 4

6

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

allTime = [NSString stringWithFormat:@"%d:%d",mins, secs];
Sign up to request clarification or add additional context in comments.

4 Comments

Not equivalent to the OP's code - different release rules (sans ARC).
@SevaAlekseyev: Not really. There are no "release rules" for the expression @"%i:%i",mins, secs — it yields a long, not an object. If that were somehow a valid way to create a string, though, it would almost certainly be equivalent to stringWithFormat:, not alloc+initWithFormat:, as it does not contain any of the magic words that confer ownership.
Well, there's a compilation error in the OP's code. Modulo that, there's a memory leak. Modulo that, I guess he wants a non-autoreleased string :) Again, we don't know if ARC is enabled.
For a new problem with new code, accept an answer to this question and create a new question. That's how SO works - this is not a forum.
4
long mins = 02;
long secs = 35;   
NSString *allTime = [[NSString alloc] initWithFormat: @"%d:%d", mins, secs]; 

Comments

3

Try:

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

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.