0

The output should be

strString = değiştim
wkString  = NULL   

but it is not. WHY?

#import <Foundation/Foundation.h>

@interface learnARC : NSObject {
    NSString *strString, __weak *wkString;
}

@property (strong) NSString *strString;
@property (weak) NSString *wkString;

-(void) yaz;

@end

#import "learnARC.h"

@implementation learnARC

@synthesize wkString, strString;

-(void) yaz {
    NSString *anaString = @"anaString";
    strString = anaString;
    wkString = anaString;
    NSLog(@"\nstrString = %@\nwkString  = %@",strString,wkString);

    anaString = @"değiştim";
    NSLog(@"\nstrString = %@\nwkString  = %@",strString,wkString);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        learnARC *lrnarc = [[learnARC alloc]init];
        [lrnarc yaz];
    }
    return 0;
}
Kara
  • 6,115
  • 16
  • 50
  • 57
agulerer
  • 5
  • 2

1 Answers1

2

WHY?

Because you're captalizing your question instead of your class names...

Seriously, the weak reference should not be NULL. You have assigned a pointer to it (a pointer to the string @"anaString"). And since string literals have static storage duration, they are never deallocated during the lifetime of the program. (I think you may be confusing variables with properties?)

  • so what is difference between strong and weak for NSString? – agulerer Apr 12 '13 at 14:34
  • @agulerer Not "for NSString". For every object. The one increases the reference count of the object assigned, the other doesn't. –  Apr 12 '13 at 15:15
  • 1
    (Which is wholly futile seeing as immutable constant NSStrings have a retain count of close to `NSUIntegerMax`) – CodaFi Apr 12 '13 at 16:01