加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Obj-C,’self’未设置为'[(super或self)init

发布时间:2020-12-14 18:13:45 所属栏目:百科 来源:网络整理
导读:我知道我不久前问了一个类似的问题,但我仍然有点不确定.同样的事情发生在几个地方. ‘self’时使用的实例变量未设置为'[(super或self)init …]的结果 一个 - (id)initWithCoder:(NSCoder *)decoder { if (![super init]) return nil; red = [decoder decodeF
我知道我不久前问了一个类似的问题,但我仍然有点不确定.同样的事情发生在几个地方.

‘self’时使用的实例变量未设置为'[(super或self)init …]的结果

一个

- (id)initWithCoder:(NSCoder *)decoder {
  if (![super init]) return nil;
  red = [decoder decodeFloatForKey:kRedKey];  //occurs here
  green = [decoder decodeFloatForKey:kGreenKey];
  blue = [decoder decodeFloatForKey:kBlueKey];
  return self;
}

- (id)initWithFrame:(CGRect)frame title:(NSString*)str sideUp:(BOOL)up{

    if(![super initWithFrame:frame]) return nil;

    int y;
    UIImage *img;

    if(up){
        img = [UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/graph/popup"];
        y = 5;
    }else{
        img = [UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/graph/popdown"];
        y = 14;
    }

    background = [[UIImageView alloc] initWithImage:img]; // occurs here

C

- (id) initWithFrame:(CGRect)frame {
    if(![super initWithFrame:frame]) return nil;

    UILabel *titleBackground = [[[UILabel alloc] initWithFrame:
            CGRectMake(0,480,40)] autorelease];
    titleBackground.backgroundColor = [UIColor whiteColor];
    [self addSubview:titleBackground];

    titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // occurs here

对于块A,这是正确的

self = [self init]; 
if( self != nil )
{

和B& C

- (id) initWithFrame:(CGRect)frame {
   super = [super initWithFrame:frame]
    if(super != nil)
   {

解决方法

一般来说,你应该写:

self = [super init...];  // Potentially change "self"
if (self) {
    something = x;
    another = y;
}
return self;

这是因为在某些情况下init可能不会返回原始的self值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读