Archive for febbraio 1st, 2012
01
feb

Suppose that we have a ViewController.h and .m like this:

1
2
3
4
5
// ViewController.h
#import UIKit/UIKit.h

@interface ViewController : UIViewController
@end
1
2
3
4
5
6
7
8
9
10
11
12
// ViewController.m
#import "ViewController.h"
#import "SetValue.h"

@implementation ViewController
- (void)viewDidLoad
{
  [super viewDidLoad];

  [SetValue SetTheValue:@"a string"];
}
@end

And a class called SetValue, with two methods.

1
2
3
4
5
6
7
8
9
// SetValue.h
#import Foundation/Foundation.h

@interface SetValue : NSObject { }

+ (void) SetTheValue:(id)Obj;
- (void) MySelector:(id)Obj;

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// SetValue.m
#import "SetValue.h"

@implementation SetValue

+ (void) SetTheValue:(id)Obj
{
  [self performSelectorOnMainThread:@selector(MySelector:)
                withObject:Obj
                waitUntilDone:NO];
}

- (void) MySelector:(id)Obj
{
  NSLog(@"Data: %@", [Obj description]);
}

@end

What is the value printed by the NSLog?

enjoy.

Ref: albertopasca.it


FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

01
feb

NSString and retainCount memo:

1
2
3
4
5
6
7
8
    NSString *A = [[NSString alloc] init];
    NSLog(@"_A_RC: %i", [A retainCount]);

    NSString *B = [[NSString alloc] initWithFormat:@"AAA"];
    NSLog(@"_B_RC: %i", [B retainCount]);

    NSString *C = [[NSString alloc] initWithString:@"AAA"];
    NSLog(@"_C_RC: %i", [C retainCount]);
1
2
3
...Test[3530:15203] _A_RC: -1  (ios < 5 = 2147483647)
...Test[3530:15203] _B_RC: 1
...Test[3530:15203] _C_RC: -1  (ios < 5 = 2147483647)

Because your string is not in the heap, but is a constant. A NSString made from a constant NSString is identical to the original NSString.

UINT_MAX (2147483647) -> http://it.wikipedia.org/wiki/2147483647

Ref: albertopasca.it


FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , ,

Switch to our mobile site