我是客观c编码的新手,请耐心问我是否这是一个简单的问题。
当我尝试运行下面的代码时,它说
NO visible @interface for 'SampleClass' declares the selector 'multiplyA:withB'
在目标C中乘以两个数字是错误的方法吗?我无法找到错误。
这是代码的头文件
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@end
@interface SampleClass : NSObject
-(NSNumber *)multiplYA:(NSNumber *)a withB:(NSNumber *)b;
@end
这是代码的实现文件
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
SampleClass *sampleClass = [[SampleClass alloc]init];
NSNumber *a = [NSNumber numberWithFloat:10.5];
NSNumber *b = [NSNumber numberWithFloat:10.0];
NSNumber *result = [sampleClass multiplyA:a withB:b];
NSString *resultString = [result stringValue];
NSLog(@"The product is %@",resultString);
}
@end
@implementation SampleClass
- (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b
{
float number1 = [a floatValue];
float number2 = [b floatValue];
float product = number1 * number2;
NSNumber *result = [NSNumber numberWithFloat:product];
return result;
}
@end
答案 0 :(得分:2)
这是头文件中的大小写。将标题函数标题更改为:
-(NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b;
答案 1 :(得分:1)
您只需在multiplyA:withB
部分提供@interface
方法声明即可。现在它读取-(NSNumber *)multiplYA:(NSNumber *)a withB:(NSNumber *)b;
。看到错误?大Y.它一定很小。或者其他 - @implementation
部分内的大Y.换句话说,他们应该匹配