Convert CGColorRef address to UIColor

674 Views Asked by At

My app uses Game Center to send a UIColor to another player. I am using structs and therefor the UIColor is represented as a CGColorRef. I am having trouble converting this CGColorRef back to a UIColor on the other end as the UIColor's method colorWithCGColor does not take an address.

typedef struct {
    MessageType messageType;
} Message;

typedef struct {
    Message message;
    CGColorRef color;
} MessageTypeColor;

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data
   fromPlayer:(NSString *)playerID {
     Message *message = (Message*)[data bytes];
     if (message->messageType == kMessageTypeColor) {
          MessageTypeColor messageTypeColor;
          [data getBytes:&messageTypeColor length:sizeof(MessageTypeColor)];
          UIColor * color = [UIColor colorWithCGColor: messageTypeColor.color];
     }
}

The app crashes when it attempts to convert the CGColorRef to a UIColor. I printed the CGColorRef to the log, it is not nil.

(lldb) p messageTypeColor.color
(CGColorRef) $0 = 0x00000001742b0f80
(lldb) p [UIColor colorWithCGColor: messageTypeColor.color]
error: cannot initialize a parameter of type 'CGColor *' with an lvalue of type 'CGColorRef' (aka 'CGColor *')
error: 1 errors parsing expression
0

There are 0 best solutions below