I think I may made a silly mistake, but I can't figure out why:
I have a method and Block to handle some network API like:
-(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;
}];
}
before the block, I can see newNumber
has value correctly,
However when the block is invoked, the newNumber is nil, instead of the text. But I was able to print weakSelf.nextPhoneTextField.text
, which is not changed.
Any explainations is appreciated!
UPDATE:
After creating a sample project, I found it's not reproducible. the weak string pointer has valid text. Then I start debugging it, and I found that,
In order to avoid the new
keyword, I changed the pointer name to anotherNumber
In my real project, when calling __weak NSString *anotherNumber = self.nextPhoneTextField.text;
the anotherNumber
has a new address, rather than the self.nextPhoneTextField.text;
address:
(lldb) p anotherNumber
(__NSCFString *) $2 = 0x00007f88b3ff2960 @"12345678901"
(lldb) p self.nextPhoneTextField.text
(__NSCFString *) $3 = 0x00007f88b15f8690 @"12345678901"
However in the sample project, I have the similar function,
- (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);
}];
}
it is the same address:
(lldb) p text
(NSTaggedPointerString *) $2 = 0xa000000747365744 @"test"
(lldb) p self.textField.text
(NSTaggedPointerString *) $3 = 0xa000000747365744 @"test"
and the class type changed also... Looking for answers!!!
Another update:
I delete the block, simply create two weak pointers with some strings like "hello" and "12345678901", the formmer one has the same address and marked as NSTaggedPointerString
, however the latter one has different address and marked as NSCFString
It seems to me that once the text reach a specific length, it will have the NSCFString and different address, and after some tests, the bounty is 9. once more than 9 words, it will be NSCFString
, tested on iOS 9.1 iPhone 6S simulator.
on iOS 8.4 simulator, all the strings with different length result in different mem adress and NSCFString
sample project:https://github.com/liuxuan30/WeakStringPointer