在未触及的AppDelegate.m中编译错误

时间:2013-01-04 20:17:38

标签: ios xcode parsing appdelegate

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

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

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

- (BOOL)应用程序:( UIApplication )应用程序didFinishLaunchingWithOptions :( NSDictionary )启动选项它告诉我预期';'方法原型之后。

这些问题似乎是从AppDelegate.m文件中的#import“ViewController.h”引用开始的。当我删除这个时,这两个错误消失了,并且用Receiver'ViewController'代替类消息是一个前向声明,这是我期望的导入丢失。

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

这是delegete和视图控制器文件文件

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

ViewController.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;

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

2 个答案:

答案 0 :(得分:2)

ViewController.h似乎缺少@end

该行:

#import "ViewController.h"

基本上会复制整个文件,因此如果ViewController.h中出现错误,它将显示在导入文件的任何地方。

答案 1 :(得分:1)

您未在@end

中添加viewcontroller.h