automatic-ref-counting

Under ARC, is it still advisable to create an @autoreleasepool for loops?

巧了我就是萌 提交于 2019-12-28 13:06:26
问题 Let's say that I have a loop that returns a bunch of autoreleased NSData objects... NSData* bigData = ... while(some condition) { NSData* smallData = [bigData subdataWithRange:...]; //process smallData } Under ARC, should I still wrap an @autoreleasepool around the while condition? NSData* bigData = ... @autoreleasepool { while(some condition) { NSData* smallData = [bigData subdataWithRange:...]; //process smallData } } The reason why I'm asking is I see the living allocation count in

Under ARC, is it still advisable to create an @autoreleasepool for loops?

≯℡__Kan透↙ 提交于 2019-12-28 13:05:42
问题 Let's say that I have a loop that returns a bunch of autoreleased NSData objects... NSData* bigData = ... while(some condition) { NSData* smallData = [bigData subdataWithRange:...]; //process smallData } Under ARC, should I still wrap an @autoreleasepool around the while condition? NSData* bigData = ... @autoreleasepool { while(some condition) { NSData* smallData = [bigData subdataWithRange:...]; //process smallData } } The reason why I'm asking is I see the living allocation count in

Make self weak in methods in Swift

扶醉桌前 提交于 2019-12-28 11:53:50
问题 I have a Swift class that needs to store a table of its own methods. Unfortunately this is causing a reference cycle, because its table retains references to self via the methods it stores. Example leaky code below: typealias Callback = ()->() class CycleInducingClass : NSObject { var myCallbacks = [Callback]() override init() { super.init() myCallbacks.append(myInternalFunction) } func myInternalFunction() { NSLog("lolol: %d", self.myCallbacks.count) } } The only solution I've found so far

Make self weak in methods in Swift

眉间皱痕 提交于 2019-12-28 11:53:11
问题 I have a Swift class that needs to store a table of its own methods. Unfortunately this is causing a reference cycle, because its table retains references to self via the methods it stores. Example leaky code below: typealias Callback = ()->() class CycleInducingClass : NSObject { var myCallbacks = [Callback]() override init() { super.init() myCallbacks.append(myInternalFunction) } func myInternalFunction() { NSLog("lolol: %d", self.myCallbacks.count) } } The only solution I've found so far

Lazy initialisation and retain cycle

淺唱寂寞╮ 提交于 2019-12-28 05:37:31
问题 While using lazy initialisers, is there a chance of having retain cycles? In a blog post and many other places [unowned self] is seen class Person { var name: String lazy var personalizedGreeting: String = { [unowned self] in return "Hello, \(self.name)!" }() init(name: String) { self.name = name } } I tried this class Person { var name: String lazy var personalizedGreeting: String = { //[unowned self] in return "Hello, \(self.name)!" }() init(name: String) { print("person init") self.name =

Weak property is set to nil in dealloc but property's ivar is not nil

橙三吉。 提交于 2019-12-28 05:35:11
问题 I noticed the following in Objective-C with ARC enabled: Let's have simple class A and autosynthesized weak property @interface A @property (nonatomic, weak) id refObject; @end @implementation A @end And second class B with dealloc implemented @interface B @end @implementation B -(void) dealloc { NSLog(@"In dealloc"); } @end And finally somewhere in class A have the following: @implementation A ... -(void) foo { B* b = [B new]; self.refObject = b; // Just use b after the weak assignment // in

Reference cycles with value types?

给你一囗甜甜゛ 提交于 2019-12-28 04:08:10
问题 Reference cycles in Swift occur when properties of reference types have strong ownership of each other (or with closures). Is there, however, a possibility of having reference cycles with value types only ? I tried this in playground without succes ( Error: Recursive value type 'A' is not allowed ). struct A { var otherA: A? = nil init() { otherA = A() } } 回答1: A reference cycle (or retain cycle ) is so named because it indicates a cycle in the object graph: Each arrow indicates one object

NSError and __autoreleasing

流过昼夜 提交于 2019-12-28 02:32:07
问题 Can someone please explain to me the purpose of having __autoreleasing in the following sample code block? - (void)execute:(NSError * __autoreleasing *)error { // do stuff, possibly assigning error if something went wrong } I removed the __autoreleasing and everything still seems to compile/run fine. I started using obj-c post ARC so I never really learned/understood all those double underscore thingamajigs. I've read the ARC transition guide, but I don't fully understand their NSError

Is it possible to debug “Terminated due to memory error”?

空扰寡人 提交于 2019-12-27 23:39:50
问题 In a certain (consistent) point when my app is running, I consistently get the xcode error message Terminated due to memory error. I cannot find the code causing the error, but I can tell what code is near the error (using breakpoints). The error is caused directly after returning a certain cell in my implemenation of the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDataSource delegate method. I can confirm that it is

iOS 5 Best Practice (Release/retain?)

﹥>﹥吖頭↗ 提交于 2019-12-27 11:33:08
问题 As a beginning iPhone programmer, what is the best practice for writing apps to be used either with iOS 5 or older versions? Specifically, should I continue using the release/retain of data, or should I ignore that? Does it matter? 回答1: It's up to you. You can write apps using ARC (Automatic Reference Counting), and Xcode will write "glue code" to allow your ARC enabled apps to run on iOS 4, no modifications required. However, certain things wont work, and most noticeably many libraries you