PDA

View Full Version : NSString format and outputting to UITextView




Tyre
May 26, 2011, 12:21 PM
I'm trying to query my iPhone to find out the play count of songs and all their data.


NSArray *star;
MPMediaQuery *query = [[MPMediaQuery alloc] init];

[query addFilterPredicate: [MPMediaPropertyPredicate
predicateWithValue: @"Vampire Weekend"
forProperty: MPMediaItemPropertyArtist]];
// Sets the grouping type for the media query
[query setGroupingType: MPMediaGroupingAlbum];

NSArray *albums = [query collections];
for (MPMediaItemCollection *album in albums) {
MPMediaItem *representativeItem = [album representativeItem];
NSString *artistName =
[representativeItem valueForProperty: MPMediaItemPropertyArtist];
NSString *albumName =
[representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
NSLog (@"%@ by %@", albumName, artistName);

NSArray *songs = [album items];
for (MPMediaItem *song in songs) {
NSString *playCount = [song valueForProperty:MPMediaItemPropertyPlayCount];
NSString *lastPlayed = [song valueForProperty:MPMediaItemPropertyLastPlayedDate];
NSString *songTitle =
[song valueForProperty: MPMediaItemPropertyTitle];

NSString *info = [[NSString alloc] initWithFormat: @"%@ has been played %@ times.\n Last played %@.", songTitle, playCount, lastPlayed];
NSLog(@"\n%@ has been played %@ times.\n Last played %@.", songTitle, playCount, lastPlayed);



}

}


I am having trouble with formatting the *info NSString so that it puts in the values for songTitle etc. How can I put all of that into a string, put the string into NSArray *star, then output that to a UITextView?



Sydde
May 26, 2011, 01:14 PM
Your best bet, if you want to display in a UITextView, might be to skip the array entirely and use a NSMutableString. Then, instead of the -initWithFormat: for each string, you just allocate the mutable string once and use -appendFormat: to add each line (making sure you put a \n at the end of each line).

Arrays are primarily for table views and do not really work so well with text views.

Tyre
May 26, 2011, 01:52 PM
Your best bet, if you want to display in a UITextView, might be to skip the array entirely and use a NSMutableString. Then, instead of the -initWithFormat: for each string, you just allocate the mutable string once and use -appendFormat: to add each line (making sure you put a \n at the end of each line).

Arrays are primarily for table views and do not really work so well with text views.

Okay. My only worry is that I'm going to eventually want to send this information to a server that will process the song information. So if I use an array, it can treat each element as a separate song instead of searching through a string.

The bigger problem is that it still won't handle this correctly:

NSString *info = [[NSString alloc] initWithFormat: @"%@ has been played %@ times.\n Last played %@.", songTitle, playCount, lastPlayed];

It sends warnings that songTitle, playCount, lastPlayed aren't being used. They obviously are, since I'm using them in an NSString definition. Any ideas?

Sydde
May 26, 2011, 04:33 PM
The bigger problem is that it still won't handle this correctly:

NSString *info = [[NSString alloc] initWithFormat: @"%@ has been played %@ times.\n Last played %@.", songTitle, playCount, lastPlayed];

It sends warnings that songTitle, playCount, lastPlayed aren't being used. They obviously are, since I'm using them in an NSString definition. Any ideas?

One thing you should look at is what those properties are. lastPlayed, for instance, is not a NSString, it is a NSDate. Similarly, playCount is a NSNumber. Not sure how these objects translate via -description, but I think it is generally bad form to just use them that way. I would suggest converting them first.

Personally, I prefer to declare my locals at the beginning of a method block, but that is mostly a matter of style, I guess. It could be (I am not certain how the compiler works exactly) the fact that info is not used is why it warns you that the others are not being used.

RonC
May 26, 2011, 06:10 PM
The bigger problem is that it still won't handle this correctly:

NSString *info = [[NSString alloc] initWithFormat: @"%@ has been played %@ times.\n Last played %@.", songTitle, playCount, lastPlayed];

It sends warnings that songTitle, playCount, lastPlayed aren't being used. They obviously are, since I'm using them in an NSString definition. Any ideas?

I just pasted that code into a Xcode editor window and compiled it. It doesn't complain about songTitle, playCount, or lastPlayed, but it does complain about star and info. Are you perhaps misreading the warning messages?