How do I avoid capturing self in blocks when implementing an API?

Short answer Instead of accessing self directly, you should access it indirectly, from a reference that will not be retained. If you’re not using Automatic Reference Counting (ARC), you can do this: __block MyDataProcessor *dp = self; self.progressBlock = ^(CGFloat percentComplete) { [dp.delegate myAPI:dp isProcessingWithProgress:percentComplete]; } The __block keyword marks variables that can be modified … Read more

Objective-C ARC: strong vs retain and weak vs assign

After reading so many articles Stackoverflow posts and demo applications to check variable property attributes, I decided to put all the attributes information together: atomic //default nonatomic strong=retain //default weak retain assign //default unsafe_unretained copy readonly readwrite //default Below is the detailed article link where you can find above mentioned all attributes, that will definitely … Read more

Sending an HTTP POST request on iOS

The following code describes a simple example using POST method.(How one can pass data by POST method) Here, I describe how one can use of POST method. 1. Set post string with actual username and password. NSString *post = [NSString stringWithFormat:@”Username=%@&Password=%@”,@”username”,@”password”]; 2. Encode the post string using NSASCIIStringEncoding and also the post string you need … Read more

How does the new automatic reference counting mechanism work?

Every new developer who comes to Objective-C has to learn the rigid rules of when to retain, release, and autorelease objects. These rules even specify naming conventions that imply the retain count of objects returned from methods. Memory management in Objective-C becomes second nature once you take these rules to heart and apply them consistently, … Read more

Objective-C declared @property attributes (nonatomic, copy, strong, weak)

Nonatomic Nonatomic will not generate threadsafe routines thru @synthesize accessors. atomic will generate threadsafe accessors so atomic variables are threadsafe (can be accessed from multiple threads without botching of data) Copy copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, … Read more