Objective c 如何调用在另一个类中定义的方法
嗨 我有一个名为root的类: 在Objective c 如何调用在另一个类中定义的方法,objective-c,Objective C,嗨 我有一个名为root的类: 在root.h中:- #import "UIKit/UIKit.h" #import "AVFoundation/AVFoundation.h" #import "AudioToolbox/AudioToolbox.h" @interface root : UIView { } +(void)somefunction:(BOOL) sf; @end 在root.m中,somefunction的定义如下 -(void)somefunction:(BOOL)
root.h
中:-
#import "UIKit/UIKit.h"
#import "AVFoundation/AVFoundation.h"
#import "AudioToolbox/AudioToolbox.h"
@interface root : UIView {
}
+(void)somefunction:(BOOL) sf;
@end
在root.m中,somefunction
的定义如下
-(void)somefunction:(BOOL) sf {
//AVAudioPlayer *myExampleSound; //this variable can be named differently
if ( issoundon==TRUE) {
NSString *path =[[NSBundle mainBundle] pathForResource:"bg" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(
(CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
}
else{
NSString *path =[[NSBundle mainBundle] pathForResource:nil ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(
(CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
}
bool abc=true;
[root somefunction:true];
现在,我已经在另一个类中导入了root.h
,我正在调用“somefunction”,如下所示
-(void)somefunction:(BOOL) sf {
//AVAudioPlayer *myExampleSound; //this variable can be named differently
if ( issoundon==TRUE) {
NSString *path =[[NSBundle mainBundle] pathForResource:"bg" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(
(CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
}
else{
NSString *path =[[NSBundle mainBundle] pathForResource:nil ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(
(CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
}
bool abc=true;
[root somefunction:true];
但此时我的应用程序终止(崩溃)
基本上我正在尝试在我的应用程序中设置背景音乐(游戏开始),在游戏的中间,我允许用户切换声音。(即使我在视图的委托中调用函数,它也会崩溃)。
请告诉我发生了什么错误,因为我的代码编译正确(但有一些警告)。当然,您的代码编译成功。但是你知道吗?您的代码中有一个输入错误,导致程序崩溃不要因为代码编译成功就认为代码没有输入错误。 在
root.m
文件中,您有以下内容:
-(void)somefunction:(BOOL) sf {
它应该是+
,而不是-
,就像在头文件中一样:
+(void)somefunction:(BOOL) sf {
C类型bool
和Objective-C类型bool
之间可能也存在差异,但我不太确定:
bool abc=true; // Shouldn't this be BOOL abc = YES, and
[root somefunction:true]; // shouldn't you be passing abc here?
您的代码是不完整的,如果没有上下文,很难说出错误是什么。然而:
- 您还没有在任何地方定义
issoundon
- 您在哪里构造了根对象?在界面生成器中
- 为什么它是UIView的一个子类
- AudioServicesPlaySystemSound是否同步
- 为什么您希望能够播放名为
(nil).wav的文件
请参阅此问题:“但此时我的应用程序终止(崩溃)” 我们需要更多地了解这一点,以帮助您解决这一问题 在Xcode控制台中,它应该发出一些消息,以缩小问题的范围。堆栈跟踪也会有帮助
我总的猜测是,
root
的本地实例未定义,因此您正在对该对象调用一个无法识别的选择器。但是如果没有控制台的更多帮助,这完全是猜测。plz在代码正确编译时忽略输入错误。请阅读编辑帮助并正确格式化代码。@Rachit Singhal:这是一件可怕的事情。仅仅因为代码编译正确并不意味着语法没有问题。您的标题似乎与您的问题无关。您应该注意,Cocoa命名约定的类名以大写字母开头(最好是一个前缀,用于解决名称空间问题)。调用类root
非常令人困惑,甚至在处理具有名为root
的属性的其他类时可能会导致问题BOOL
被定义为auint8\u t
而BOOL
据我所知是一个1位整数BOOL
是一个有符号字符true
和YES
都是1
(和false
和NO
都是0
),因此不应该存在任何问题。我打赌实现中缺少的类方法就是正在发生的事情。他可能会收到警告,这些警告应该被视为错误。但是,即使他更改了类方法实现,看起来他希望从中访问实例变量issoundon
,因此他需要在这里进行一些重新编译。感谢您的建议,我将从现在开始遵循它。我的类是“root”(在其中我将函数“somefonction”定义为+(void)somefunction:(BOOL)是soundon{/....},第二个类是“anyUIViewController.m”,在其中我试图调用名为“-(IBActive)togglesound”的函数中的函数如果您可以这样做,这将非常有帮助。您的代码可能只播放30秒的同步声音,这不是“背景音乐”。为什么UIView的子类是放置声音代码的好地方?删除If语句第二条的内容-播放文件nil
与静音现有声音不同。