代码之家  ›  专栏  ›  技术社区  ›  suse

在Objective-C中编译int变量

  •  1
  • suse  · 技术社区  · 14 年前

    如何在Objective-C中解码和编码int变量?

    这是我到目前为止所做的,但应用程序将在此时终止。

    这里有什么错误?

    -(void)encodeWithCoder:(NSCoder*)coder
    {
       [coder encodeInt:count forKey:@"Count"];
    }
    
    -(id)initWithCoder:(NSCoder*)decoder
    {
       [[decoder decodeIntForKey:@"Count"]copy];
       return self;
    }
    
    2 回复  |  直到 14 年前
        1
  •  8
  •   V1ru8    14 年前

    [decoder decodeIntForKey:@"Count"] 返回一个 int . 还有你的留言 copy 为了这个 -&燃气轮机;撞车。

    在Objective-C中,简单的数据类型不是对象。所以你不能给他们发信息。int是简单的c数据类型。

        2
  •  6
  •   Alex Zavatone    9 年前

    - (void)encodeWithCoder:(NSCoder *)coder {
        [coder encodeObject:[NSNumber numberWithInt:self.count] forKey:@"Count"];
    }
    
    - (id)initWithCoder:(NSCoder *)decoder {
        if (self = [super init]) {
            self.count = [[decoder decodeObjectForKey:@"Count"] intValue];
        }
        return self;
    }