1

I m pretty new to objective-c, and trying some examples on my own. Here is my sample code

#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];
}

and it compiled with no error, but when i run the program i get the following error.

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

What am i doing wrong in my code ?

Whoami
  • 13,930
  • 19
  • 84
  • 140

2 Answers2

3

By default 1st letter of iVar is capitalized in setter method name. So correct call will be:

[t setNoOfWheels: 10];
Vladimir
  • 170,431
  • 36
  • 387
  • 313
3
[t setnoOfWheels: 10];

should be

[t setNoOfWheels: 10];

or even better, since you're declaring a property:

t.noOfWheels = 10;
Jimmy Luong
  • 1,109
  • 1
  • 8
  • 16
  • `t.noOfWheels` is actually worse. Dot notation is an abomination that should have been strangled at birth. Other than that, good answer. – JeremyP Apr 10 '12 at 16:03
  • If you come from a C# .NET background like I did, then dot notation is really helpful. – Jimmy Luong Apr 10 '12 at 16:15
  • 1
    @JeremyP dot notation is NOT an abomination, it cleans up code in situations where it would otherwise be horrible. Take this example: `[[[[CCDirector sharedDirector] openGLView] layer] setMask:nil];`, turns into: `CCDirector.sharedDirector.openGLView.layer.mask = nil;` which is much more concise and sticks to the coding style in C & C++, which is what Obj-C was based on. I suppose you would say the same thing about subscripting, that it's an abomination as well? Learn to appreciate how the language has grown and become more elegant. – Richard J. Ross III Apr 10 '12 at 17:03
  • Note that you don't need an `@property` to use `dot syntax` and vice-versa. – bbum Apr 10 '12 at 17:19
  • @Richard: We will have to agree to disagree, but yes it is. It obscures what is happening, overloads the dot operator inappropriately and, worst of all, breaks dynamic binding (try using dot notation with an object declared as type `id`). It does nopt make Objective-C more elegant to pollute the syntax with unnecessary rubbish. – JeremyP Apr 11 '12 at 07:37