解析问题预期标识符或"("

时间:2014-11-24 11:55:11

标签: ios objective-c xcode uiviewcontroller

我正在尝试创建一个噪音应用程序,但卡住了。我不是开发人员而只是为了好玩而创建它!

问题在于我收到一条错误,上面写着"预期的标识符或'('"在" @interface ViewController:NSObject:UIViewController" line在ViewController.h中。

ViewController.h的代码是:

@interface ViewController : NSObject  :UIViewController {
}

-(IBAction)sound1;
-(IBAction)sound2;
-(IBAction)sound3;
-(IBAction)sound4;
-(IBAction)sound5;
-(IBAction)sound6;

@end

ViewController.m的代码是:

#import "ViewController.h"

@implementation ViewController : NSObject;

-(IBAction)five {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

-(IBAction)cramp {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound2", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

-(IBAction)cycling {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound3", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

-(IBAction)iLoveBanking {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound4", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

-(IBAction)sound5 {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound5", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

-(IBAction)sound6 {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound6", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

@end

HELP !!!!!!

4 个答案:

答案 0 :(得分:2)

这段代码迫不及待重构......

  1. 帮助查找导致问题的原因。
  2. 让它更容易使用。
  3. 你去......

    #import "ViewController.h"
    
    @implementation ViewController
    
    -(IBAction)five {
        [self playSoundWithName:@"sound1"];
    }
    
    -(IBAction)cramp {
        [self playSoundWithName:@"sound2"];
    }
    
    -(IBAction)cycling {
        [self playSoundWithName:@"sound3"];
    }
    
    -(IBAction)iLoveBanking {
        [self playSoundWithName:@"sound4"];
    }
    
    -(IBAction)sound5 {
        [self playSoundWithName:@"sound5"];
    }
    
    -(IBAction)sound6 {
        [self playSoundWithName:@"sound6"];
    }
    
    - (void)playSoundWithName:(NSString *)name
    {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)name, CFSTR("wav"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }
    
    @end
    

    此外,您的标题应该是......

    @interface ViewController : UIViewController
    
    @end
    

    如果您只是从按钮调用IBAction方法,则根本不需要在标题中声明它们。

答案 1 :(得分:1)

  

@interface ViewController:NSObject:UIViewController

这是错误的,你不能有多个班级。你只能从一个子类。 由于你在.m文件中有一些IBAction,我想你是用它作为视图控制器而在.h文件中删除NSObject。 @interface ViewController : UIViewController

在.m中它应该是

@implementation ViewController

答案 2 :(得分:1)

你不能继承两个班级;只需使用UIViewController

@interface ViewController : UIViewController 

答案 3 :(得分:0)

像这样编写.h文件

   @interface ViewController :UIViewController

  -(IBAction)sound1;
-(IBAction)sound2;
 -(IBAction)sound3;
-(IBAction)sound4;
-(IBAction)sound5;
  -(IBAction)sound6;
  @end

和.m文件用UIViewController替换NSObject

    #import "ViewController.h"
      @implementation ViewController : UIViewController;