automatic-ref-counting

Custom class clusters in Swift

寵の児 提交于 2019-11-26 14:45:19
问题 This is a relatively common design pattern: https://stackoverflow.com/a/17015041/743957 It allows you to return a subclass from your init calls. I'm trying to figure out the best method of achieving the same thing using Swift. I do know that it is very likely that there is a better method of achieving the same thing with Swift. However, my class is going to be initialized by an existing Obj-C library which I don't have control over. So it does need to work this way and be callable from Obj-C.

Why do weak NSString properties not get released in iOS?

旧街凉风 提交于 2019-11-26 14:41:34
问题 I wrote the following sample code to see how ARC works @property (nonatomic, weak) NSString *myString; @property (nonatomic, weak) NSObject *myObj; @end @implementation ViewController @synthesize myString = _myString; @synthesize myObj = _myObj; - (void) viewDidAppear:(BOOL)animated { NSLog(@"Appearing Obj: !%@!",self.myObj); NSLog(@"Appearing String: !%@!",self.myString); } - (void)viewDidLoad { self.myObj = [[NSObject alloc] init]; self.myString = [[NSString alloc] init]; NSLog(@"Loading

Handling Pointer-to-Pointer Ownership Issues in ARC

狂风中的少年 提交于 2019-11-26 14:08:03
Suppose Object A has a property: @property (nonatomic, strong) Foo * bar; Synthesized in the implementation as: @synthesize bar = _bar; Object B manipulates a Foo ** , as in this example call from Object A : Foo * temp = self.bar; [objB doSomething:&temp]; self.bar = temp; Can this, or something similar, be done legitimately? What is the correct declaration for the doSomething: method? Furthermore, suppose Object B may be deallocated before I have a chance to set the bar property (and thus take on ownership of the instance pointed to by temp ) - How would I tell ARC to hand off an owning

Can I use Objective-C blocks as properties?

我的梦境 提交于 2019-11-26 13:53:24
Is it possible to have blocks as properties using the standard property syntax? Are there any changes for ARC ? @property (nonatomic, copy) void (^simpleBlock)(void); @property (nonatomic, copy) BOOL (^blockWithParamter)(NSString *input); If you are going to be repeating the same block in several places use a type def typedef void(^MyCompletionBlock)(BOOL success, NSError *error); @property (nonatomic) MyCompletionBlock completion; Richard J. Ross III Here's an example of how you would accomplish such a task: #import <Foundation/Foundation.h> typedef int (^IntBlock)(); @interface myobj :

Referring to weak self inside a nested block

China☆狼群 提交于 2019-11-26 13:00:36
问题 Suppose I already create a weak self using __weak typeof(self) weakSelf = self; [self doABlockOperation:^{ ... }]; Inside that block, if I nest another block: [weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; } will it create a retain cycle? Do I need to create another weak reference to the weakSelf? __weak typeof(self) weakerSelf = weakSelf; [weakSelf doAnotherBlockOperation:^{ [weakerSelf doSomething]; } 回答1: It depends. You only create a retain cycle if you actually store the

Why is object not dealloc&#39;ed when using ARC + NSZombieEnabled

假如想象 提交于 2019-11-26 12:39:37
问题 I converted my app to ARC and noticed that an object alloc\'ed in one of my view controllers was not being dealloc\'ed when that view controller was dealloc\'ed. It took a while to figure out why. I have Enable Zombie Objects on for my project while debugging and this turned out to be the cause. Consider the following app logic: 1) Users invokes action in RootViewController that causes a SecondaryViewController to be created and presented via presentModalViewController:animated . 2)

When converting a project to use ARC what does “switch case is in protected scope” mean?

心不动则不痛 提交于 2019-11-26 12:35:38
问题 When converting a project to use ARC what does \"switch case is in protected scope\" mean? I am converting a project to use ARC, using Xcode 4 Edit -> Refactor -> Convert to Objective-C ARC... One of the errors I get is \"switch case is in protected scope\" on \"some\" of the switches in a switch case. Edit, Here is the code: the ERROR is marked on the \"default\" case: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString

Retain Cycle in ARC

半世苍凉 提交于 2019-11-26 12:28:27
问题 I have never worked on non ARC based project. I just came across a zombie on my ARC based project. I found it was because of retain cycle.I am just wondering what is a retain cycle.Can Could you give me an example for retain cycle? 回答1: A retain cycle is a situation when object A retains object B , and object B retains object A at the same time * . Here is an example: @class Child; @interface Parent : NSObject { Child *child; // Instance variables are implicitly __strong } @end @interface

Disable Automatic Reference Counting for Some Files

[亡魂溺海] 提交于 2019-11-26 11:57:34
I have downloaded the iOS 5 SDK and found that ARC is a great feature of the new Apple compiler. For the time being, many third party frameworks don't support ARC. Could I use ARC for my new code and keep the current retain/release code unchanged? The ARC converter doesn't work here, because some frameworks, such as JSONKit, cannot be converted to ARC by using the converter. Edit: The answer is to add -fno-objc-arc to the compiler flags for the files you don't want ARC. In Xcode 4, you can do this under your target -> Build Phases -> Compile Sources. The public ARC docs , while not directly

Does ARC support dispatch queues?

好久不见. 提交于 2019-11-26 11:33:30
I'm reading apple's documentation about "Memory Management for Dispatch Queues": Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory. I know that ARC is not a garbage collector but I'd like to be sure that I don't need to dispatch_retain and dispatch_release my dispatch_queue_t The short answer: YES, ARC retains and releases dispatch queues. And now for the long answer… If your deployment target is lower than iOS 6.0 or