Reputation: 1643
Currently I m using Xcode 4.2 with ARC.
Below is my code
.h file
@property (readonly, nonatomic) NSString *username;
.m file
@synthesize username = _username;
I got error in .m file like this
ARC forbids synthesizing a property of an objective-c object with unspecified ownership or storage attribute
Upvotes: 0
Views: 154
Reputation: 1643
In ARC assign it would be released anywhere. For pointer variable "strong" only more comfortable. I modified the code in .h file by below line
@property (nonatomic, strong) NSString *username;
Upvotes: 0
Reputation: 1752
Please try below :
@property (nonatomic,strong) NSString
*username;
Upvotes: 0
Reputation: 644
@property (nonatomic, strong) NSString *userName;
In ARC it automatically release the pointer variable memory...
Upvotes: 1
Reputation: 150615
When you are creating a property for an Object, you need to specify the memory management semantics that will be used. assign
is used for scalar values, such as structs, on numbers, but strings are objects so you need to specify whether it is weak
, or strong
or copy
.
strong
is the ARC equivalent of retain
in non-ARC code. It says that the property will maintain a strong reference to the object.
weak
is specific to ARC. It means that although the property has an object reference it is not holding on to it. Also, if the object is released the reference will become nil
. This is used, for example, when storing a reference to a delegate.
copy
means that the property will hold a strong reference to a copy of the object. This is particularly useful if you are holding on to a reference that may be changed outside of the class.
Despite what the other answers have said, you should be using copy
for NSString
, NSArray
, NSSet
, NSDictionary
. That is because these classes are part of class clusters, which have mutable counterparts. e.g. NSMutableString
etc. Which means that although you my think you have an immutable object, it may actually be a mutable one. So setting the property to a copy means that it is unlikely to be changed from under you without you knowing about it.
the correct declaration should be:
@property (copy, readonly, nonatomic) NSString *username;
It's strange that you need to specify a memory management specifier for a readonly property, I agree. It's possible that this might change in the future, other aspects of properties have changed (autosynthesis, automatic creation of backing iVars, etc). However, this means it is easier to make your property readwrite in the private implementation of the class by redeclaring it in a class extension in the .m file:
// in the .m file
@interface YourClass ()
@property (copy, readwrite, nonatomic) NSString *username;
@end
So now, within your class; you can read and write to the property, but externally, other classes can only read the property.
Upvotes: 3
Reputation: 15015
While writing in arc you should declare the ivar whether strong or weak.
Upvotes: 0
Reputation: 17535
Try this in .h file
@property (strong, nonatomic) NSString *username;
Upvotes: 0
Reputation: 4019
Change your property declaration to:
@property (nonatomic,strong) NSString *username;
so that ARC knows it should be retained. This would have compiled before ARC but it would be dangerous since the default was assign
and the color would have been released unless it was retained elsewhere.
I would highly recommend the WWDC2011 video about ARC.
Upvotes: 0