Iphone 无法保留CGPath对象(出现警告)

Iphone 无法保留CGPath对象(出现警告),iphone,ios,cocoa-touch,core-graphics,Iphone,Ios,Cocoa Touch,Core Graphics,我有一个实例变量CGMutablePathRef\u mutablePath,我将其设置为@property(非原子)CGMutablePathRef mutablePath。我重写setter方法: - (void) setMutablePath:(CGMutablePathRef)mutablePath { if (_mutablePath) { CGPathRelease(_mutablePath); _mutablePath = NULL;

我有一个实例变量
CGMutablePathRef\u mutablePath
,我将其设置为
@property(非原子)CGMutablePathRef mutablePath。我重写setter方法:

- (void) setMutablePath:(CGMutablePathRef)mutablePath
{
    if (_mutablePath)
    {
        CGPathRelease(_mutablePath);
        _mutablePath = NULL;
    }

    _mutablePath = CGPathRetain(mutablePath);
}
然而,我在这一行得到一个警告:
\u mutablePath=cgpathrain(mutablePath)表示:

Assigning to 'CGMutablePathRef' (aka 'struct CGPath *') from 'CGPathRef' (aka 'const struct CGPath *') discards qualifiers
为什么这不起作用?当我这样做时,这似乎适用于CT(核心文本)对象。我尝试了一系列不同的强制转换,但无法消除错误,任何建议都将不胜感激。

cgpathrain()
声明为

CGPathRef CGPathRetain(CGPathRef path);

这意味着它返回一个
CGPathRef
,而不是
CGMutablePathRef
。在将结果分配给您的
\u mutablePath

ivar之前,您应该将结果强制转换回
CGMutablePathRef

谢谢。我一直在CGPathRetain调用的内部而不是外部执行强制转换,这是我犯的愚蠢错误。虽然与问题没有直接关系,但您应该避免出现
mutablePath==\u mutablePath
的情况,因为您在保留之前进行了释放。谢谢。如果可变路径已经等于_mutablepath,那么这最终是为了防止浪费时间?或者是否还有其他可能发生的副作用(如果有的话,我看不到)。不,不仅仅是性能。调用release时,如果对象是唯一持有路径引用的对象,则将解除分配该对象。因此,当您调用retain时,它将无效/取消分配。你可以在发布前调用retain,或者显式地检查是否相等,来解决这个问题。我已经看到了这两种方法,我想这是个人品味的问题。这是发布前的保留<代码>CGMutablePathRef tmp=(CGMutablePathRef)CGPathRetain(mutablePath);如果(_mutablePath){cgpathlease(_mutablePath);_mutablePath=NULL;}_mutablePath=tmp
@idz:
tmp
是不必要的,因为
CGPathRetain()
必须返回其输入参数。您可以只说
cgpathrain(mutablePath);CGPathRelease(_mutablePath)_可变路径=可变路径,没有任何条件。