0

我一直在创建一个简单的示例应用程序来演示在 IOS 中播放声音文件。

为此,我用一个视图控制器创建了一个非常简单的 XCode 项目。但是,尽管我的 AppDelegate.h 和 .m 文件仍未编辑,但我在 AppDelegate.m 中遇到了奇怪的解析问题。

@Implimentation行上,编译器告诉我它缺少'@end'。

在线-(BOOL) application: (UIApplication ) application didFinishLaunchingWithOptions: (NSDictionary ) 启动选项它告诉我 Expected ';' 在方法原型之后。

这些问题似乎源于 AppDelegate.m 文件中的#import "ViewController.h" 引用。当我删除这两个错误时,这两个错误就消失了,并被 Receiver 'ViewController' 取代,因为类消息是一个前向声明,这是我在缺少导入时所期望的。

这是一个奇怪的问题。我之前构建了几个 IOS 应用程序,但从未遇到过这个问题。有关背景信息,该项目是在 XCode 4 中作为单视图应用程序创建的。我已将 ViewController.h 的 IBOutlets 和属性正确排列到界面构建器中的 XIB 中。我还通过构建阶段 > Link Library with Libraries功能添加了 AudioToolbox 框架。

这是委托和视图控制器文件文件

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

ViewContoller.m

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController ()

SystemSoundID pig;
SystemSoundID cow;
SystemSoundID sheep;
SystemSoundID chicken;
@end

@implementation ViewController

@Synthesize but_cow, but_pig, but_sheep, but_chicken;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString * cowSoundURL= [[NSBundle mainBundle] pathForResource:@"cow" ofType: @"mp3"];
    NSString * pigSoundURL= [[NSBundle mainBundle] pathForResource:@"pig" ofType: @"mp3"];
    NSString * sheepSoundURL= [[NSBundle mainBundle] pathForResource:@"sheep" ofType: @"mp3"];
    NSString * chiickenSoundURL= [[NSBundle mainBundle] pathForResource:@"chicken" ofType: @"mp3"];

    AudioServicesCreateSystemSoundID(cowSoundURL, &cow);
    AudioServicesCreateSystemSoundID(pigSoundURL, &pig);
    AudioServicesCreateSystemSoundID( sheepSoundURL, &sheep);
    AudioServicesCreateSystemSoundID(chickenSoundURL, &chicken);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//====================================================
/**
 Called when a button is pressed
 **/
//====================================================
-(IBAction)buttonPressed:(id)sender
{
    if (sender == but_cow)
    {
        AudioServicesPlaySystem(cow);
    }
    else if (sender == but_sheep)
    {
        AudioServicesPlaySystem(sheep);
    }
    else if (sender ==  but_pig)
    {
        AudioServicesPlaySystem(pig);
    }
    else if (sender == but_chicken)
    {
        AudioServicesPlaySystem(chicken);
    }

}//===================================================
@end

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@property (nonatomic, retain) IBOutlet UIButton * but_cow;
@property (nonatomic, retain) IBOutlet UIButton * but_pig;
@property (nonatomic, retain) IBOutlet UIButton * but_sheep;
@property (nonatomic, retain) IBOutlet UIButton * but_chicken;



-(IBAction)buttonPressed:(id)sender;

非常感谢您花时间阅读本文。

4

2 回答 2

2

ViewController.h 似乎缺少一个@end

该行:

#import "ViewController.h"

基本上会复制整个文件,所以如果 ViewController.h 中有错误,它也会显示在导入该文件的任何地方。

于 2013-01-04T20:18:56.860 回答
1

你没有@end加入viewcontroller.h

于 2013-01-04T20:22:15.257 回答