问题
I'm extending Facebook’s Chisel to be able to visualize a color from the debugger. I want it to work for UIColor
, CIColor
, and CGColorRef
. The two object-based ones are working fine, but the CGColorRef
is giving me trouble.
Here is the bug I'm working from, where I've already hashed out a bunch of the stuff from this question.
I've boiled the issue down to this test case:
If I have some function:
+ (UIColor *)someColor {
UIColor *uiColor = [UIColor redColor];
CGColorRef cgColor = uiColor.CGColor;
UIColor *newUIColor = [UIColor colorWithCGColor:cgColor];
return newUIColor;
}
And I set a breakpoint on the return newUIColor;
line, this is what happens in LLDB:
(lldb) po cgColor
<CGColor 0x7f992b626710> [<CGColorSpace 0x7f992b70d5b0> (kCGColorSpaceDeviceRGB)] ( 0 0.478431 1 1 )
(lldb) po [UIColor colorWithCGColor:cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an lvalue of type 'CGColorRef' (aka 'CGColor *')
error: 1 errors parsing expression
(lldb) po [UIColor colorWithCGColor:(CGColorRef)cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an rvalue of type 'CGColorRef' (aka 'CGColor *')
error: 1 errors parsing expression
(lldb) po [UIColor colorWithCGColor:(id)cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an rvalue of type 'id'
error: 1 errors parsing expression
(lldb) po [UIColor colorWithCGColor:(CGColor *)cgColor]
error: use of undeclared identifier 'CGColor'
error: expected expression
error: 2 errors parsing expression
(lldb) expr @import CoreGraphics
(lldb) po [UIColor colorWithCGColor:(CGColorRef)cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an rvalue of type 'CGColorRef' (aka 'CGColor *')
error: 1 errors parsing expression
(lldb) po [UIColor colorWithCGColor:(CGColor *)cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an rvalue of type 'CGColor *'
error: 1 errors parsing expression
(lldb) po [UIColor colorWithCGColor:(struct CGColor *)cgColor]
error: cannot initialize a parameter of type 'CGColor *' with an rvalue of type 'struct CGColor *'
error: 1 errors parsing expression
So, how can I get lldb
to execute a line that takes a parameter of type CGColorRef
?
回答1:
I was able to get this to work using:
(lldb) po [[UIColor alloc] initWithCGColor:cgColor]
UIDeviceRGBColorSpace 1 0 0 1
From UIColor.h
:
@property(nonatomic,readonly) CGColorRef CGColor;
- (CGColorRef)CGColor NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;
So I believe the UIColor
is going out of scope and the memory of the .CGColor
is no longer valid. I would probably have to copy it, but for my purposes, using the -initWithCGColor:
works fine.
来源:https://stackoverflow.com/questions/31810543/cant-pass-parameter-of-type-cgcolorref-in-lldb