xcode 预期的 ';' 在声明列表的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6144497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:08:18  来源:igfitidea点击:

Expected ';' at end of declarations list

objective-cxcode

提问by Chris

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.

我正在尝试在 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 freaks out and gives errors at line that I define an object, or try to use query (since it is undefined).

Xcode 吓坏了,并在我定义对象的行中给出错误,或者尝试使用查询(因为它未定义)。

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

回答by mipadi

You need to put your code inside a method -- you can't have it "loose" in the implementation.

你需要把你的代码放在一个方法中——你不能让它在实现中“松散”。

回答by pgb

You don't need the curly braces after the @implementation, and you need to put your code inside some method!

在 之后不需要花括号@implementation,您需要将代码放在某个方法中!

It should read something like:

它应该是这样的:

@implementation Query

- (void)aMethod {
    // Code here
}

@end