Encoding CGPoint struct with NSCoder

后端 未结 3 1007
别跟我提以往
别跟我提以往 2021-02-07 10:19

How do you encode and decode a CGPoint struct using NSCoder?

3条回答
  •  故里飘歌
    2021-02-07 10:36

    To encode:

    CGPoint point = /* point from somewhere */
    NSValue *pointValue = [NSValue value:&point withObjCType:@encode(CGPoint)];
    [coder encodeObject:pointValue forKey:@"point"];
    

    To decode:

    NSValue *decodedValue = [decoder decodeObjectForKey:@"point"];
    CGPoint point;
    [decodedValue getValue:&point];
    

提交回复
热议问题