Tuesday, 10 March 2015

Creating and Running Our First iOS App



Before we dive any deeper into the features of Objective-C, we should have a brief look at how to create a simple iOS app in Xcode. Xcode is Apple’s IDE (integrated develop‐ ment environment) that allows you to create, build, and run your apps on iOS Simulator and even on real iOS devices. We will talk more about Xcode and its features as we go along, but for now let’s focus on creating and running a simple iOS app. I assume that you’ve already downloaded Xcode into your computer from the Mac App Store. Once that step is taken care of, please follow these steps to create and run a simple iOS app: 1. Open Xcode if it’s not already open. 2. From the File menu, choose New Project... 3. In the New Project window that appears, on the lefthand side under the iOS cate‐ gory, choose Application and then on the right hand side choose Single View Application. Then press the Next button. 4. On the next screen, for the Product Name, enter a name that makes sense for you. For instance, you can set the name of your product as My First iOS App. In the
Organization Name section, enter your company’s name, or if you don’t have a company, enter anything else that makes sense to you. The organization name is quite an important piece of information that you can enter here, but for now, you don’t have to worry about it too much. For the Company Identifier field, enter com.mycompany. If you really do own a company or you are creating this app for a company that you work with, replace mycompany with the actual name of the com‐ pany in question. If you are just experimenting with development on your own, invent a name. For the Devices section, choose Universal. 5. Once you are done setting the aforementioned values, simply press the Next button. 6. You are now being asked by Xcode to save your project to a suitable place. Choose a suitable folder for your project and press the Create button. 7. As soon as your project is created, you are ready to build and run it. However, before you begin, make sure that you’ve unplugged all your iOS devices from your com‐ puter. The reason behind this is that once an iOS device is plugged in, by default, Xcode will attempt to build and run your project on the device, causing some issues with provisioning profiles (which we haven’t talked about yet). So unplug your iOS devices and then press the big Run button on the top-lefthand corner of Xcode. If you cannot find the Run button, go to the Product menu and select the Run menu item. Voilà! Your first iOS app is running in iOS Simulator now. Even though the app is not exactly impressive, simply displaying a white screen in the simulator, this is just the first step toward our bigger goal of mastering the iOS SDK, so hold on tight as we embark on this journey together.

Creating and Taking Advantage of Classes


A class is a data structure that can have methods, instance variables, and properties, along with many other features, but for now we are just going to talk about the basics. Every class has to follow these rules:
• The class has to be derived from a superclass, apart from a few exceptions such as NSObject and NSProxy classes, which are root classes. Root classes do not have a superclass. • It has to have a name that conforms to Cocoa’s naming convention for methods. • It has to have an interface file that defines the interface of the class. • It has to have an implementation where you implement the features that you have promised to deliver in the interface of the class. NSObject is the root class from which almost every other class is inherited. For this example, we are going to add a class, named Person, to the project we created in “Cre‐ ating and Running Our First iOS App” on page 2. We are going to then add two prop‐ erties to this class, named firstName and lastName, of type NSString. Follow these steps to create and add the Person class to your project: 1. In Xcode, while your project is open and in front of you, from the File menu, choose New → File... 2. On the lefthand side, ensure that under the iOS main section you have chosen the Cocoa Touch category. Once done, select the Objective-C Class item and press the Next button. 3. In the Class section, enter Person. 4. In the “Subclass of ” section, enter NSObject. 5. Once done, press the Next button, at which point Xcode will ask where you would like to save this file. Simply save the new class into the folder where you have placed your project and its files. This is the default selection. Then press the Create button, and you are done. You now have two files added to your project: Person.h and Person.m. The former is the interface and the latter is the implementation file for your Person class. In Objective- C, .h files are headers, where you define the interface of each class, and .m files are implementation files where you write the actual implementation of the class. Now let’s go into the header file of our Person class and define two properties for the class, of type NSString: @interface Person : NSObject @property (nonatomic, copy) NSString *firstName; @property (nonatomic, copy) NSString *lastName; @end Just like a variable, definition of properties has its own format, in this particular order:
1. The definition of the property has to start with the @property keyword. 2. You then need to specify the qualifiers of the property. nonatomic properties are not thread-safe. We will talk about thread safety in Chapter 16. You can also specify assign, copy, weak, strong, or unsafe_unretained as the property qualifiers. We will read more about these soon too. 3. You then have to specify the data type of the property, such as NSInteger or NSString. 4. Last but not least, you have to specify a name for the property. The name of the property has to follow the Apple guidelines. We said that properties can have various qualifiers. Here are the important qualifiers that you need to know about: strong Properties of this type will be retained by the runtime. These can only be instances of classes. In other words, you cannot retain a value into a property of type strong if the value is a primitive. You can retain objects, but not primitives. copy The same as strong, but when you assign to properties of this type, the runtime will make a copy of the object on the right side of the assignment. The object on the righthand side of the assignment must conform to the NSCopying or NSMutable Copying protocol. assign Objects or primitive values that are set as the value of a property of type assign will not be copied or retained by that property. For primitive properties, this qualifier will create a memory address where you can put the primitive data. For objects, properties of this type will simply point to the object on the righthand side of the equation. unsafe_unretained The same as the assign qualifier. weak The same as the assign qualifier with one big difference. In the case of objects, when the object that is assigned to a property of this type is released from memory, the runtime will automatically set the value of this property to nil. We now have a Person class with two properties: firstName and lastName. Let’s go back to our app delegate’s implementation (AppDelegate.m) file and instantiate an object of type Person:

#import "AppDelegate.h" #import "Person.h" @implementation AppDelegate - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ Person *person = [[Person alloc] init]; person.firstName = @"Steve"; person.lastName = @"Jobs"; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } We are allocating and initializing our instance of the Person class in this example. You may not know what that means yet, but continue to the “Adding Functionality to Classes with Methods”