2

I'm getting the ID of a media item from the MPMediaPickerController. According to the documentation, this value is an NSNumber object containing a uint64_t (unsigned long long). I would like to convert it to an NSString for saving in my data model. However, when I convert it to a string its value changes, and when I convert it back to a number it changes again! Obviously I'm not understanding something about these data types. Can you help?

MPMediaItem *mediaPicked;

// set mediaPicked to an MPMedia item using MPMediaPickerController...

NSLog(@"id as number: %qu", [mediaPicked valueForProperty:MPMediaItemPropertyPersistentID]); // outputs 566042331449280
NSLog(@"id as string: %@", [[mediaPicked valueForProperty:MPMediaItemPropertyPersistentID] stringValue]); // outputs 16204893883745507648
NSLog(@"id as number: %qu", [[[mediaPicked valueForProperty:MPMediaItemPropertyPersistentID] stringValue] longLongValue]); // outputs 9223372036854775807

If I try to play the media item before this conversion, it always works. But if I try to play the media item after this conversion, only about half of the media items I've tried work. So some ID values survive the conversion and some don't.

1 Answer 1

6

You are starting by saying this:

NSLog(@"id as number: %qu", [mediaPicked valueForProperty:MPMediaItemPropertyPersistentID]); // outputs 566042331449280

But that's wrong. %qu means "this thing is an unsigned long long". But this thing is not an unsigned long long. It's an object! It's an NSNumber wrapped around an unsigned long long. You are lying to NSLog, so you're getting garbage output in your very first statement.

Now, try this on your own machine:

uint64_t x = 16204893883745507648ULL;
NSLog(@"%qu", x);
NSNumber* n = [NSNumber numberWithUnsignedLongLong:x];
NSLog(@"%@", n);
NSLog(@"%@", [n stringValue]);

All of those NSLog statements give the same result - because they are all correct formulations, unlike the one you started with. So, those NSLog statements show you the kind of thing you ought to be saying.

Now, you might think: Oh, great, so I can get from an NSNumber to an NSString with stringValue after all. Yes, but you can't get back again. We cannot get from [n stringValue] to a correct NSNumber by using longlongValue, because a long long is not an unsigned long long. There is no unsignedLonglongValue. So you can't get there from here.

So what's the right thing to do? Don't convert at all! You've got an NSNumber, it's valid, just keep it and use it. An NSNumber is a value you can store in your model. (For example, it can go into a dictionary as a value or as a key, it can be a value in user defaults, and so on.)

1
  • I had a feeling I was getting bad output, but the "NSNumber wrapped around an unsigned long long" was throwing me off. If I change the NSLog to use %@ instead of %qu then I can see that the conversion from the original NSNumber to NSString using stringValue is fine. But then, as you stated, there's no comparable way to convert the NSString back to an NSNumber. However, with my problem better defined, I did some more searching and found an alternative here: stackoverflow.com/questions/1181637/…. Thanks for your help!
    – arlomedia
    Jan 7, 2011 at 16:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.