I think I can make a stupid mistake, but I can not understand why:
I have a method and block to handle some network APIs, for example:
-(IBAction)confirm:(id)sender {
__weak typeof(self) weakSelf = self;
__weak NSString *anotherNumber = self.nextPhoneTextField.text;
[SharedInstance bindNewPhoneNumber:self.nextPhoneTextField.text pinCode:self.verifyCodeTextField.text sucess:^(id result) {
SharedInstance.phoneNumber = anotherNumber;
}];
}
in front of the block, I see that newNumberit matters correctly,
However, when the block is called, the new number is nil instead of text. But I managed to print weakSelf.nextPhoneTextField.text, which has not changed.
Any explanation is appreciated!
UPDATE:
After creating the sample project, I found it irreproducible. a weak line pointer has valid text. Then I start debugging it, and I found that
To avoid the keyword new, I changed the name of the pointer toanotherNumber
In my real project, when called, it __weak NSString *anotherNumber = self.nextPhoneTextField.text; anotherNumberhas a new address, not an address self.nextPhoneTextField.text;:
(lldb) p anotherNumber
(__NSCFString *) $2 = 0x00007f88b3ff2960 @"12345678901"
(lldb) p self.nextPhoneTextField.text
(__NSCFString *) $3 = 0x00007f88b15f8690 @"12345678901"
,
- (void)clickBlock:(void (^)(NSString * string))block {
if (block) {
block(@"haha");
}
}
- (IBAction)clicked:(id)sender {
__weak typeof(self) weakSelf = self;
__weak NSString *text = self.textField.text;
[self clickBlock:^(NSString *string) {
NSLog(text);
NSLog(string);
}];
}
:
(lldb) p text
(NSTaggedPointerString *) $2 = 0xa000000747365744 @"test"
(lldb) p self.textField.text
(NSTaggedPointerString *) $3 = 0xa000000747365744 @"test"
... !!!
:
, "" "12345678901", NSTaggedPointerString, NSCFString
, , NSCFString , 9. 9 , NSCFString, iOS 9.1 iPhone 6S.
iOS 8.4, mem- NSCFString
: https://github.com/liuxuan30/WeakStringPointer