我正在尝试在Xcode中创建一个新的Objective-C类,并且在.m文件中遇到了上述几个错误。
#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吓坏了,并在我定义一个对象的行上给出错误,或者尝试使用查询(因为它是未定义的)。
我的头文件如下(不完整,但我停下来处理这个):
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface Query : NSObject {
MPMediaQuery *query;
}
@property (nonatomic, retain) MPMediaQuery *query;
@end
答案 0 :(得分:6)
您需要将代码放在方法中 - 您不能在实现中“松散”。
答案 1 :(得分:2)
@implementation
之后你不需要花括号,你需要将代码放在某个方法中!
它应该是这样的:
@implementation Query
- (void)aMethod {
// Code here
}
@end