Making the getter/setter of an int? - objective-c

#property (retain) int myInteger;
That throws me an error because apparently int is not considered an object... but I want to get the advantage of creating a getter/setter method with the #synthetize thing, but with an int. How could I achieve so? Is there an equivalent?

#property (assign) int chunkID;
or
#property (readonly) int chunkID;
You cannot retain a primitive type like integers. Only objects can be retained...

Use this:
#property (nonatomic, assign) int chunkID;
assign is the default so you might want to leave it out.
You need to use the assign type of property because you are dealing with a primitive object type (i.e. int). This kind of type can't be retained.
Only subclasses of NSObject can be retained / released.

Related

In case of #property without ivar what's difference between strong, weak and assign?

I want to declare #property without ivar. Is there any difference if I declare #property with strong, weak or assign ?
The #property declaration defines a contract with the user's of the property. If it is declared as weak, for example, then the user of the property knows that the passed object won't be retained.
This contract should be true whether the property is backed by an ivar or not.
In your case, the property should be strong, weak, or assign depending on your implementation of your property and the contract you wish to provide for the property.
It depends of what type the property is. For example for primitive types like NSInteger you can't declare property with strong or weak attributes. Xcode will show you an error.
So these declarations are invalid:
#property (nonatomic, strong) NSInteger max;
#property (nonatomic, weak) NSInteger max;
#property (nonatomic, copy) NSInteger max;
because strong, weak and copy are attributes of object types and NSInteger isn't object type. It's primitive type. Thus, following declaration is valid:
#property (nonatomic, assign) NSInteger max;
Attributes assign, weak, strong, copy and unsafe_unretained specify memory management rules for ivar that is backed by property. So if you are not going to create ivar for property (suppose you want to provide your own getter/setter and property will not store any value) it doesn't matter what attribute you'll specify. But as I mentioned above you couldn't specify attributes of object types for primitive types.

What's the difference the member variable between in a interface and #property (nonatomic, strong)?

#interface ChargeView (){
NSString* billid;
int clickRow;
NSMutableArray *arr1;
}
#property (nonatomic, strong) NSMutableArray *arr2;
What's the difference between arr1 and arr2? Which is better or write it anywhere if I like?
The property also generates the accessors -(NSMutableArray *)arr2 (getter) and -(void)setArr2:(NSMutableArray *)arr2 (setter). It further generates a corresponding instance variable _arr2 (the underscore is convention for ivars). The attributes of the property determine the behavior of the accessors. For instance, if the property is marked atomic, the accessors will synchronize access to the ivar.
On the other hand, arr1 is just an ivar and you have to write accessors (if you need any) yourself.

obj-c, how do I create a property and synthesize an NSUInteger?

I'm having some trouble using an NSUInteger, I've tried various things and googled, but not found the answer ?
I have... I also tried ... nonatomic, retain
#property (readwrite, assign) NSUInteger *anAmount;
#synthesize anAmount;
error: type of property 'anAmount' does not match type of ivar 'anAmount'
Also when I release it in dealloc I get a warning..
warning: invalid receiver type 'NSUInteger'
Remove the * if you're not intending to use a pointer to an NSUInteger (which is a value type, and not a pointer type):
#property (readwrite, assign) NSUInteger anAmount;
And do not release it.
Another possibility is to use NSNumber declaring and initializing below. NSNumber provides some helper methods that may be of interest to you.
#property (readwrite, copy) NSNumber showMeTheMoney;
showMeTheMoney = [NSNumber numberWithUnsignedInt:1234567890];

Objective-C Property - Difference Between Retain and Assign

I think I am missing something about property attributes.
First, I can't understand the difference between retain and assign.
If I use assign, does the property increase the retain counter by 1 to the setter and also to the getter, and do I need to use release to both of them?
And how does this work with readwrite or copy? From the view of a retain count.
I am trying to understand when i need to use release after working with a property (setter and getter)
#property (readwrite,assign) int iVar;
What does assign do here?
What is the difference between:
#property (readwrite,assign) int iVar;
and
#property (readwrite,retain) int iVar;
and
#property (readwrite) int iVar;
Many thanks...
what is the different between : #property (readwrite,assign) int iVar; to #property (readwrite,retain) int iVar; to #property (readwrite) int iVar;
The setter for #property (readwrite,assign) sometype aProperty; is semantically equivalent to
-(void) setAProperty: (sometype) newValue
{
ivar = newValue;
}
The above is more or less what you will get if you put
#asynthesize aProperty = ivar;
in your implementation.
The setter for #property (readwrite,retain) sometype aProperty; is semantically equivalent to
-(void) setAProperty: (sometype) newValue
{
[newValue retain];
[ivar release];
ivar = newValue;
}
Clearly, it makes no sense to retain or release an int, so sometype must be either id or SomeObjectiveCClass*
The setter for #property (readwrite,copy) sometype aProperty; is semantically equivalent to
-(void) setAProperty: (sometype) newValue
{
sometype aCopy = [newValue copy];
[ivar release];
ivar = aCopy;
}
In this case, not only must sometype be an objective C class but it must respond to -copyWithZone: (or equivalently, implement NSCopying).
If you omit retain or assign or copy, the default is assign.
By the way, I have simplified the above by not considering the locking that occurs because the properties don't also specify nonatomic.
There are two kind of specifiers:
The readwrite specifier tells that the property will be read/write, so when you do a # synthesize it will create both the getter and the setter.
There's also readonly, to specify that the property will only have a getter.
The other modifiers specify how the properties will behave respect of the reference counting:
The assign modifier, tells that the ivar will simply be assigned with whatever the setter receives. So, in case of an object, retain won't be called.
With retain, whenever you use the synthesized setter, retain will be called, so the object will be retained. This means that the class that has the setter needs to release it at some point (probably in its dealloc method).
As for copy, it means that instead of retain, the object will receive a copy message. This means that you'll end up with a copy of the original object, with a retain count of one, so again, you are responsible of releasing it.

Objective C problem defining properties

When I attempt to define an int as a property in Objective C i get an error (not of type object). I have tried using NSInteger and int but neither work.
code:
int seat;
#property (nonatomic, retain) int seats;
You cannot retain an int or a NSInteger as they are primitive types - not objects. Use the following instead:
#property (nonatomic, assign) int seats;