automatic-ref-counting

__strong and __weak keyword placement - Objective-C

点点圈 提交于 2019-11-30 09:44:36
The compiler seems to have no problem with the two following declarations: NSObject * __weak weakThing; __weak NSObject *anotherWeakThing; Is there a difference between the two? Is the behavior like the const keyword ? I ask because Xcode's warning generally suggest ... SomeDataType * __weak / __strong ... when you've goofed something up. So I've tried to follow this pattern, but wondered if there was a difference at all. No, there is no difference. With the const keyword, there are multiple things it could apply to in a declaration; it could apply to the pointer, or it could apply to the

How to activate Cycles reporting in Instruments under ARC?

喜欢而已 提交于 2019-11-30 09:13:12
Instruments can visualize retain cycles under ARC in a graphically interesting way. I also remember that a few days ago I spotted the "Cycles" view in Instruments by accident. Now where I started using ARC, suddenly I'm not able to find that anymore. The Allocations and VM Tracker instruments don't offer it, and the Leaks instrument either. What must I do in order to see retain cycles? Found a screenshot as evidence: Using Xcode 4.2.1, I found the it in "Leaks", under "Cycles & Roots". However, I've found it less than useful when using ARC. It does detect CF leaks and apparently I'm leaking a

iOS 5 Blocks ARC bridged cast

我们两清 提交于 2019-11-30 09:12:06
This Question references this Question: How to simplify callback logic with a Block? My header has these typedefs typedef void (^StuffDoneBlock)(NSDictionary * parsedData); typedef void (^StuffFailedBlock)(NSError * error); And in init stuffDoneCallback = Block_copy(done); StuffFailedCallback = Block_copy(error); In this paper its says that Block_copy is unnecessary. But then it needs a bridged cast. The compiler message is at follows: error: cast of block pointer type 'StuffDoneBlock' (aka 'void (^)(NSDictionary *__strong)') to C pointer type 'const void *' requires a bridged cast [4]

Why is my object's weak delegate property nil in my unit tests?

£可爱£侵袭症+ 提交于 2019-11-30 08:42:56
I have a pretty simple setup for this unit test. I have a class that has a delegate property: @interface MyClass : NSObject ... @property (nonatomic, weak) id<MyDelegateProtocol> connectionDelegate; ... @end and I set the delegate in my test: - (void)testMyMethod_WithDelegate { id delegate = mockDelegateHelper(); // uses OCMock to create a mock object [[delegate expect] someMethod]; myClassIvar.connectionDelegate = delegate; [myClass someOtherMethod]; STAssertNoThrow([delegate verify], @"should have called someMethod on delegate."); } But the delegate is not actually set on line 3 of my unit

How to know when to invalidate an `NSTimer`

橙三吉。 提交于 2019-11-30 07:35:05
问题 Here's my issue: I have a model class that has an NSTimer in it that I want the Timer to run for the entire lifespan of the model object. Initiliazation is easy: I just have the following line of code in the init method: self.maintainConnectionTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(maintainConnection) userInfo:nil repeats:YES]; However, my issue is, how do I invalidate this timer when the model is released from memory? Now, this would usually be easy,

ARC error when compiling

﹥>﹥吖頭↗ 提交于 2019-11-30 07:19:38
I'm trying to compile using the LLVM GCC 4.0 compiler, and I get this error in multiple of my .m files: ARC forbids explicit message send of 'release' I've tried using -fno-objc-arc as a compiler flag but that returns the error: Unrecognized command line option "-fno-objc-arc". How can I solve this? Simply remove all calls to -release . You're not allowed to call -release under ARC because the compiler will insert all the necessary retain / release calls for you. Read more about ARC here . The other way is to use the conversion tool, and Xcode can convert your project to ARC (including

In Objective-C with ARC, is it true that we usually only need to specify nonatomic as property attributes?

自作多情 提交于 2019-11-30 07:16:08
It is strange that in Big Nerd Ranch iOS 5 book (p.73) and Programming iOS 5 book (O'Reilly, p.314) (updadte: even Kochan's Objective-C book Fourth edition) , in the context of ARC, they say the default for properties attribute is assign ... But Apple's documentation says the default is strong . I also tried a simple program where if I don't specify strong , the program works ok, and if I specify strong , it works the same, and when assign is used instead, the compiler shows a warning, so it seems the default is indeed strong . So if most of the time, we want @property (nonatomic, readwrite,

Setting NSError within a block, using ARC

烈酒焚心 提交于 2019-11-30 07:04:06
I wish to set an NSError pointer from within a block in a project using automatic reference counting. What follows is a simplified version of my code: - (BOOL)frobnicateReturningError:(NSError **)error { NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]; __block Frobnicator *blockSelf = self; [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) { [blockSelf doSomethingWithItem:item error:error]; }]; } This compiles but given error may be modified by doSomethingWithItem I tried creating a local NSError for the block to modify, which would then be used

How to enable/ disable “Automatic Reference Counting”?

人盡茶涼 提交于 2019-11-30 07:02:59
问题 Using Xcode 4.2, how can one enable/disable "Automatic Reference Counting"? ANSWERED Under Build Settings, flip "yes" and "no" depending whether you want ARC enabled. 回答1: Globally: Go to "Build Settings", look for "Apple LLVM compiler 3.0 - Language". Set the field "Objective-C Automatic Reference Counting" to "No". For individual files: Go to "Build Phases", select the file, double-click the "Compiler Flags" column and put "-fno-objc-arc" in it. 回答2: To enable ARC per-file, you can also set

What is the difference Memory Leak and a Zombie?

半世苍凉 提交于 2019-11-30 06:54:16
I am working on a ARC based project . I have never worked on Non ARC based project .Recently I came across a zombie in my ARC enabled project.As far as I understood there wont be memory leaks in ARC , as the objects will be deallocated automatically.But I came across a zombie saying "message passed to a deallocated instance".My confusion is is a Memory Leak equivalent to a Zombie. If that is the case then Memory Leak occur in ARC too ? Any help ? "Zombies" in Objective-C parlance are the opposite of leaks. A leak is a bit of allocated memory that you no longer have any references to, so you