如何在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当应用程序崩溃时会出现什么错误?@eligory
int
的保留计数?我不这么认为……是的,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
。一个会编译,一个不会。谢谢你的回答,我想我需要更新我的问题,请再次检查!