gpt4 book ai didi

objective-c - 静态分析和 "create rule"

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:30 24 4
gpt4 key购买 nike

我想在 NSBezierPath 上添加一个类别以返回路径的 CGPathRef。由于这是一个核心基础对象,我不能自动释放它,方法的调用者负责释放它。通过将“复制”或“创建”作为方法名称的一部分,我认为我遵循了 Apple 在此处设定的约定:https://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html

但是,静态分析器不会接受它,除非我更进一步,并且——不仅包括 create/copy 作为名称的一部分,而且——实际上以“copy”(或 mutableCopy 等)开始方法名称.

但是,这样做违反了命名类别方法的最佳实践,因为它们应该加上前缀以避免命名冲突。

实现以下所有目标的最佳方式是什么:

  1. 在 Cocoa 类的类别上添加方法
  2. ... 返回一个 Core Foundation 对象
  3. …在分析器中不会给出警告
  4. ……并根据最佳实践命名?

最佳答案

你的意思是这样的:

//
// 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

这不是我的代码;我添加了 CF_RETURNS_RETAINED 来帮助静态分析器。

clang 源注释的完整引用可用 here .

关于objective-c - 静态分析和 "create rule",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11795575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com