Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I declare a variable like "newVariable" in Obj-C? [duplicate]

Error I got

error: property's synthesized getter follows Cocoa naming convention for returning 'owned' objects
/Users/.../ViewController.h:12:40: 

note: property declared here
@property (nonatomic, retain)NSString *newString;

why can't I use new in the variable name at the beginning? (like newString)

like image 710
Lithu T.V Avatar asked Mar 12 '13 15:03

Lithu T.V


2 Answers

Declaring a property synthesizes two accessor (getter/setter) methods:

-(NSString*)newString;
-(void)setNewString:(NSString*)newString;

Objective-C has a naming convention for memory management that is enforced by the compiler. Methods that start with new (also "alloc", "copy", "mutableCopy") are required to return an object that will be "owned" by the caller. See the documentation. The generated accessor method doesn't follow the rule.

like image 82
progrmr Avatar answered Oct 19 '22 23:10

progrmr


This naming convention is already used in Objective-C. You will need to use something like just plain old myString to make this work. It's a compiler thing - compilers are picky.

like image 38
Undo Avatar answered Oct 19 '22 22:10

Undo