How to store a NSUInteger using NSCoding?

后端 未结 4 1269
攒了一身酷
攒了一身酷 2021-02-05 04:03

How do I store a NSUInteger using the NSCoding protocol, given that there is no method on NSCoder like -encodeUnsignedInteger:(NSUIn

4条回答
  •  无人及你
    2021-02-05 04:40

    You're going to want to use NSCoder's

    encodeInt:ForKey:
    

    and

    decodeIntForKey:
    

    methods. So, in your case you would need :

    - (void)encodeWithCoder:(NSCoder *)encoder {
        [encoder encodeInt:count forKey:@"count"];
    }
    
    - (id)initWithCoder:(NSCoder *)decoder {
        self = [super init];
        if (self != nil) {
            count = [decoder decodeIntForKey:@"count"];
        }
        return self;
    }
    

    Hope that helps, also, there's plenty more encode "encode" methods, take a look at the documentation for NSCoder :D

提交回复
热议问题