automatic-ref-counting

Is cleaning up strong references in deinit a correct pattern?

可紊 提交于 2019-12-13 12:22:57
问题 There are several resources (blog, SO question, plus I've seen it used everywhere) that recommend removing an observer from the NotificationCenter in the deinit of the UIViewController , e.g.: deinit { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } Now while according to another blog entry I don't have to care about removing

How to enforce using `-retainCount` method and `-dealloc` selector under ARC?

人走茶凉 提交于 2019-12-13 12:22:53
问题 Under ARC , compiler will forbid using any method or selector of -retainCount , -retain , -dealloc , -release , and -autorelease . But sometimes I want to know the retain counts at runtime, or using method swizzling to swap the -dealloc method of NSObject to do something. Is it possible to suppress (or bypass) the compiler complaining just a couple of lines of code? I don't want to modify ARC environment for whole project or whole file. I think preprocessor can do it, but how? Additions:

What is the main difference using “copy” and “strong” ownership qualifiers in property declaration with block types?

寵の児 提交于 2019-12-13 12:18:05
问题 Example #1 @property (nonatomic, copy) void (^errorBlock) (NSError *); Example #2 @property (nonatomic, strong) void (^errorBlock) (NSError *); I know that blocks are standard variables on stack, and by making a copy we are "moving" them to the heap. Thats all? Or not? 回答1: There should be no difference. Since the property has a block type, according to http://clang.llvm.org/docs/AutomaticReferenceCounting.html#blocks With the exception of retains done as part of initializing a __strong

Objective C memory management with blocks, ARC and non-ARC

岁酱吖の 提交于 2019-12-13 11:47:26
问题 I have been using blocks for some time now, but I feel that there are things I miss about memory management in both ARC and non-ARC environments. I feel that deeper understanding will make me void many memory leaks. AFNetworking is my main use of Blocks in a particular application. Most of the time, inside a completion handler of an operation, I do something like "[self.myArray addObject]". In both ARC and non-ARC enabled environments, "self" will be retained according to this article from

How do I box Arc and Mutexed variables?

二次信任 提交于 2019-12-13 10:22:25
问题 The following code gets some variables in closures and returns a struct containing that data. I can't return the struct with that data even when I box the struct and clone the variables; they are impossible to take out of this scope. I thought about using a callback closure but I don't really want to do that. Is there any way to take those out without having a callback? pub fn get(addr: &str) -> std::io::Result<Box<Response>> { use std::sync::{Arc, Mutex}; let mut crl = curl::easy::Easy::new(

EXC_BAD_ACCESS with ARC [duplicate]

心不动则不痛 提交于 2019-12-13 09:18:46
问题 This question already has answers here : Assigning an existing CGColor to a CGColor property works in iOS Simulator, not iOS device. Why? (3 answers) Closed 6 years ago . I'm using ARC, I have a CustomTableViewController and I'm adding a CustomView to my background cell, header and footer. For example in the tableView:viewForFooterInSection method i put these lines of code: - (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { CustomFooter *footer = [

ios ARC strong and alloc

℡╲_俬逩灬. 提交于 2019-12-13 09:12:09
问题 I have a question related to ARC. In my interface I have declared a uiwebview as a strong property. Inside my code I have lazy loading like so: - (UIWebView *)aWebView{ if(aWebView == nil){ aWebView = [[UIWebView alloc] initWithFrame:self.bounds]; } return aWebView; } Is that code ok under ARC? 回答1: Yes, it's fine. ARC will include the release call for you. 来源: https://stackoverflow.com/questions/12166323/ios-arc-strong-and-alloc

Sending “NSString *_strong*to parameter of type _unsafe_unretained id* ”change retain/release properties of pointer

廉价感情. 提交于 2019-12-13 07:38:58
问题 Im getting the following error Sending "NSString *_strong*to parameter of type _unsafe_unretained id* "changes retain/release properties of pointer ... in the following line: [theDict getObjects:values andKeys:keys]; Im trying to add an address from contacts to my app. Could someone explain to me what its complaining about? I think its an ARC issue, possibly to do with manual memory management? but im unsure how to fix it. - (BOOL)peoplePickerNavigationController:

How to properly keep a local reference to a child CCNode in a custom class in ARC

心不动则不痛 提交于 2019-12-13 07:35:21
问题 So, I want to hold a local reference to a child CCNode in a CCNode custom class without creating a retain cycle or without having the compiler complaining that the property or instance variable may be unpredictable set to nil For instance with this custom class @interface MyNode : CCNode @property (nonatomic, strong) CCNode *background; + (id)nodeWithBackground:(id)background; @end @implementation MyNode + (id)nodeWithBackground:(id)background { MyNode *node = [MyNode node]; node.background =

when will the retained object (that assigned to weak variable object) be released

血红的双手。 提交于 2019-12-13 06:11:51
问题 these code will get a warning: assigning retained object to weak variable object will be released after assignment __weak NSString *str = [[NSString alloc] initWithFormat:@"1234"]; NSLog(@"url:%@",str); but the nslog will print 1234 normally,seems that the object isn't released after assignment , so when will the release happend? 回答1: You can get the behavior you expect by setting OBJC_DISABLE_TAGGED_POINTERS to YES in the program's environment. For example, you can set it in your scheme in