automatic-ref-counting

How to properly implement ARC compatible and `alloc init` safe Singleton class? [duplicate]

纵饮孤独 提交于 2019-11-30 02:26:15
This question already has an answer here: How do I implement an Objective-C singleton that is compatible with ARC? 10 answers I saw thread safe version +(MyClass *)singleton { static dispatch_once_t pred; static MyClass *shared = nil; dispatch_once(&pred, ^{ shared = [[MyClass alloc] init]; }); return shared; } but what would happen if someone just calls [MyClass alloc] init] ? How to make it return same instance as the +(MyClass *)singleton method? Apple recommends the strict singleton implementation (no other living object of the same type is allowed) this way: + (instancetype)singleton {

The role of -supportsWeakPointers

点点圈 提交于 2019-11-30 02:04:18
Apple have recently published Transitioning to ARC Release Notes , a document that explains ARC and tackles some of the issues of converting non-ARC code to ARC. In these notes, they mention the following: If you you [sic] find that you must implement custom retain or release methods, then you must also implement the following method in your class: -(BOOL)supportsWeakPointers { return NO; } This method will prevent weak pointers from being formed to your objects. You are strongly encouraged to find a solution that doesn’t require implementing your own retain and release methods instead of

Why does the ARC migrator say that NSInvocation's -setArgument: is not safe unless the argument is __unsafe_unretained?

只愿长相守 提交于 2019-11-30 01:22:17
I was migrating a block of code to automatic reference counting (ARC), and had the ARC migrator throw the error NSInvocation's setArgument is not safe to be used with an object with ownership other than __unsafe_unretained on code where I had allocated an object using something like NSDecimalNumber *testNumber1 = [[NSDecimalNumber alloc] initWithString:@"1.0"]; then set it as an NSInvocation argument using [theInvocation setArgument:&testNumber1 atIndex:2]; Why is it preventing you from doing this? It seems just as bad to use __unsafe_unretained objects as arguments. For example, the following

ARC and ASIHTTPRequest

我怕爱的太早我们不能终老 提交于 2019-11-30 00:38:27
I have a strange problem. I use ASIHTTPRequest in a iOS 5 project with ARC enabled. Since ASIHTTPRequest does not support ARC I have disabled ARC on all individual ASIHTTPRequest files. However, when I'm trying to compile my project, xcode still believes that those files are ARC-enabled and it complains. Did I do anything wrong or is that a bug in xcode? Don't tell me to convert ASIHTTPRequest to ARC-compatible code with the refactor tool. I have tried to do that and xcode complains on that ARC is enabled on the project (?!?!). You typed -fno-ojbc-arc . The correct flag is -fno-objc-arc . I

ARC and autorelease

喜欢而已 提交于 2019-11-30 00:34:23
autorelease is used for returned function object so the caller don't take ownership and callee will release the object in the future. However, ARC is capable to count ownership of caller and release it after use, that is, it can behavior just like Smart Pointer in C++. With ARC, it can get rid of autorelease because autorelease is non-deterministic. The reason I ask for this question is that I do see the returned object calls dealloc earlier in ARC than non-ARC code. This leads me to think that ARC can behvior like Smart Pointer and can make autorelease useless. Is it true or possible? The

ARC and CFRelease?

心不动则不痛 提交于 2019-11-29 23:43:56
I'm slightly confused. Everywhere I've read, suggest that when using ARC, you still need to release core foundation objects which makes sense, ARC doesn't manage them. However, I've got a method which uses some CF methods/objects which I used CFRelease on, but that then caused the app to crash. Uncommenting my CFRelease s fixes the issue but then I'm assuming I've got a memory leak? Could someone please explain which things need releasing and which don't, or anything else that's wrong with this code? + (NSString *) fileExtensionForMimeType:(NSString *)type { CFStringRef mimeType = (__bridge

Understanding task_basic_info task resident_size

微笑、不失礼 提交于 2019-11-29 23:41:54
问题 short question: someone (cit. 5) told me that resident memory could be reclaimed by my system. What does this mean? Does it mean that my App is not using that memory or is the resident memory value directly related to the memory being currently used by my App? I haven't found much documentation on this apart from those answers. I am trying to solve an issue. I am writing a game usign iOS 6.0 and Cocos2d 2.0 and I do get some memory problems. I have Cococs2d 2.0 as static library and I wrote

What is the difference in Swift between 'unowned(safe)' and 'unowned(unsafe)'?

筅森魡賤 提交于 2019-11-29 23:00:36
Apple's Swift Programming Language Guide mentions the capture specifiers unowned(safe) and unowned(unsafe) , in addition to weak and unowned . I (think I) understand the differences between weak and unowned ; but what is the difference between unowned(safe) and unowned(unsafe) ? The guide doesn't say. Please: Don't rely on simply stating an Objective-C equivalent. From what I understand, although I can't find a definitive source from Apple, unowned can be broken into two flavors, safe and unsafe . A bare unowned is unowned(safe) : it is a specially wrapped reference which will throw an

how to use delegates with Automatic Reference Counting

你离开我真会死。 提交于 2019-11-29 22:49:29
I've jumped on the ARC bandwagon. In the past I would have my delegate properties declared like this: @property(assign) id<MyProtocol> delegate; So I thought I would do this under ARC: @property(weak) id<MyProtocol> delegate; Not so. On the @synthesize statement in the .m I have a compile error : *Semantic Issue: Existing ivar 'delegate' for __weak property 'delegate' must be __weak* I HAVE declared it as weak though! Also how do I pass a class implementing a protocol to a weakly referenced property. Do I have to wrap it in one of those weird obj_unretained calls? Any help on this would be

recursive block and retain cycles in ARC

只谈情不闲聊 提交于 2019-11-29 21:01:52
EDIT2: No. The suggested answer is about async calls. I want & need synchronous calls, like in a normal, standard recursive call. EDIT: while __unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ; compiles without warning or errors, it fails at runtime with a NULL stored into unsafe_apply. However this: - (void) applyToView: (UIView *) view { UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) { return [UIColor colorWithHue: ((CGFloat) index / 255.0f) saturation: 0.5f brightness: 0.5f alpha: 1.0f] ; } ; void (^applyColors) (UIView *, NSInteger index) = ^(UIView * view,