background preloader

Objective-C

Facebook Twitter

Using Blocks in iOS 4: The Basics. iOS 4 introduces one new feature that will fundamentally change the way you program in general: blocks.

Using Blocks in iOS 4: The Basics

Blocks are an extension to the C language and thus fully supported in Objective-C. If you're coming from a programming language such as Ruby, Python, or Lisp, then you know the power of blocks. Simply put, blocks let you encapsulate chunks of code and pass them around like any other object. It's a different style of programming that you'll want to become familiar with to take advantage of new APIs in iOS 4. Let's start by taking a look at two examples of where you might use blocks in iOS 4: view animations and enumeration. Blocks By Example As our first example, suppose we're creating a card game and we want to animate sliding a card from the dealer's hand to a player's position.

[UIView animateWithDuration:2.0 animations:^ { self.cardView.alpha = 1.0; self.cardView.frame = CGRectMake(176.0, 258.0, 72.0, 96.0); self.cardView.transform = CGAffineTransformMakeRotation(M_PI); }]; Your Turn. Astuce pour debugger un problème d'autorelease pool. Getters, setters and properties for the newbie. iPhone developers who have experience with Macintosh development have probably long since become comfortable with the whole "property" notion in Objective C.

Getters, setters and properties for the newbie

For iPhone developers who are recent converts from other mobile environments (such as Java or Symbian), this can be a bit twistier. In the hopes that some people won't repeat my pain, here's a brief discussion of variables and properties, along with the implications to proper memory management. Let's start with a simple class: //MyClass.h file@interface MyClass: NSObject{ NSString *text;}-(void) init;-(void) logText;@end//MyClass.m file@implementation MyClass- (void)init{ text = @"some text";}- (void)logText{ NSLog(@"%@", text);}@end. Friday Q&A 2010-01-15: Stack and Heap Objects in Objective-C. Friday Q&A 2010-01-15: Stack and Heap Objects in Objective-C Welcome to another Friday Q&A.

Friday Q&A 2010-01-15: Stack and Heap Objects in Objective-C

I survived my travel and am (just barely) ready to write another exciting edition. This week's topic comes from Gwynne, who asked why Objective-C only uses heap objects, and no stack objects. Before we get into that, let's define our terms. Stack The stack is a region of memory which contains storage for local variables, as well as internal temporary values and housekeeping. Heap The heap is, essentially, everything else in memory. Stack vs Heap Objects Given that, what's a stack object, and what's a heap object? First, we must understand what an object is in general. The precise location of that memory is less important. NSObject *obj = [[NSObject alloc] init]; The storage for the obj variable itself is on the stack, but the object it points to is in the heap. A stack object is just an object where the memory for that object is allocated on the stack. Why allow both? Google Objective-C Style Guide. Unlike C++, Objective-C doesn't have a way to differentiate between public and private methods—everything is public.

Google Objective-C Style Guide

As a result, avoid placing methods in the public API unless they are actually expected to be used by a consumer of the class. This helps reduce the likelihood they'll be called when you're not expecting it. This includes methods that are being overridden from the parent class. For internal implementation methods, use a category defined in the implementation file as opposed to adding them to the public header. #import "GTMFoo.h" @interface GTMFoo (PrivateDelegateHandling) - (NSString *)doSomethingWithDelegate; // Declare private method @end @implementation GTMFoo (PrivateDelegateHandling) ... - (NSString *)doSomethingWithDelegate { // Implement this method } ... If you are using Objective-C 2.0, you should instead declare your private category using a class extension, for example: @interface GMFoo () { ... } Again, "private" methods are not really private.

Mac Dev Center: Coding Guidelines for Cocoa: Code Naming Basics. Mac Developer Tips. NSString @property, using copy instead of retain - Stack Overflo.