Can I have a static pointer that is weak in objective-c? I know it compiles, but I want to know if it will behave as I expect a weak pointer to behave.
__weak static HMFSomeClass *weakStaticPointer;
Can I have a static pointer that is weak in objective-c? I know it compiles, but I want to know if it will behave as I expect a weak pointer to behave.
__weak static HMFSomeClass *weakStaticPointer;
Yes, that behaves like a proper weak pointer:
__weak static NSObject *weakStaticPointer;
int main(int argc, char * argv[])
{
@autoreleasepool {
NSObject *obj = [NSObject new];
weakStaticPointer = obj;
NSLog(@"%@", weakStaticPointer);
obj = nil; // object is deallocated -> weak pointer is set to nil
NSLog(@"%@", weakStaticPointer);
}
return 0;
}
Output:
<NSObject: 0x100106a50>
(null)
Also I cannot find any restrictions in the Clang/ARC documentation that forbids a weak pointer to be static.