iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – Obj-C,’self’未设置为'[(super或self)init …]的结果时使用的实例变量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我知道我不久前问了一个类似的问题,但我仍然有点不确定.同样的事情发生在几个地方. ‘self’时使用的实例变量未设置为'[(super或self)init …]的结果 一个 - (id)initWithCoder:(NSCoder *)decoder { if (![super init]) return nil; red = [decoder decodeFloatForKey:kRed
我知道我不久前问了一个类似的问题,但我仍然有点不确定.同样的事情发生在几个地方.

‘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@R_139_6964@BACkground];

    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值.

大佬总结

以上是大佬教程为你收集整理的objective-c – Obj-C,’self’未设置为'[(super或self)init …]的结果时使用的实例变量全部内容,希望文章能够帮你解决objective-c – Obj-C,’self’未设置为'[(super或self)init …]的结果时使用的实例变量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。