未修改的 AppDelegate.m 中的编译错误
Posted
技术标签:
【中文标题】未修改的 AppDelegate.m 中的编译错误【英文标题】:Compile Errors in untouched AppDelegate.m 【发布时间】:2013-01-04 20:17:37 【问题描述】:我一直在创建一个简单的示例应用程序来演示在 ios 中播放声音文件。
为此,我创建了一个带有一个视图控制器的非常简单的 XCode 项目。但是,尽管我的 AppDelegate.h 和 .m 文件仍未编辑,但我在 AppDelegate.m 中遇到了奇怪的解析问题。
在 @Implimentation 行上,编译器告诉我它缺少“@end”。
在 -(BOOL) application: (UIApplication ) application didFinishLaunchingWithOptions: (NSDictionary) launch options 这行告诉我 Expected ';'在方法原型之后。
问题似乎源于 AppDelegate.m 文件中的#import "ViewController.h" 引用。当我删除这两个错误时,这两个错误消失了,并被 Receiver 'ViewController' 取代,因为类消息是一个前向声明,这是我对缺少导入的期望。
这是一个奇怪的问题。我之前构建了几个 IOS 应用程序,但从未遇到过这个问题。有关背景信息,该项目是在 XCode 4 中作为单视图应用程序创建的。我已将 ViewController.h 的 IBOutlets 和属性正确排列到界面构建器中的 XIB 中。我还通过 build phase > 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
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;
非常感谢您花时间阅读本文。
【问题讨论】:
【参考方案1】:ViewController.h 似乎缺少 @end
行:
#import "ViewController.h"
基本上会复制整个文件,因此如果 ViewController.h 中出现错误,它也会显示在导入该文件的所有位置。
【讨论】:
谢谢各位,这就是问题所在。它所做的就是为我的代码中的其他小错误打开了大门,敏锐的 IOS 程序员可能会发现这些错误。我希望编译器更精确,现在是 2013 年。【参考方案2】:您没有在viewcontroller.h
中添加@end
【讨论】:
以上是关于未修改的 AppDelegate.m 中的编译错误的主要内容,如果未能解决你的问题,请参考以下文章
AppDelegate.m 类中的@interface 错误
appdelegate.m 中的 UIAlertView 不起作用
将 AppDelegate.h 和 AppDelegate.m 替换为 AppDelegate.swift
在 AppDelegate.m 中使用未声明的标识符“对讲机”-react-native-intercom