When I write the following code in ARC mode, the compiler reports an error.
Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned 'objects
@ Interface ViewController: UIViewController {
NSString * newTitle;
}
@ Property (strong, nonatomic) NSString * newTitle;
. M
@ Synthesize newTitle;
This is because this naming convention is unreasonable in the ARC mode of the High-version compiler. You can refer to the memory management documents on the official website of Apple to describe the memory management rules.
You take ownership of an object if you create it using a method whose name begins with "alloc", "new", "copy", or "mutableCopy ".
The getter and setter methods are generated when @ synthesize includes the new attribute. If the new header is used, the newTitle method is called when the getter is generated.
The new object, instead of the original get attribute, prompts an error message.
Solution:
1. Add other characters before new, such as theNewTitle.
@property (strong, nonatomic) NSString *theNewTitle;
2. Override the getter Method
@property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;
3. The third method is to start with "new", but to tell the compiler that it is not a new object.
# 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
4. That's okay.
@synthesize newTitle = _newTitle; // Use instance variable _newTitle for storage
The Transitioning to ARC Release Notes document provided by Apple indicates that developers should not start with new or copy when naming.
Unacceptable Object Names
- NewButton
- NewLabel
- NewTitleAcceptable Object Names