__weak NSString * text = self.textField.text has inconsistent behavior

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) {

      // update phone number
      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

+4
1

__weak NSString *anotherNumber = self.nextPhoneTextField.text;

, NSString NOT , NSString NSString , , , , ,

, textFields, anotherNumber.

NSString *test = self.nextPhoneTextField.text;
self.nextPhoneTextField.text = @"Something else";
NSSLog(@"Test object contains %@ , the textField contains %@ ",test,self.nextPhoneTextField.text);

:

  • NSString self.nextPhoneTextField.text
  • NSString - anotherNumber
  • anotherNumber __weak, (NSString), __weak , .

, anotherNumber

__weak NSString *anotherNumber = self.nextPhoneTextField.text;
NSString *strongAnotherNumber = self.nextPhoneTextField.text;
NSLog(@"Weak number - %@ , strong - %@",anotherNumber,strongAnotherNumber);
dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"Block Weak number - %@ , strong - %@",anotherNumber,strongAnotherNumber);
});

, NSString iOS?

0

Source: https://habr.com/ru/post/1614394/


All Articles