Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-21-2009, 08:34 AM   #1 (permalink)
ranjeet_bhatta
 
Join Date: Sep 2009
Posts: 16
afixi.ranjeet is on a distinguished road
Default retain and release method in Objective C

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 ?

-(void)setFooid) aFoo{
if(aFoo!=foo){
[aFoo retain];
[foo release];
foo=aFoo;
}
}
afixi.ranjeet is offline   Reply With Quote
Old 09-21-2009, 10:32 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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.

There's a link in my signature that will explain more:
http://www.stepwise.com/Articles/Tec...-03-11.01.html

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
    }
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-22-2009, 02:31 AM   #3 (permalink)
ranjeet_bhatta
 
Join Date: Sep 2009
Posts: 16
afixi.ranjeet is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
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.

There's a link in my signature that will explain more:
Very simple rules for memory management in Cocoa

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----
}
afixi.ranjeet is offline   Reply With Quote
Old 09-22-2009, 11:14 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by afixi.ranjeet View Post
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:

Code:
NSArray *arr = [[NSArray alloc] initWithObjects: @"1",@"2",@"3",@"4",nil];

NSString secondString = [arr objectAtIndex:1];
if ( [secondString isEqualToString: @"2"]){
    NSLog(@"Second string is: %@ ",  secondString);
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-24-2009, 11:19 PM   #5 (permalink)
ranjeet_bhatta
 
Join Date: Sep 2009
Posts: 16
afixi.ranjeet is on a distinguished road
Red face retain and release method in objective c

Quote:
Originally Posted by smasher View Post
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.

There's a link in my signature that will explain more:
Very simple rules for memory management in Cocoa

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???
afixi.ranjeet is offline   Reply With Quote
Old 09-24-2009, 11:44 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by afixi.ranjeet View Post
So can u give me a good example about NSInteger ?
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.
Code:
NSNumber *myNumber = [NSNumber numberWithInt: 14];
Quote:
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.
__________________

Free Games!

Last edited by smasher; 09-24-2009 at 11:47 PM.
smasher is offline   Reply With Quote
Old 09-25-2009, 12:44 AM   #7 (permalink)
ranjeet_bhatta
 
Join Date: Sep 2009
Posts: 16
afixi.ranjeet is on a distinguished road
Smile

Quote:
Originally Posted by smasher View Post
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.
Code:
NSNumber *myNumber = [NSNumber numberWithInt: 14];
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.
afixi.ranjeet is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 460
13 members and 447 guests
Domele, Duncan C, Feldspar, MacBook MH, Objective Zero, patapple, peterwilli, pipposanta, PixelInteractive, Punkjumper, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:03 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0