Sunday, August 11, 2013

Objective C continued discussion
Properties encapsulate an object's values so you define the interface of type NSObject and add the properties. Properties values are accessed by Get or Set property values. Custom accessors can also be implemented. Properties are atomic by default means that the value is retrieved in its entirety even if accessed from different threads.  Objects can be allocated on the heap and their lifetime managed by reference counting. This is necessary because the scope of the pointer variable does not guarantee the lifetime of the object. Furthermore, the objects should not each be managed manually but through relationships between the objects. A strong reference is sufficient to keep the object alive. Avoid strong reference cycles though.
Use strong and weak declarations to manage ownership.
Blocks can be defined to represent a single task or a collection of methods. This is defined with the ^ syntax.  The braces indicate the start and the end of the methods. It can be assigned like a variable.
Blocks can take arguments and return values. Blocks can capture values from enclosing scope. You can pass blocks as arguments to methods or functions. Blocks can simplify concurrent tasks.
Errors are encountered in almost all applications. Some of them can be recoverable. There are some delegate methods that alert you to errors. Some methods pass errors by reference. If possible, display the error to the user. Errors can be generated as well and handled. Use the @try, @catch and @finally semantics. NSError can be used for most errors. Other than that, there may be some delegates that can alert on errors. Some methods pass errors by reference.
Some conventions to follow include the following:
Class names must be unique across an entire app. This means we cannot give general names that conflict with others such as with those in the frameworks. Some common prefixes used are NS for foundation framework and application kit, UI for UIKit, AB for address book, CA for core animation, CI for core image. If you are using classes, you could have three letter prefixes. Method names should be expressive and unique within the class. When you use the @property syntax, the compiler synthesizes the methods. Custom methods for these properties could use the dot syntax notation. Similarly, object creation method names must follow conventions.Class factory memthod should always start with the name of the class (without the prefix) that they create. In the case of NSArray class factory methods start with an array.
Categories add methods to existing classes. Categories are declared with the @interface ClassName (CategoryName) syntax. It can be added for any class, even if you don't have the original implementation source code. Any methods that you declare in a category will be available to all instances of that category.

No comments:

Post a Comment