Can someone help me to understand retain and release method in objective c ?
What exactly they do?
I have a following sample code , just help me what happens when these methods executed by runtime environment ?
Every object has a "retainCount" which tells the runtime how many other objects need this one. When the retainCount reaches zero, the object is destroyed (the system calls dealloc). This count starts at 1 when you alloc an object, it goes up by 1 when you retain, and down by 1 when you release.
The code you gave is for removing an old value and adding a new one. It releases the old value so that it can be destroyed, and retains the new value so that it can not be destroyed yet.
Code:
-(void)setFoo:(id) aFoo{
if(aFoo!=foo){ //make sure new value is different than old value
[aFoo retain]; // retain new value so it can't be destroyed
[foo release]; // release old value - it may get destroyed
foo=aFoo; // point the instance variable foo at the new value
}
}
Every object has a "retainCount" which tells the runtime how many other objects need this one. When the retainCount reaches zero, the object is destroyed (the system calls dealloc). This count starts at 1 when you alloc an object, it goes up by 1 when you retain, and down by 1 when you release.
The code you gave is for removing an old value and adding a new one. It releases the old value so that it can be destroyed, and retains the new value so that it can not be destroyed yet.
Code:
-(void)setFoo:(id) aFoo{
if(aFoo!=foo){ //make sure new value is different than old value
[aFoo retain]; // retain new value so it can't be destroyed
[foo release]; // release old value - it may get destroyed
foo=aFoo; // point the instance variable foo at the new value
}
}
Thanks a lot.....
Can u help me to declare and assign a NSArray object or some methods related to it....
can we write NSArray *arr[4]={1,2,3,4};
if(arr[1]==1){
code----
}
Can u help me to declare and assign a NSArray object or some methods related to it....
can we write NSArray *arr[4]={1,2,3,4};
if(arr[1]==1){
code----
}
No, brackets are core c arrays and sending messages. And NSArraycan only hold objects, like NSNumber and NSString, not integers. Something like this would work:
Every object has a "retainCount" which tells the runtime how many other objects need this one. When the retainCount reaches zero, the object is destroyed (the system calls dealloc). This count starts at 1 when you alloc an object, it goes up by 1 when you retain, and down by 1 when you release.
The code you gave is for removing an old value and adding a new one. It releases the old value so that it can be destroyed, and retains the new value so that it can not be destroyed yet.
Code:
-(void)setFoo:(id) aFoo{
if(aFoo!=foo){ //make sure new value is different than old value
[aFoo retain]; // retain new value so it can't be destroyed
[foo release]; // release old value - it may get destroyed
foo=aFoo; // point the instance variable foo at the new value
}
}
Thank you Smasher for your quick response ......It helps me a lot to clear my doubt.Now I faced another problem regarding NSInteger and NSNumber .In Java autoboxing is possible,but in objective c when I directly assign an integer to a NSInteger object it gives me an warning.
So can u give me a good example about NSInteger ?
Again I have another doubt ,is iPhone is a different operating system like Windows and Mac...If it is a diff operating system then is it need Mac operating system to run objective c code???
NSInteger is an integer type, and you should be able to assign it with no problems; but you can not put it in NSArrays or NSDictionaries because it is not an object.
Code:
NSInteger myInt = 7;
NSNumber is an object, so you must use a constructor from the NSNumber class. Because it is an object, you can put it in NSArrays and NSDictionaries. There is no autoboxing.
Again I have another doubt ,is iPhone is a different operating system like Windows and Mac...If it is a diff operating system then is it need Mac operating system to run objective c code???
Iphone is a different operating system. It is basically a close cousin of the Mac OS operating system (OS X). You need a Mac to develop iPhone software.*
* Someone else will come and say you can build a fake Mac (hackintosh), and you can, but I think it is too much trouble.
NSInteger is an integer type, and you should be able to assign it with no problems; but you can not put it in NSArrays or NSDictionaries because it is not an object.
Code:
NSInteger myInt = 7;
NSNumber is an object, so you must use a constructor from the NSNumber class. Because it is an object, you can put it in NSArrays and NSDictionaries. There is no autoboxing.
Iphone is a different operating system. It is basically a close cousin of the Mac OS operating system (OS X). You need a Mac to develop iPhone software.*
* Someone else will come and say you can build a fake Mac (hackintosh), and you can, but I think it is too much trouble.
Thanks a lot Smasher ,I am very much happy that you have replied my these basic questions .Actually I am a beginner to objective c,so I am reading the books from the Apple online try to implement in coding and whenever I faced any problem I just post them to forum.So your reply in a very systematic way helps me a lot.Once again thanks.