Add function ios7cook directory for class through Method
The method is the building block of the class. For example, a person class can have such features as walking logic, breathing, eating, and drinking water. These features are typically encapsulated.
A method can contain parameters. When a method is called or only the variables passed by the caller are displayed. For example, in a simple world, we have a person-class walk method. If you want to, you can add a parameter or parameter method and name it cgfloat-type walkingspeed ??, Therefore, when another programmer calls the method of your class, she can specify who is going at a speed. As a programmer of this class, you will write the corresponding code and your class will handle different speeds. If it all sounds like too much, don't worry, but look at the example below, here I added a method, the implementation file created in the person class on page 1 of "create and use Class:
#import "Person.h" @implementation Person- (void) walkAtKilometersPerHour:(CGFloat)paramSpeedKilometersPerHour{ /* Write the code for this method here */}- (void) runAt10KilometersPerHour{ /* Call the walk method in our own class and pass the value of 10 */?8 |Chapter 1: Implementing Controllers and Views [self walkAtKilometersPerHour:10.0f]; }@end
A typical method is in objective-C with modifier:
1. The prefix tells the compiler whether the method is an instance method (-) or a class method (+ ). An instance method can be accessed only when the programmer assigns and initializes an instance of your class. Class methods can be accessed directly from the class itself. Don't worry, if it sounds complicated. We will see examples of these methods in this book, so don't stick them here.
For method 2, a data type if any value returned by this method. In our example, we specify an invalid one to tell us that the compiler does not return anything.
3. The first part of the first parameter after the method name. You have not returned any parameter methods. You can have a method without parameters.
4. The first parameter is as follows:
Let me tell you that the example of a method has two parameters:
- (void) singSong:(NSData *)paramSongData loudly:(BOOL)paramLoudly{ /* The parameters that we can access here in this method are: paramSongData (to access the song‘s data) paramLoudly will tell us if we have to sing the song loudly or not */}
It is important to remember that each parameter of each method has an external and internal name. The external name is part of the method, and the internal part is the actual name or the alias of the parameter that can be used in the implementation of the method. In the previous example, the external name of the first parameter is singsong, and its internal name is paramsongdata. The external name of the second parameter is loudly, but its internal name is paramloudly. The method name is combined with the external name of its parameter to form a method called selector. The Selector Used in this case to convert SINGSONG: loudly :. One choice, which you will see later in this book, is the runtime identifier of each method. No two methods of the same class can have the same choice.
In our example, we define three methods for our person class, and its internal implementation file (person. m ):
• Walkatkilometersperhour:
• Runat10kilometersperhour
• SINGSONG: loudly:
If we want to use any of these methods from the outside-for example,
From the application delegate, we should expose the method (person. h) in our interface file ):
#import <Foundation/Foundation.h>@interface Person : NSObject@property (nonatomic, copy) NSString *firstName;@property (nonatomic, copy) NSString *lastName;- (void) walkAtKilometersPerHour:(CGFloat)paramSpeedKilometersPerHour;- (void) runAt10KilometersPerHour;/* Do not expose the singSong:loudly: method to the outside world. That method is internal to our class. So why should we expose it? */@end
In view of this interface file, programmers can call the walkatkilometersperhour: from outside the person class, runat10kilometersperhour method, rather than monotonous:
SINGSONG: loudly: method because it is not made public in this file. So let's move on and try to call all the three methods from our app delegate to see what will happen!
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {person * person = [[person alloc] init]; [person walkatkilometersperhour: 3.0f]; [person runat10kilometersperhour];/* If you uncomment this line of code, the compiler will give you an error telling you this method doesn't exist on the person class */
/* If this line of code is removed, the compiler will give
Your error tells you that this method does not exist for the person class */
// [Person SINGSONG: Nil loudly: Yes]; self. window = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds]; self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeyandvisible]; return yes ;}
Now we know how to define and call the instance method, but for the class method? Let's take a look at the class methods first, and their differences from the instance methods.
An instance method involves an instance method of a class. For example, in our person class, you can instantiate this class twice to create a game where you are using two different people in a hypothetical game with one of those people, walk at a speed of 3 kilometers at a time, while others walk at a speed of 2 kilometers per hour.
Even if you write the code to walk the instance method only once, when two individual person class instances are created at run time, the instance method called will be routed to the corresponding instance of this class.
On the contrary, the work of the class method itself. For example, in a game where you have a game landscape named "light", you may have a dimalllights method in this category, where programmers can call dim lights in the game, no matter where they are placed. Let's take a look at the example of a class method in our person class:
# Import "person. H"
@ Implementation person
+ (Cgfloat) maximumheightincentimeters {return 2500000f;
}
+ (Cgfloat) minimumheightincentimeters {return 40366f;
}
@ End
The maximumheightincentimeters method is a class method that returns the imaginary maximum height of anyone in centimeters. The minimum height of anyone returned by the minimumheightincentim eters class method. The following describes how to expose these methods to the interfaces of our class:
#import <Foundation/Foundation.h>@interface Person : NSObject@property (nonatomic, copy) NSString *firstName; @property (nonatomic, copy) NSString *lastName; @property (nonatomic, assign) CGFloat currentHeight;+ (CGFloat) maximumHeightInCentimeters; + (CGFloat) minimumHeightInCentimeters;@end
We also added a new floating point performance for the class named currentheight person. This allows instances of this class to store their heights in memory for future reference, just like their first or last name.
In the delegate of our application, we will continue to use these new methods, as shown below:
??
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{Person *steveJobs = [[Person alloc] init]; steveJobs.firstName = @"Steve";steveJobs.lastName = @"Jobs"; steveJobs.currentHeight = 175.0f; /* Centimeters */if (steveJobs.currentHeight >= [Person minimumHeightInCentimeters] && steveJobs.currentHeight <= [Person maximumHeightInCentimeters]){/* The height of this particular person is in the acceptable range */}else{/* This person‘s height is not in the acceptable range */} self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];return YES;}
1.4 Add a function for the class: class method and instance method