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”
No comments:
Post a Comment