Objective C Class可能不响应methord

时间:2016-05-03 10:04:23

标签: objective-c

我是Objective C的新手,我正在尝试自己创建一个示例程序。当我在我的Linux机器上编译代码时,我得到了足够的警告并且它已被编译。但令人惊讶的是我没有输出,而是收到了很多消息。代码描述如下:

  

方法声明

    @interface AsciitoBinaryClass:NSObject
    /* method declaration */
    - (int)AsciitoBinary: (char)d Num1:(int*)bin ;
   @end
  

方法

@implementation AsciitoBinaryClass
    /* method Convert Ascii to Binary */
    - (int)AsciitoBinary: (char)d Num1:(int*)bin{
        int len=0;
        int a=d;
        while(a>0)
        {
            bin[len]=a%2;
            a/=2;
            len++;
        }
        return len;
    }
@end
  

来自Main

的方法调用
AsciitoBinaryClass *asciitobinaryClass = [[AsciitoBinaryClass alloc]init];
    len = [asciitobinaryClass AsciitoBinary:a Num2:bin];

编译警告在下面给出

shameerariff@shameerariff-Lenovo-G460:~/study/c/crypt/objc$ gcc test.m -o test  `gnustep-config --objc-flags` `gnustep-config --base-libs`
test.m: In function ‘main’:
test.m:35:2: warning: ‘AsciitoBinaryClass’ may not respond to ‘-AsciitoBinary:Num2:’
  len = [asciitobinaryClass AsciitoBinary:a Num2:bin];
  ^
test.m:35:2: warning: (Messages without a matching method signature
test.m:35:2: warning: will be assumed to return ‘id’ and accept
test.m:35:2: warning: ‘...’ as arguments.)
test.m:35:6: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
  len = [asciitobinaryClass AsciitoBinary:a Num2:bin];
      ^
test.m:32:13: warning: variable ‘len’ set but not used [-Wunused-but-set-variable]
  int bin[8],len=0;

运行时的消息

shameerariff@shameerariff-Lenovo-G460:~/study/c/crypt/objc$ ./test 
2016-05-03 15:25:02.600 test[22624] autorelease called without pool for object (0x2607270) of class GSCInlineString in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.616 test[22624] autorelease called without pool for object (0x2745330) of class NSException in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.616 test[22624] autorelease called without pool for object (0x2744530) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.616 test[22624] autorelease called without pool for object (0x2735790) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x2732dc0) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x2734260) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x26ae280) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x264c610) of class NSIntNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x2653490) of class NSLongLongNumber in thread <NSThread: 0x2593270>
2016-05-03 15:25:02.617 test[22624] autorelease called without pool for object (0x264af40) of class NSIntNumber in thread <NSThread: 0x2593270>
./test: Uncaught exception NSInvalidArgumentException, reason: -[AsciitoBinaryClass AsciitoBinary:Num2:]: unrecognized selector sent to instance 0x2598520
shameerariff@shameerariff-Lenovo-G460:~/study/c/crypt/objc$ 

1 个答案:

答案 0 :(得分:2)

您已将方法定义为:

<div></div>

你称之为:

- (int)AsciitoBinary: (char)d Num1:(int*)bin;

- (int)AsciitoBinary: (char)d Num2:(int*)bin ; AsciitoBinary:Num1:没有相同的删除。

在你的主要功能中替换:

AsciitoBinary:Num2:

通过

AsciitoBinaryClass *asciitobinaryClass = [[AsciitoBinaryClass alloc]init];
len = [asciitobinaryClass AsciitoBinary:a Num2:bin];