automatic-ref-counting

Did the Target-Action design pattern became bad practice under ARC?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 01:39:50
问题 For years I've been following a great pattern called Target-Action which goes like this: An object calls a specified selector on a specified target object when the time comes to call. This is very useful in lots of different cases where you need a simple callback to an arbitrary method. Here's an example: - (void)itemLoaded { [specifiedReceiver performSelector:specifiedSelector]; } Under ARC it now turns out that doing something like this all of a sudden became dangerous. Xcode throws a

How to get notified when a zeroing weak reference becomes nil on Objective-C under ARC?

我的未来我决定 提交于 2019-12-03 01:26:25
Is there a mechanism which would allow an object to know that a zeroing weak reference turned nil? For example I have a property @property (nonatomic, weak) MyClass *theObject; when theObject deallocates and the property turns nil I want to get notified. But how? Does the zeroing weak reference system use the setter to set the property to nil when the object goes away? The runtime just sets the weak ivar _theObect to nil, a custom setter is not called. What you could do (if you really need the notification): define a local "watcher" class and implement dealloc in that class, create a watcher

How to know if my Xcode iPhone project is using ARC?

孤街醉人 提交于 2019-12-03 01:04:10
I want to know if my Xcode iPhone project is using ARC, and I can't remember if I ticked that box when creating the project. How can I get this information? Select your project, then Build Settings . Look for Objective-C Automatic Reference Counting in the Apple LLVM Compiler - Language section. Make sure you select the target; while you can set this in the project, the target can override it. (You can also use the search bar in Build Settings for OBJC_ARC .) Keep in mind, too, that you can turn ARC on or off on a per file basis in build phases. Or, just try something like this in code: [[

ObjectiveC and JavaScriptCore: Will using this method of calling CallBacks cause memory issues?

微笑、不失礼 提交于 2019-12-03 00:33:15
DISCLAIMER: This is a long post, but could prove very valuable for those grappling with using the new ObjectiveC JavascriptCore framework and doing asynchronous coding between ObjC and JS. Hi there, I'm super new to Objective C and am integrating a javascript communication library into my iOS app. Anyway, I've been trying my hand at using the new ObjectiveC JavaScriptCore Framework introduced in iOS7. It's pretty awesome for the most part, though quite poorly documented so far. It's really strange mixing language conventions, but also kind of liberating in some ways. I should add that I am of

Releasing unused pages in UIPageViewController

巧了我就是萌 提交于 2019-12-03 00:23:01
I am using a separate .h , .m , and .xib files for each UIViewController based page of a UIPageViewController based picture book. Each page is loaded with animations, music, etc. and takes about 4MB of memory. In Instruments, the free memory drops down about 4MB as each page is loaded. This memory is never released as the pages are turned. It eventually gives memory warnings. UIPageViewController seems to keep each page it instantiates in memory and won't unload it. So when pages are turned fast, the app crashes. I would like to be able to unload all pages except the 3 needed by

Objective C - ARC - When to use @autoreleasepool

元气小坏坏 提交于 2019-12-02 23:39:43
I was reading a little about ARC and I saw this: @interface Address : NSObject { @public NSString *city; } @end @implementation Address - (Address*) init: (NSString*) c { city = c; return self; } - (void) dealloc { NSLog(@"Destroying address: %@", city); } @end @interface Customer : NSObject { NSString *name; Address *addr; } @end @implementation Customer - (Customer*) init: (NSString*) n withAddress: (Address*) a { //Note 1: Automatic retain on assignment name = n; addr = a; return self; } - (void) dealloc { NSLog(@"Destroying: %@", name); //Note 2: Automatic release of member variables }

Pointer casting with ARC

醉酒当歌 提交于 2019-12-02 23:35:47
ARC is giving me a hard time with following cast: NSDictionary *attributes; SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes); Error: Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef ' (aka 'const void * ') is disallowed with ARC utahwithak The problem is that attributes shouldn't be a dictionary, it should be a SecKeyRef or CFDataRef. And then cast that back into NSData for the password data copied into it. Like so: CFDataRef attributes; SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes);

NSNotificationCenter removeObserver: in dealloc and thread-safety

纵然是瞬间 提交于 2019-12-02 22:59:45
I'm using ARC and I'm calling [[NSNotificationCenter defaultCenter] removeObserver:someObserver]; in observer's dealloc . From NSNotificationCenter Class Reference Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated. NSNotificationCenter does not retain the observer. Q1: Is NSNotificationCenter thread-safe? In case, the observer is being deallocated(and removing observer from the notification center) and another thread post a notification at the same time. I encounter random crash

How to enable ARC for a file in Non-ARC project?

丶灬走出姿态 提交于 2019-12-02 21:08:42
I know you can use -fno-objc-arc flag to disable ARC for files that NOT support ARC in an ARC project. Is there any way to enable ARC for files support ARC in a Non-ARC project? Thanks! Yup: add the -fobjc-arc flag the same way. 来源: https://stackoverflow.com/questions/11255177/how-to-enable-arc-for-a-file-in-non-arc-project

UIView Animation on multiple UIImageViews in ARC

不羁岁月 提交于 2019-12-02 20:52:31
问题 I have an animation in my app that grows a UIImageView and then shrinks it (really two animations). Throughout the app this may happen on several different UIImageViews. I found a way to do this that worked really well, but it now doesn't seem to be compatible with Automatic Reference Counting. Here is my code: [UIView beginAnimations:@"growImage" context:imageName]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; [UIView setAnimationDelegate:self];