Ios 将电桥CFType转换为支持弧

Ios 将电桥CFType转换为支持弧,ios,objective-c,automatic-ref-counting,Ios,Objective C,Automatic Ref Counting,有人能帮我转换下面的代码来支持ARC吗 UIFont *boldFont = [UIFont fontWithName:@"DINPro-Medium" size:14]; CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL); CGColorRef cgColor = UIColorFromRGB(0x2b776d).CGCol

有人能帮我转换下面的代码来支持ARC吗

UIFont *boldFont = [UIFont fontWithName:@"DINPro-Medium" size:14];
CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
CGColorRef cgColor = UIColorFromRGB(0x2b776d).CGColor;

NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];
NSDictionary *ULSubattributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     CFBridgingRelease(ctBoldFont), (id)(id)kCTFontAttributeName,
                                     cgColor, (id)kCTForegroundColorAttributeName,underline, (NSString*)kCTUnderlineStyleAttributeName, nil];
我在最后一句话中听到一个提示

*** -[Not A Type retain]: message sent to deallocated instance 0x20ad4910
*** -[Not A Type class]: message sent to deallocated instance 0x20ad4910
我希望这是一个重复的问题。但是我被卡住了。谢谢

/* OK */UIFont *boldFont = [UIFont fontWithName:@"DINPro-Medium" size:14];
assert(boldFont); // just be sure
/* OK */CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
assert(boldFont); // just be sure

/* 1) CGColorRef cgColor = UIColorFromRGB(0x2b776d).CGColor; */
CGColorRef cgColor = CGColorCreateCopy( UIColorFromRGB(0x2b776d).CGColor );
assert(cgColor); // just to be sure

/* OK */NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];

NSDictionary *ULSubattributes = @{ 
/* you don't own the kCT strings */(__bridge NSString *)kCTFontAttributeName : CFBridgingRelease(ctBoldFont),
/* ... */   (__bridge NSString *)kCTForegroundColorAttributeName : CFBridgingRelease(cgColor),
/* ... */   (__bridge NSString *)kCTUnderlineStyleAttributeName : underline };
1) iOS-iOS6之前版本中存在一个漏洞,ARC将在您仍在使用CGColorRef时释放支持CGColorRef的UIColor。我知道这一点,因为我创建了一个rdar,然后在iOS 6中报告为已修复

附:迈克·阿什写的一篇不错的博客

编辑:这段代码在我的iOS演示应用程序中运行良好(部署设置为5.1和6),请参阅日志消息。我把口述做成了一个ivar

{

    UIFont *boldFont = [UIFont fontWithName:@"Helvetica" size:14];
    assert(boldFont); // just be sure
    CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
    assert(boldFont); // just be sure

    CGColorRef cgColor = CGColorCreateCopy( [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:0.8f].CGColor );
    assert(cgColor);

    NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];
    assert(underline);

    ULSubattributes = @{ 
        (__bridge NSString *)kCTFontAttributeName : CFBridgingRelease(ctBoldFont),
        (__bridge NSString *)kCTForegroundColorAttributeName : CFBridgingRelease(cgColor),
        (__bridge NSString *)kCTUnderlineStyleAttributeName : underline };

    NSLog(@"ULSubattributes: %@", ULSubattributes);
}
2013-04-12 11:48:20.294 WebViewScrollTester[44147:c07] ULSubattributes:{ CTForegroundColor=“[(kCGColorSpaceDeviceRGB)](0.80.80.80.8)”; NSFont=“CTFont\nCTFontDescriptor{type=mutable dict,count=1,\n项=>\n\t1: {contents=\“NSFontNameAttribute\”}= {contents=\'Helvetica\'}\n}\n>“; NSUnderline=1;}

EDIT2:我甚至在之后添加了一个dispatch_以显示ARC没有发布任何内容:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
        {
            NSLog(@"ULSubattributes: %@", ULSubattributes);
        } );

下面是我的一些实际工作代码,它与您的代码非常相似:

 CTFontRef font2 =
     CTFontCreateCopyWithSymbolicTraits (
         basefont, f, nil, kCTFontBoldTrait, kCTFontBoldTrait);
 NSDictionary* d2 =
     @{(NSString*)kCTFontAttributeName: CFBridgingRelease(font2)};
 [mas addAttributes:d2 range:encRange];
请注意,我没有释放
font2
CfBrigingRelease
表示“ARC,此对象已被保留;到时候您将释放它。”ARC会这样做。没有泄漏,没有僵尸,没有崩溃,没有问题

(另一方面,
basefont
必须使用
CFRelease
明确释放,它是先前使用
CTFontCreateWithName
创建的,从未移交给ARC进行控制。)

下面是我书中的更多核心文本代码,展示了整个ARC内存管理:

以下是我书中关于使用ARC进行CFType内存管理的部分:


您能运行NSZombies并确定哪些内容发布过量吗?我认为这会导致您最后一行出现问题。。CfBrigingRelease(ctBoldFont)谢谢David,它在这里给了我相同的错误:(uu bridge NSString*)kCTForegroundColorAttributeName:CfBrigingRelease(cgColor)。