弱指针与强指针在 xcode 5 中的行为略有不同 [英] weak vs strong pointers behaving slightly differently in xcode 5
问题描述
我有一些在 xcode 4.6 中运行良好的代码.自从我升级到 xcode 5 后,一节就无法工作了.这不是错误.非常奇怪的是,没有修改的完全相同的代码在模拟器中工作,但在设备上不起作用.如果我在 xcode 4.6 中编译相同的 coe,它可以在设备和模拟器上运行.
i have some code that was working happily in xcode 4.6. since i upgraded to xcode 5 one section was not working. it's not erroring. very oddly the exact same code with no modifications DOES work in the simulators, but does NOT work on devices. if i compile the same coe in xcode 4.6 it DOES work on devices as well as simulators.
它使用数百万的可达性类.
it uses tony millions reachability class.
我已经找到了这个问题,但是因为它可以在 xcode 5 的模拟器上运行,我不明白.
i have tracked down the issue but becasue itworks on simulators in xcode 5, i dont understand.
基本上在一个按钮点击我检查可达性.
basically on a button click i check the reachability.
我有一个指向可达性对象的弱指针,我在按钮点击时设置了它 - 下面的代码片段
i have a weak pointer to a reachability object, which i setup on the button click - snippet below
@interface settingsViewController ()
@property (weak,nonatomic) Reachability *reachable;
@end
....
//called on click
/ Checks if we have an internet connection or not
- (void)testInternetConnection
{
self.reachable= [Reachability reachabilityWithHostname:@"www.google.com"];
if (self.reachable)
{
NSLog(@"reachability created");
}
else
{
NSLog(@"NO OBJECT");
}
//do more stuff.....
}
基本上不改变模拟器中的任何东西,调试控制台打印创建的可达性",但在任何设备(iPhone/ipad,IOS6/IOS7)上,调试控制台打印NO OBJECT"
basically without changing ANYTHING in the simulators the debug console prints "reachability created" but on ANY device (iPhone/ipad, IOS6/IOS7) the debug console prints "NO OBJECT"
使用reachabilityWithHostname 创建对象的tony milions 代码如下
tony milions code to create the object using reachabilityWithHostname is below
#pragma mark - class constructor methods
+(Reachability*)reachabilityWithHostname:(NSString*)hostname
{
SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostname UTF8String]);
NSLog(@"init1");
if (ref)
{
NSLog(@"init2");
id reachability = [[self alloc] initWithReachabilityRef:ref];
#if __has_feature(objc_arc)
NSLog(@"init with arc");
return reachability;
#else
NSLog(@"init no arc");
return [reachability autorelease];
#endif
}
NSLog(@"cannot init");
return nil;
}
并且在模拟器和设备上,调试控制台按预期打印init with arc".
and on both the simulator and device the debug console prints "init with arc" as expected.
所以基本上它创建的对象OK,但是一旦我在模拟器上测试它它是有效的,但在设备上它是空的.那一点我不明白.
so basically it creates the object OK, but as soon as i test it on the simulator it is valid, but on the device it is null. that bit i dont understand.
但是,如果我将可达性对象更改为强指针,模拟器会继续工作并且设备也能正常工作 - 因为当我测试可达性对象时,它会被设置,并且调试控制台会打印已创建可达性"
BUT if i change the reachability object to be a strong pointer, the simulator continues to work AND the device works as well - in that when i test the reachability object, it is set and the debug console prints "reachability created"
我不明白该对象如何在仅在具有弱指针的设备上创建后立即为空...当然a) 模拟器和设备的行为应该相同b) 我刚刚创建了该行之前的对象 - 如果我使用弱指针,它是如何在设备上变为空的?/
i dont understand how the object can be null as soon as it's created ONLY on the device with a weak pointer... surely a) the simulator and device should behave the same b) i have only just created the object the line before - how has it become null on the device if i'm using weak pointer?/
提前致谢!
推荐答案
您发布的代码不应该起作用.
The code you posted should never work.
它确实可以在模拟器上运行的事实是模拟器不完美本质的产物,而不是它应该可以运行的反映.
The fact that it does work on simulators is an artifact of the imperfect nature of the simulator, not a reflection that it SHOULD work.
编译器应该给你一个警告,创建一个对象并将它保存到一个弱指针,它会被立即释放.这就是 ARC 的工作原理.
The compiler should be giving you a warning that creating an object and saving it to a weak pointer, it will be released immediately. That's how ARC works.
使用局部强变量.一旦强变量超出范围,调用者就可以决定是否应该保留对对象的强引用.
Use a local strong variable. As soon as the strong variable goes out of scope, it will be up to the caller to decide if it should retain a strong reference to the object.
这篇关于弱指针与强指针在 xcode 5 中的行为略有不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!