【问题标题】:parse issue Expected identifier or "("解析问题预期的标识符或“(”
【发布时间】:2014-11-24 11:55:11
【问题描述】:

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

问题是我在 ViewController.h 中的“@interface ViewController : NSObject :UIViewController”行上收到错误提示“Expected identifier or '('”。

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

求助!!!!!!

【问题讨论】:

  • 记得投票并接受解决您问题的答案。

标签: ios objective-c xcode uiviewcontroller


【解决方案1】:

这段代码急需重构……

  1. 帮助找出导致问题的原因。
  2. 让它更易于使用。

给你...

#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 方法,那么您根本不需要在标题中声明它们。

【讨论】:

  • 感谢您的帮助,但不幸的是 xcode 说有问题。我想做的是创建一个在按下按钮时播放噪音的音板。你介意为我写代码吗?编写代码后,我会将声音文件放入正确的文件夹中。 @雾迈斯特
  • @HarryTGraham Xcode 显示的问题是什么?如果你想让我为你写,我可以给你寄一张发票,然后把它做好。 :)
  • 谢谢你,那太好了!问题是在 " (void)playSoundWithName:(NSString *)name { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)name, CFSTR("wav"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); AudioServicesPlaySystemSound(soundID); }
  • 部分每一行都有问题。第一行说“预期类型”,第二行说“使用未声明的标识符“CFURLRef”@Fogmeister
  • @HarryTGraham 那么我建议找一个关于播放声音的教程。
【解决方案2】:

@interface ViewController : NSObject :UIViewController

这是错误的,你不能有多个类。您只能从一个子类化。 由于您在 .m 文件中有一些 IBAction,我猜您将其用作视图控制器并在 .h 文件中删除 NSObject。 @interface ViewController : UIViewController

在 .m 中应该是

@implementation ViewController

【讨论】:

    【解决方案3】:

    你不能从两个类继承;只需使用UIViewController:

    @interface ViewController : UIViewController 
    

    【讨论】:

    • 谢谢 我把那个放在哪里?以 .m 还是 .h 为单位? @trojanfoe
    • @HarryTGraham 在标头 (.h) 文件中。
    【解决方案4】:

    这样写.h文件

       @interface ViewController :UIViewController
    
      -(IBAction)sound1;
    -(IBAction)sound2;
     -(IBAction)sound3;
    -(IBAction)sound4;
    -(IBAction)sound5;
      -(IBAction)sound6;
      @end
    

    .m 文件将 NSObject 替换为 UIViewController

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

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多