如何在iOS中初始化整数?
我定义如何在iOS中初始化整数?,ios,objective-c,Ios,Objective C,我定义 @property (nonatomic, assign) int currentUserNum; @property (nonatomic, assign) BOOL isAlive; 在@接口MyClass中 @synthesize currentUserNum, isAlive; -(id) init { if (self = [super init]) { self.currentUserNum = 0; self.isAlive = YES; }
@property (nonatomic, assign) int currentUserNum;
@property (nonatomic, assign) BOOL isAlive;
在@接口MyClass中
@synthesize currentUserNum, isAlive;
-(id) init {
if (self = [super init]) {
self.currentUserNum = 0;
self.isAlive = YES;
}
return self;
}
并在@implementation MyClass
@synthesize currentUserNum, isAlive;
-(id) init {
if (self = [super init]) {
self.currentUserNum = 0;
self.isAlive = YES;
}
return self;
}
self.currentUserNum=0代码>崩溃,但self.isAlive=YES代码>可以工作!它们都是assign
属性
我想知道为什么?谢谢 您的init
方法缺少许多重要代码
- (id)init {
if ((self = [super init])) {
_currentUserNum = 0; // it's not wise to reference properties in the init method
}
return self;
}
每个init
方法都应该遵循这个基本模式。您可以为self
指定调用适当的super init
或其他self init
的值。如果这不是nil
,则执行适当的初始化代码,最后返回self
您的init
方法缺少许多重要代码
- (id)init {
if ((self = [super init])) {
_currentUserNum = 0; // it's not wise to reference properties in the init method
}
return self;
}
每个init
方法都应该遵循这个基本模式。您可以为self
指定调用适当的super init
或其他self init
的值。如果这不是nil
,那么您将执行相应的初始化代码,最后在init中返回self
,为什么在调用[super init]后没有返回self?对不起,我有super method当应用程序崩溃时会出现什么错误?@eligoryint
的保留计数?我不这么认为……是的,xcode不能保留int。在init中,为什么在调用[super init]后没有返回self?对不起,我有super method应用程序崩溃时出现了什么错误?@eligory一个int
的保留计数?我不这么认为……是的,xcode不能保留int。对不起,我有超级方法并更新了我的问题。还有一个问题:\u currentUserNum=0
=currentUserNum=0
?你知道,我已经定义了@synthesis currentUserNum
根据编译器和@synthesis,ivar将被命名为currentUserNum
或\u currentUserNum
。一个会编译,一个不会。谢谢你的回答,我想我需要更新我的问题,请再次检查!对不起,我有超级方法和更新我的问题。还有一个问题:\u currentUserNum=0
=currentUserNum=0
?你知道,我已经定义了@synthesis currentUserNum
根据编译器和@synthesis,ivar将被命名为currentUserNum
或\u currentUserNum
。一个会编译,一个不会。谢谢你的回答,我想我需要更新我的问题,请再次检查!