強く弱いのはプロパティ宣言で使われ、__strongと__weakはインスタンス変数の宣言で使われます... strongはオブジェクトをメモリに長くそれを所有していると弱いということは、他の誰かがそれを強く参照している限り、オブジェクトを記憶に残すと言う...そうですか?しかし、私はなぜプロパティのための弱いとインスタンス変数のための__weakが異なって動作するのか分からないのですか?ここで私は...なぜweak for propertyと__weakのインスタンス変数が異なって振る舞うのですか
@interface DemoViewController(){
__weak NSArray *weakArray;
__strong NSArray *strongArray;
__weak NSString *weakString;
__strong NSString *strongString;
}
@property (weak) NSString *weakStringProperty;
@property (strong) NSString *strongStringProperty;
@property (weak) NSArray *weakArrayProperty;
@property (strong) NSArray *strongArrayProperty;
@end
@implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
strongArray = [[NSArray alloc] initWithObjects:@"one",@"two", nil];
weakArray = strongArray;
NSLog(@"Round:1 strongArray is %@.", strongArray);
NSLog(@"Round:1 weakArray is %@.", weakArray);
strongArray = nil;
NSLog(@"Round:2 strongArray is %@.", strongArray);
NSLog(@"Round:2 weakArray is %@.", weakArray);
self.strongArrayProperty = [[NSArray alloc] initWithObjects:@"one",@"two", nil];
self.weakArrayProperty = self.strongArrayProperty;
NSLog(@"Round:1 strongArrayProperty is %@.", self.strongArrayProperty);
NSLog(@"Round:1 weakArrayProperty is %@.", self.weakArrayProperty);
self.strongArrayProperty = nil;
NSLog(@"Round:2 strongArrayProperty is %@.", self.strongArrayProperty);
NSLog(@"Round:2 weakArrayProperty is %@.", self.weakArrayProperty);
strongString = [[NSString alloc]initWithFormat:@"instanceVariable"];
weakString = strongString;
NSLog(@"Round:1 strongString is %@.", strongString);
NSLog(@"Round:1 weakString is %@.", weakString);
strongString = nil;
NSLog(@"Round:2 strongString is %@.", strongString);
NSLog(@"Round:2 weakString is %@.", weakString);
self.strongStringProperty = [[NSString alloc]initWithFormat:@"Property"];
self.weakStringProperty = self.strongStringProperty;
NSLog(@"Round:1 strongStringProperty is %@.", self.strongStringProperty);
NSLog(@"Round:1 weakStringProperty is %@.", self.weakStringProperty);
self.strongStringProperty = nil;
NSLog(@"Round:2 strongStringProperty is %@.", self.strongStringProperty);
NSLog(@"Round:2 weakStringProperty is %@.", self.weakStringProperty);
}
@end
そして、ここでは、彼らは弱いを参照されたオブジェクトの後に結果のログ
Round:1 strongArray is (
one,
two
).
Round:1 weakArray is (
one,
two
).
Round:2 strongArray is (null).
Round:2 weakArray is (null).
Round:1 strongArrayProperty is (
one,
two
).
Round:1 weakArrayProperty is (
one,
two
).
Round:2 strongArrayProperty is (null).
Round:2 weakArrayProperty is (
one,
two
). —???
Round:1 strongString is instanceVariable.
Round:1 weakString is instanceVariable.
Round:2 strongString is (null).
Round:2 weakString is (null).
Round:1 strongStringProperty is Property.
Round:1 weakStringProperty is Property.
Round:2 strongStringProperty is (null).
Round:2 weakStringProperty is Property. ——??
弱いインスタンス変数の印刷(ヌル)ともにnilに設定されていると不思議に思ったものですこれは期待どおりですが、弱いプロパティーweakStringPropertyとweakArrayPropertyの両方が以前の値を表示していて、strongStringPropertyとstrongArrayPropertyを強く指していたように振る舞うのはなぜだろうか?
感謝:)
ラウンド2ではstrongArrayPropertyとweakArrayPropertyを(null)として取得できますが、ラウンド2のweakStringPropertyはまだ「プロパティ」です。 ARCが技術的にこれを処理する必要があるので、これは非常に奇妙です。 PS-私はこれを私のコンピュータで走らせました。 –
私は主なメソッドは、アプリケーションを実行するautoreleaseプールを実行しているので、弱いプロパティが保持されている理由だと思う。自動解放プールのため、弱いプロパティはすぐにはnilに設定されません。しかし、私はviewDidAppearとdidReceiveMemoryWarning(メモリ警告をシミュレートして)にアクセスしようとしましたが、それはまだnilにならなかった。 –
奇妙な!!このコードを実際のデバイスで実行すると、弱いインスタンス変数と弱いプロパティの両方に期待される結果が得られました。つまり、シミュレータだけで問題が発生しています。とにかくThxsシミュレータ上の@Kunal Shrivastava – Dipika