我一直在创建一个简单的示例应用程序来演示在 IOS 中播放声音文件。
为此,我创建了一个非常简单的带有一个 View Controller 的 XCode 项目。但是,尽管我的 AppDelegate.h 和 .m 文件仍未编辑,但我在 AppDelegate.m 中遇到了奇怪的解析问题。
在 @Implimentation 行上,编译器告诉我它缺少“@end”。
在线 -(BOOL) application: (UIApplication ) application didFinishLaunchingWithOptions: (NSDictionary) launch options 它告诉我 Expected ';'在方法原型(prototype)之后。
问题似乎源于 AppDelegate.m 文件中的#import "ViewController.h"引用。当我删除它时,这两个错误消失了,并被 Receiver 'ViewController' 替换,因为类消息是前向声明,这是我期望缺少导入的结果。
这是一个奇怪的问题。我以前构建过几个 IOS 应用程序,但从未遇到过这个问题。有关背景信息,该项目是在 XCode 4 中创建为单 View 应用程序的。我已将 ViewController.h 的 IBOutlets 和属性正确地排列到界面生成器中的 XIB。我还通过 build phases > Link Library with Libraries 功能添加了 AudioToolbox 框架。
这里是delegete和view controller 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;
非常感谢您花时间阅读本文。
最佳答案
ViewController.h 似乎缺少一个 @end
行:
#import "ViewController.h"
基本上会复制整个文件,所以如果 ViewController.h 中有错误,它也会在导入文件的所有地方出现。
关于ios - 未触及的 AppDelegate.m 中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14164293/