语义问题:属性的合成 getter 遵循 Cocoa 命名约定以返回“拥有"对象

时间:2023-03-24
本文介绍了语义问题:属性的合成 getter 遵循 Cocoa 命名约定以返回“拥有"对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在使用 iOS 5 SDK 尝试开发我的应用程序.我正在尝试使 NSString 成为属性,然后在 .m 文件中合成它(我之前已经这样做了,没有任何问题).现在,我遇到了这个问题:语义问题:属性的合成 getter 遵循 Cocoa 命名约定以返回‘拥有’对象."

I'm currently using the iOS 5 SDK trying to develop my app. I'm trying to make an NSString a property, and then to synthesize it in the .m file (I have done this before with no issues). Now, I came across this: "Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects."

这是我的代码:.h

@interface ViewController : UIViewController {
     NSString *newTitle;
}
@property (strong, nonatomic) NSString *newTitle;

.m

@synthesize newTitle;

有人知道我该如何解决这个问题吗?谢谢!!

Does anyone have a clue how I could fix this? Thanks!!

推荐答案

我猜你使用的编译器版本如下 内存管理规则 用于声明的属性——更具体地说,用于声明的属性的访问器:

My guess is that the compiler version you’re using follows the memory management rules for declared properties, too — more specifically, for declared properties’ accessors:

如果您使用名称以alloc"、new"、copy"或mutableCopy"开头的方法创建对象,您将获得该对象的所有权.

You take ownership of an object if you create it using a method whose name begins with "alloc", "new", "copy", or "mutableCopy".

一个名为 newTitle 的属性在合成时会产生一个名为 -newTitle 的方法,因此会出现警告/错误.-newTitle 应该是 newTitle 属性的 getter 方法,但是命名约定规定名称以 new 开头的方法返回一个对象它归调用者所有,而 getter 方法则不然.

A property named newTitle, when synthesised, yields a method called -newTitle, hence the warning/error. -newTitle is supposed to be a getter method for the newTitle property, however naming conventions state that a method whose name begins with new returns an object that’s owned by the caller, which is not the case of getter methods.

您可以通过以下方式解决此问题:

You can solve this by:

  1. 重命名该属性:

  1. Renaming that property:

@property (strong, nonatomic) NSString *theNewTitle;

  • 保留属性名称并指定不以特殊方法名称前缀之一开头的 getter 名称:

  • Keeping the property name and specifying a getter name that doesn’t begin with one of the special method name prefixes:

    @property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;
    

  • 同时保留属性名和getter名,并告诉编译器,即使getter名以new开头,它也属于nonenew 方法族相对的方法族:

  • Keeping both the property name and the getter name, and telling the compiler that, even though the getter name starts with new, it belongs to the none method family as opposed to the new method family:

    #ifndef __has_attribute
    #define __has_attribute(x) 0  // Compatibility with non-clang compilers
    #endif
    
    #if __has_attribute(objc_method_family)
    #define BV_OBJC_METHOD_FAMILY_NONE __attribute__((objc_method_family(none)))
    #else
    #define BV_OBJC_METHOD_FAMILY_NONE
    #endif
    
    @interface ViewController : UIViewController
    @property (strong, nonatomic) NSString *newTitle;
    - (NSString *)newTitle BV_OBJC_METHOD_FAMILY_NONE;
    @end
    

    请注意,即使此解决方案允许您将 newTitle 保留为属性名称和 getter 名称,但有一个名为 -newTitle 的方法不会返回调用者拥有的对象可能会让其他阅读您的代码的人感到困惑.

    Note that even though this solution allows you to keep newTitle as both the property name and the getter name, having a method called -newTitle that doesn’t return an object owned by the caller can be confusing for other people reading your code.

    <小时>

    作为记录,Apple 已发布 Transitioning到 ARC 发行说明,他们在其中声明:

    您不能为属性指定以 newcopy 开头的名称.

    You cannot give a property a name that begins with new or copy.

    他们已经被告知他们的陈述不太准确:罪魁祸首是 getter 方法名称,而不是属性名称.

    They’ve already been notified that their statement is not quite accurate: the culprit is the getter method name, not the property name.

    2015 年 1 月 17 日我刚刚注意到一个 最近对 Clang 的提交建议上面的选项 3(使用 objc_method_family(none)),包括一个修复它,用于属性名称与特殊方法系列前缀之一匹配的一般情况.Xcode 最终可能会包含此更改.

    Edit 17 Jan 2015: I’ve just noticed a recent commit to Clang that suggests option 3 above (using objc_method_family(none)), including a fix-it, for the general case where a property name matches one of the special method family prefixes. Xcode will likely incorporate this change eventually.

    这篇关于语义问题:属性的合成 getter 遵循 Cocoa 命名约定以返回“拥有"对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

  • 上一篇:试图在视图控制器之间传递数据 下一篇:NSManagedObjectContext 的 performBlock: 是做什么用的?

    相关文章