Question

I want to add a category on NSBezierPath to return a CGPathRef for the path. Since this is a core foundation object, I cannot autorelease it, and the caller of the method is responsible for releasing it. By placing either "Copy" or "Create" as part of the method name, I think I am following the convention set by Apple here: https://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html

However, the static analyzer won't accept it unless I go even further, and—not only include create/copy as part of the name, but—actually start the method name with "copy" (or mutableCopy, etc).

However, doing so is in violation of best practice for naming category methods, as they should be prefixed to avoid naming collision.

What is the best way of achieving all of the following:

  1. Adding a method on a category on a Cocoa class
  2. … that returns a Core Foundation object
  3. … that will give no warnings in the analyzer
  4. … and are named according to best practices?
Was it helpful?

Solution

Something like this you mean:

//
//  NSBezierPath+MCAdditions.h
//
//  Created by Sean Patrick O'Brien on 4/1/08.
//  Copyright 2008 MolokoCacao. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "AnalyzerMacros.h"

@interface NSBezierPath (MCAdditions)

+ (NSBezierPath *)bezierPathWithCGPath:(CGPathRef)pathRef;
- (CGPathRef)cgPath CF_RETURNS_RETAINED;

- (NSBezierPath *)pathWithStrokeWidth:(CGFloat)strokeWidth;

- (void)fillWithInnerShadow:(NSShadow *)shadow;
- (void)drawBlurWithColor:(NSColor *)color radius:(CGFloat)radius;

- (void)strokeInside;
- (void)strokeInsideWithinRect:(NSRect)clipRect;

@end

It's not my code; I added the CF_RETURNS_RETAINED to help the static analyzer out.

The full reference to clang source annotations is available here.

OTHER TIPS

For clarification: the root class of CoreFoundation objects, CFTypeRef, is toll-free bridged with NSObject. That means you can autorelease CoreFoundation objects (after doing an appropriate cast).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top