开发者

Expected ';' at end of declarations list

开发者 https://www.devze.com 2023-03-08 20:27 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消