Rebol Tutorial
Rebol Tutorial

Reputation: 2754

Objective C: Why Invalid Receiver type on NSUInteger and not NSString whereas created the same way

I have generated this source code automatically the same way for all fields why compiler tells me Invalid Receiver type on NSUInteger and not NSString whereas created the same way:

/**
 class to represent an Person
 */
@interface Person: NSObject {
    // private members
    NSString* _firstName;
    NSString* _lastName;
    NSUInteger _age;
}
// Initializers
/**
 Initializes a new instance of the Person class.
 @returns a newly initialized object
 */
- (id)initPerson;

/**
 Initializes a new instance of the Person class with
 @param firstName The First Name
 @param lastName The Last Name
 @param age The Age
 @returns a newly initialized object
 */
- (id)initPersonWithFirstName:(NSString*)firstName LastName:(NSString*)lastName Age:(NSUInteger)age;

// public accessors
- (NSString*) firstName;
- (void) setFirstName: (NSString*)input;
- (NSString*) lastName;
- (void) setLastName: (NSString*)input;
- (NSUInteger) age;
- (void) setAge: (NSUInteger)input;
@end

@implementation Person
// Initializers
/**
 Initializes a new instance of the Person class.
 @returns a newly initialized object
 */
- (id)initPerson {
    if ( self = [super init] ) {
    }
    return self;
}

/**
 Initializes a new instance of the Person class with
 @param firstName The First Name
 @param lastName The Last Name
 @param age The Age
 @returns a newly initialized object
 */
- (id)initPersonWithFirstName:(NSString*)firstName LastName:(NSString*)lastName Age:(NSUInteger)age {
    if ( self = [super init] ) {
        [self setFirstName:firstName];
        [self setLastName:lastName];
        [self setAge:age];
    }
    return self;
}

// public accessors
- (NSString*) firstName {
    return _firstName;
}
- (void) setFirstName: (NSString*)input {
    [_firstName autorelease];
    _firstName = [input retain];
}
- (NSString*) lastName {
    return _lastName;
}
- (void) setLastName: (NSString*)input {
    [_lastName autorelease];
    _lastName = [input retain];
}
- (NSUInteger) age {
    return _age;
}
- (void) setAge: (NSUInteger)input {
    [_age autorelease];
    _age = [input retain];
}
@end

Upvotes: 1

Views: 2523

Answers (1)

walkytalky
walkytalky

Reputation: 9543

NSUInteger is a numeric type, not an object. You can't send messages to it and it doesn't need memory management (unless you're malloc-ing on the heap, which is a different process anyway and isn't relevant here).

If you actually want an object type to hold a numeric value - which you almost certainly don't - use NSNumber. Otherwise, just treat it as you would an int or float. ie:

- (void) setAge: (NSUInteger)input {
    _age = input;
}

Upvotes: 8

Related Questions