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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2011, 04:35 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 498
nick.keroulis is on a distinguished road
Default Do i have to initialize variables before i return them?

For example which one is correct?

1.
Code:
-(NSString *)something
{
    NSString *something = [[NSString alloc] init];
    
    something = [NSString stringWithFormat:@"%@ 123", aPublicVariable];

    return something;
}
2.
Code:
-(NSString *)something
{
    return [NSString stringWithFormat:@"%@ 123", aPublicVariable];
}
1 or 2 ??? I REALLY care about memory. Even the last byte...
__________________
iDamaged Hands-On Preview. I like this game check it out.
nick.keroulis is offline   Reply With Quote
Old 11-06-2011, 05:17 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 46
kasparp is on a distinguished road
Default

Quote:
Originally Posted by nick.keroulis View Post
For example which one is correct?

1.
Code:
-(NSString *)something
{
    NSString *something = [[NSString alloc] init];
    
    something = [NSString stringWithFormat:@"%@ 123", aPublicVariable];

    return something;
}
2.
Code:
-(NSString *)something
{
    return [NSString stringWithFormat:@"%@ 123", aPublicVariable];
}
1 or 2 ??? I REALLY care about memory. Even the last byte...
The second one is correct.
stringWithFormat will return a new string, which is set to autorelease.
So in the first example, you first allocate a string and then you assign your string pointer to a new object, meaning the string you just allocated on the first line will never be released.
Hope it makes sense.
__________________
Cheers
kasparp is offline   Reply With Quote
Old 11-06-2011, 05:45 AM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Besides the problem with the first code that kasparp pointed out, it also doesn't follow the naming convention in regards to returning owned or autoreleased objects.
baja_yu is offline   Reply With Quote
Old 11-06-2011, 07:28 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 498
nick.keroulis is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Besides the problem with the first code that kasparp pointed out, it also doesn't follow the naming convention in regards to returning owned or autoreleased objects.
Thanks, really helpful guys.

One more thing.

Is this:

Code:
return @"value";
considered to be the same as this:

Code:
return [NSString stringWithString:@"value"];
???

Also,

When am i using a value like something that is helping me to figure out what i should return:

Code:
-(NSString *)somethingElse
{
    NSString *something = [self doSomething];
    
    if ([something isEqualToString:@"111"])
    {
         something = nil;
         return [NSString stringWithString:@"123"];
    }
    else
    {
         something = nil;
         return [NSString stringWithString:@"12345"];
    }
}
do i have to set something to nil before i return anything or not?
__________________
iDamaged Hands-On Preview. I like this game check it out.

Last edited by nick.keroulis; 11-06-2011 at 07:36 AM.
nick.keroulis is offline   Reply With Quote
Old 11-06-2011, 07:45 AM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Depends on what "doSomething" returns, an owned or an autoreleased object. By naming convention it should be autoreleased in that case. You should really read the Memory Management Programming Guide, this is all explained there.

Oh, and for the first question, @"" are actually string constants, just treat them as if they were autoreleased objects, balance retains with release calls and you'll be fine.

Last edited by baja_yu; 11-06-2011 at 07:51 AM.
baja_yu is offline   Reply With Quote
Old 11-06-2011, 08:01 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 498
nick.keroulis is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Depends on what "doSomething" returns, an owned or an autoreleased object. By naming convention it should be autoreleased in that case. You should really read the Memory Management Programming Guide, this is all explained there.

Oh, and for the first question, @"" are actually string constants, just treat them as if they were autoreleased objects, balance retains with release calls and you'll be fine.
doSomething returns something like this:
Code:
[NSString stringWithString:@"123443234"];
doSomething is my own function.

Should i return this:
Code:
[NSString stringWithString:@"123443234"];
or this:
Code:
[[NSString stringWithString:@"123443234"] autorelease];
__________________
iDamaged Hands-On Preview. I like this game check it out.
nick.keroulis is offline   Reply With Quote
Old 11-06-2011, 08:21 AM   #7 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

If you've read the Mem Management guide you would know. Is reading a short but essential guide that hard for you? You keep stumbling in the dark and coming back with these basic questions for months now, and you could have avoided it all with a days read.
baja_yu is offline   Reply With Quote
Old 11-06-2011, 08:39 AM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 56
Nighty is on a distinguished road
Default

Quote:
Originally Posted by nick.keroulis View Post
doSomething returns something like this:
Code:
[NSString stringWithString:@"123443234"];
doSomething is my own function.

Should i return this:
Code:
[NSString stringWithString:@"123443234"];
or this:
Code:
[[NSString stringWithString:@"123443234"] autorelease];
first one is right. second one is wrong.
the static function "stringWithString..." returns an autorelease object. so no need to redeclare autorelease for it.
the only reason to use autorelease is when using alloc like so:
Code:
[[[NSString alloc] initWithString:[self doSomething]] autorelease];
Nighty 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: 401
17 members and 384 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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