1

私はobjective-cにかなり慣れていないので、自分でいくつかの例を試しています。ここに私のサンプルコードがあります

#import <objc/objc.h>
#import <Foundation/Foundation.h>


@interface Test:NSObject
{
  int noOfWheels;
  int total;
}

   @property int noOfWheels;
   @property int total;
   -(void) print;
@end

@implementation Test
@synthesize  noOfWheels, total;
-(void) print
{
  NSLog (@" noofWheels is %i, total %i ", noOfWheels, total);
}

@end

int main ( int argc, char ** argv)
{
  Test *t = [Test alloc];
  t = [t init];
  [t setnoOfWheels: 10];
  [t settotal: 300];
  [t print];
}

エラーなしでコンパイルされましたが、プログラムを実行すると次のエラーが発生します。

Uncaught exception NSInvalidArgumentException, reason: -[Test setnoOfWheels:]: unrecognized selector sent to instance 0x87aef48

私のコードで何が間違っていますか?

4

2 に答える 2

3

デフォルトでは、iVar の最初の文字はセッター メソッド名で大文字になります。したがって、正しい呼び出しは次のようになります。

[t setNoOfWheels: 10];
于 2012-04-10T15:49:32.200 に答える
3
[t setnoOfWheels: 10];

する必要があります

[t setNoOfWheels: 10];

または、プロパティを宣言しているので、さらに良い:

t.noOfWheels = 10;
于 2012-04-10T15:50:14.233 に答える