【问题标题】:Core Foundation and ARC核心基础和 ARC
【发布时间】:2013-11-26 15:19:12
【问题描述】:

我在 UIView 子类中有这样的属性

@property(nonatomic, assign) CTTypesetterRef typesetter;

我在视图构造函数中启动 TypeSetter:

- (id)initWithFrame:(CGRect)frame andAttributedString:(NSMutableAttributedString * )   pageAttributedString
{
    self = [super initWithFrame:frame];
    if (self) {
            self.attributedString = pageAttributedString;
            self.typesetter = CTTypesetterCreateWithAttributedString((CFAttributedStringRef)CFBridgingRetain(self.attributedString));
}

问题是在哪里使用 CFRelease 释放 TypeSetter?

我正在使用 ARC、IOS 7 和 Xcode 5

【问题讨论】:

    标签: ios memory-management automatic-ref-counting core-foundation


    【解决方案1】:

    在您的 -dealloc 中,就像您对 MRR 所做的那样。

    请记住,与 MRR 不同,在 ARC 中,您不必调用 [super dealloc],也不需要在 ARC 管理的 ivars 上显式调用 -release。

    【讨论】:

    • 你确实需要实现 dealloc 来释放 CF 类型的属性。这不是很明显,但是 ARC 的要求,因为 ARC 不管理非 Objective-C 对象的内存。
    • 我想这就是我刚才所说的 ;) CF 类型属性是一种非 ARC 管理的 ivars,您必须自己处理它们的生命周期。哦,当我写“你不需要打电话给-release”时,我的意思是真的,你不必自己将你的ARC管理的ivars设置为零,ARC会这样做。你实际上不能在你的 ivars 上调用 -release ;)
    • 你是正确的 JTAS,我解决了这个问题,我在其子层中使用强属性引用了一个父视图,因此从未调用 dealloc @property(nonatomic, strong) CoreTextView * coreTextView;正确的是@property(nonatomic, weak) CoreTextView * coreTextView;所以释放的正确位置是dealloc。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多