问题描述
我已经为所有字段自动以相同的方式生成了此源代码,为什么编译器会告诉我NSUInteger上的Invalid Receiver类型不是NSString,而是以相同的方式创建:
/**类代表一个人*/@interface人员:NSObject {//私人成员NSString * _firstName;NSString * _lastName;NSUInteger _age;}//初始化器/**初始化Person类的新实例.@返回一个新初始化的对象*/-(id)initPerson;/**使用初始化Person类的新实例@param firstName名字@param lastName姓氏@param age年龄@返回一个新初始化的对象*/-(id)initPersonWithFirstName:(NSString *)firstName LastName:(NSString *)lastName Age:(NSUInteger)age;//公共访问器-(NSString *)firstName;-(void)setFirstName:(NSString *)input;-(NSString *)lastName;-(void)setLastName:(NSString *)input;-(NSUInteger)年龄;-(void)setAge:(NSUInteger)input;@结尾@实现人//初始化器/**初始化Person类的新实例.@返回一个新初始化的对象*/-(id)initPerson {如果(self = [super init]){}回归自我}/**使用初始化Person类的新实例@param firstName名字@param lastName姓氏@param age年龄@返回一个新初始化的对象*/-(id)initPersonWithFirstName:(NSString *)firstName姓氏:(NSString *)lastName年龄:(NSUInteger)age {如果(self = [super init]){[self setFirstName:firstName];[self setLastName:lastName];[self setAge:age];}回归自我}//公共访问器-(NSString *)firstName {返回_firstName;}-(void)setFirstName:(NSString *)input {[_firstName自动发布];_firstName = [输入保留];}-(NSString *)lastName {返回_lastName;}-(void)setLastName:(NSString *)input {[_lastName自动发布];_lastName = [输入保留];}-(NSUInteger)年龄{返回_age;}-(void)setAge:(NSUInteger)input {[_age自动发布];_age = [输入保留];}@结尾
NSUInteger
是数字类型,而不是对象.您不能向其发送消息,它也不需要内存管理(除非您在堆上 malloc
-ing,无论如何这都是一个不同的过程,在这里与此无关)./p>
如果您实际上希望对象类型保存一个数值-您几乎肯定不希望这样-请使用 NSNumber
.否则,就像对待 int
或 float
一样对待它.即:
-(void)setAge:(NSUInteger)input {_age =输入;}
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
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;
}
这篇关于目标C:为什么在NSUInteger而不是NSString上无效的接收器类型,却以相同的方式创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!