【发布时间】:2011-05-26 20:14:06
【问题描述】:
我正在尝试在 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
【问题讨论】:
-
mipadi 的回答是正确的,但是您的代码中还有一个错误:
@implementation块不应该用大括号括起来。
标签: objective-c xcode