I'm trying to create a new Objective-C class in Xcode and am getting a couple of the above errors in the .m file.
#import "Query.h"
@implementation Query {
MPMediaQuery *query = [[MPMediaQuery alloc] init]; //create new query
[[query addFilterPredicate: [MPMediaPropertyPredicate
predicateWithValue: @"Vampire Weekend"
forProperty: MPMediaItemPropertyArtist]]; //filter out artists except for Vampire Weekend
// Sets the grouping type for the media query
[query setGroupingType: MPMediaGroupingAlbum]; //sort by album
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);
}
}
// Override point for customization after application launch.
[query release];
}
@end
Xcode freaks out and gives errors at line that I define an object, or try to use query (since it is undefined).
My header file is as follows (not complete, but I stopped to deal with this):
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface Query : NSObject {
MPMediaQuery *query;
}
@property (nonatomic, retain) MPMediaQuery *query;
@end
You need to put your code inside a method -- you can't have it "loose" in the implementation.
You don't need the curly braces after the @implementation
, and you need to put your code inside some method!
It should read something like:
@implementation Query
- (void)aMethod {
// Code here
}
@end
精彩评论