Andrew S
Andrew S

Reputation: 2987

Compile Errors in untouched AppDelegate.m

I have been creating a simple example app to demonstrate playing sound files in IOS.

For this I have created a very simple XCode project with one view controller. However, although my AppDelegate.h and .m files have remained unedited I am getting strange parse issues in the AppDelegate.m.

On the line @Implimentation the compiler tells me its missing '@end'.

On the line -(BOOL) application: (UIApplication ) application didFinishLaunchingWithOptions: (NSDictionary) launch options it tells me Expected ';' after method prototype.

The issues seem to stem from the #import "ViewController.h" reference in the AppDelegate.m file. As when I remove this these two errors go away, and get replaced with Receiver 'ViewController' for class message is a forward declaration, which is what I would expect with a missing import.

This is an odd problem. I've built several IOS apps before but never encountered this issue. For background info the project was created as a Single View App in XCode 4. I have properly lined the IBOutlets and Properties of the ViewController.h to the XIB in interface builder. I have also added in the AudioToolbox framework in via the build phases > Link Library with Libraries feature.

Here is the delegete and view controller files files

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;

Thanks very much for taking the time to read this.

Upvotes: 0

Views: 1199

Answers (2)

Midhun MP
Midhun MP

Reputation: 107211

You are not adding @end in viewcontroller.h

Upvotes: 1

amattn
amattn

Reputation: 10065

ViewController.h seems to be missing an @end

The line:

#import "ViewController.h"

will basically copy in the entire file, so if there is an error in ViewController.h, it will show up everywhere that file is imported as well.

Upvotes: 2

Related Questions