Designing a class Interface
Design Interface
The objective-C syntax for creating a class is very simple. It typically comes in two parts. The syntax for creating a class is very simple and usually consists of two parts.
The class interface is usually stored in the classname. h file, and defines instance variables and public methods. the class interface usually defines the instance variables and public methods, and stores them in the class name. H (classname. h.
The implementation is in the classname. M file and contains the actual code for these methods. it also often defines private methods that aren't available to clients of the class. the implementation file of the class interface is saved in "class name. M (classname. m) "file, including the real implementation code of public methods. Private methods are often defined in this file. Private methods cannot be called by customer classes.
Here's what an interface file looks like. the class is called photo, so the file is named photo. h: The following is an instance of the class interface file. The class name is photo, so the file name is photo. h:
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer;}@end
First, we import cocoa. h, to pull in all of the basic classes for a cocoa app. the # import directive automatically guards against including a single file multiple times. first, import cocoa. h file to add all the basic classes required by the cocoa application. Command # import must be added only once, even if it is used multiple times.
The @ interface says that this is a declaration of the class photo. The colon specifies the superclass, Which is nsobject. @ interface indicates that this is a photo class declaration. Colon (:) specifies the parent class as nsobject.
Inside the curly brackets, there are two instance variables: caption and photographer. both are nsstrings, But they cocould be any object type, including ID. two instance variables are defined in braces: caption and photographer. They are all nsstrings strings and can also be any object type, including the ID type.
Finally, the @ end symbol ends the class declaration. Finally, @ end identifies the end class declaration. Add Methods
Add Method
Let's add some getters for the instance variables: Add accessors to the instance variables:
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer;}- caption;- photographer;@end
Remember, objective-C methods typically leave out the "get" prefix. A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. remember, the OC method usually skips the "get" prefix. This is an instance object method, and the plus sign is identified as a class method.
By default, the compiler assumes a method returns an ID object, and that all input values are ID. the above code is technically correct, but it's unusual. let's add specific types for the return values: the return value of the default method of the compiler is an ID object, and all input values are also IDs. The above code is technically correct, but not practical. The following code illustrates the return value type for a method:
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer;}- (NSString*) caption;- (NSString*) photographer;@end
Now let's add setters: add the setting method now:
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer;}- (NSString*) caption;- (NSString*) photographer;- (void) setCaption: (NSString*)input;- (void) setPhotographer: (NSString*)input;@end
Setters don't need to return a value, so we just specify them as void. The value method does not return a value, so it is of the void type.
Original article: learn_objective_c Part 5