Amazon.com Widgets retainCount start from 2 !!!
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 04:25:08 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 17
| | | |-+  retainCount start from 2 !!!
Pages: [1]   Go Down
Print
Author Topic: retainCount start from 2 !!!  (Read 1770 times)
jockerfox
Newbie
*
Posts: 2


Email




« on: March 18, 2009, 11:27:29 AM »

Hi to all!

On 17.1 you wrote numberWithInt:100  Grin hehe..
Why if I write numberWithInt:10, the retainCount is 2 ?  Shocked START from 2?!?

Test 1:
Code: (Objective-C)
#import <Foundation/Foundation.h>
 
int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
    NSNumber *myObject=[NSNumber numberWithInt:10];
    NSLog(@"retainCount di myObject:%i", [myObject retainCount]);
    [pool drain];
    return 0;
}
OUTPUT
2009-03-18 19:20:44.213 esempioStringXForum[32017:10b] retainCount di myObject:2


But now.. triple  Roll Eyes
Test 2:

Code: (Objective-C)
#import <Foundation/Foundation.h>
 
#define NUMERO 0xF
int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
    int i;
   
    for (i=NUMERO; i>0; i--)
    {
        NSNumber *myObject=[NSNumber numberWithInt:i];
        if ([myObject retainCount]!=1)
            printf("%i - retainCount di myObject:%i\n", i, [myObject retainCount]);
    }
    printf("i==0 ? >>> %i\n\n",i);
 
    for (i=-NUMERO; i<0; i++)
    {
        NSNumber *myObject=[NSNumber numberWithInt:i];
        if ([myObject retainCount]!=1)
            printf("%i - retainCount di myObject:%i\n", i, [myObject retainCount]);
    }
    printf("i==0 ? >>> %i\n\n",i);
 
//*****************************************************************************
//*****************************************************************************
    printf("***************************************\n");
    NSNumber *myObject2=[NSNumber numberWithInt:10];
    printf("retainCount di myObject2:%i\n", [myObject2 retainCount]);
 
    [pool drain];
    return 0;
}
OUTPUT
12 - retainCount di myObject:2
11 - retainCount di myObject:2
10 - retainCount di myObject:2
9 - retainCount di myObject:2
8 - retainCount di myObject:2
7 - retainCount di myObject:2
6 - retainCount di myObject:2
5 - retainCount di myObject:2
4 - retainCount di myObject:2
3 - retainCount di myObject:2
2 - retainCount di myObject:2
1 - retainCount di myObject:2
i==0 ? >>> 0

-1 - retainCount di myObject:2
i==0 ? >>> 0

***************************************
retainCount di myObject2:3


I'm confused...??  Huh
Regards.. and thank you!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: March 18, 2009, 11:30:06 AM »

I don't blame you for being confused!   Wink

Check here for a detailed discussion on this:

http://classroomm.com/objective-c/index.php?topic=188.0

Cheers,

Steve Kochan
Logged
jockerfox
Newbie
*
Posts: 2


Email




« Reply #2 on: March 18, 2009, 12:40:54 PM »

Thanks for the quick response!
And thanks for your reply.

I linked this topic on XCode-Italia, ok?  Wink
LINK:http://www.xcodeitalia.com/forum/viewtopic.php?t=1967

to the next..  Tongue
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: March 18, 2009, 12:49:38 PM »

Sure it's ok!   Grin

Cheers,

Steve
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.