Naming rules under iOS ARC

Source: Internet
Author: User

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
    • _ NewButton
    • MewLabel
    • NeueTitle

      # Arc # auto-synthesized # xcode-4.6.1





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.