Modern Objective-C
Many features have been introduced lately (after release of Xcode 4.4) to the syntax of Objective-C. These new features simplifies development and relieves the developer from the long code, which, sometimes, can result in unreadable code. In this post we try to give you a concise summary of the new features, as we couldn't find any web page that does.
Method definition:
Previously if you had to call a method from inside another method, it has to be defined in an interface block or before the calling method in the implementation block. It’s now possible to declare a method anywhere in the code and it can be called from any other function. Example:
@interface Person : NSObject
- (void) setPhoneNumber:(NSString *)phoneNumber;
@end
@implementation Person
- (void)setPhoneNumber:(NSString *)phoneNumber{
[self validatePhoneNumber:phoneNumber]
//Rest of the method.
}
- (BOOL)validatePhoneNumber:(NSString *)phoneNumber{
//Method body
}
@end
Properties:
It’s no longer needed to call synthesize for the properties. It’s synthesized by default. An instance variable will be declared and named “_propertyName”.
In case all the accessor methods are provided for read‐write properties, or the getter is provided for read-only properties, the instance variable is not generated and the developer is responsible for creating it.
NSNumber Literals:
To create an NSNumber object, one of the convenience constructors must be called, like the following.
NSNumber *number = [NSNumber numberWithInt:10];
It’s now possible to use literals to create an NSNumber object without having to call any methods.
number = @10;
number = @YES;
number = @13.4;
Container Literals:
The same issue with the creation of NSNumber objects is also present with the creation of the container objects like NSArray and NSDictonary. It’s now possible to use literals to create container objects.
NSString *string = @"This is a string.";
NSObject *object = [[NSObject alloc] init];
NSNumber *number = @123;
NSArray *array = @[string, object, @123];
NSDictionary* dictionary = @{@"key1" : string, @"key2" : object, @"key3" : number};
Boxed Expression:
In addition to the supported object literals, it’s now possible to turn an expression to an object by using boxed expression. The syntax is @(expression).
NSNumber *number = @(n * f + 5 );
NSString *path = @(getenv("PATH"));
Object Subscription:
It’s possible to use the C arrays’ subscription syntax with NSArray’s and NSDictionary’s and with custom objects.
Using subscripts with NSArray:
NSMutableArray *array = ....
id someObject = array[0];
array[1] = @"string";
Using subscripts with NSDictionary:
NSMutableDictionary *dictionary = ...
id key_object = ....
id objectFromDictionary = dictionary[key_object];
dictionary[key_object2] = @"string";
It’s also possible to use subscripts with custom objects by declarting the following methods:
- (id)objectAtIndexedSubscript:(NSUInteger)index
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index
Built-in Refactoring Tool:
Xcode 4.4 (and newer) provides a refactoring tool to convert your projects to the modern Objective-‐C. Choose Edit -‐> Refactor -‐> Convert to Modern Objective-C Syntax...
References:
You should check them too.
- clang.llvm.org/docs/ObjectiveCLiterals.html
- "Modern Objective-C" session video at WWDC 2012. This session gives all needed details with examples.
Read More
Today In Islamic History1
It's my pleasure to announce that BadrIT has released Today In Islamic History. It is an iPhone application that keeps you updated with most important events in Islamic and Arabic history. Events are categorized by day, month or year.
For more information, follow the application accounts on Facebook and Twitter.
Read MoreRunning iPhone Apps in the Background
iPhone OS4 has been introduced with a new important feature which is multitasking.
Multitasking and the ability to run program in the background opened a new development field for iPhone developers, so I just wanted to blog an introduction tutorial to develop an application that runs in background.
When an app According to the iOS Application Programming Guide, if the device doesn't have iPhone OS4 or later versions it will not support multitasking and the application delegate will receive the applicationWillTerminate: message instead of applicationDidEnterBackground: .
I attached a project that demonstrates the multitasking capabilities, find it here .
In The "AppDelegate" class, you will notice the new messages that the application sends to notify the delegate of its foreground/background states.
I added logging message to show the sequence of calling these messages.
In the applicationDidEnterBackground: message you can perform some tasks to wrap up your work, and the framework only gives you a small amount of time to complete wrapping your work, so I tested that on the simulator and it gave me about 10 seconds to complete my background task until my application is purged from memory.
Also I add 2 alerts to demonstrate the new local notification feature, it's very helpful feature when it comes to alerting the user at a certain time or after a certain duration. The alerts will be registered when the AppDelegate executes applicationDidEnterBackground message.
I tried to keep it simple and quick for you to start working with background feature and local notification feature. For more info please see the reference .
Read More
Arkami app is the Top Paid App
Today, 5 October 2011, Our iPhone app Arkami is ranked the top paid app on the Egyptian App Store.

Localization of your iOS application's Icon.png.
Read More

