Definitions of classes and methods
General classes are inherited NSObject, a new class when the parent class select NSObject, OC only single inheritance, not many inheritance.
Below with a simple OC project to explain (need to configure the Xcode tool to the corresponding SDK), will build the project, please skip to the ' six ' step directly.
First, create a new OC compilation environment, open xcode->file->new->project (shortcut key command+shift+n).
Second, select the OS x--application--command line Tool, select Next
Three, fill in the project name (best English), your own name, and company logo. Type selects Foundation, and the following check indicates whether Arc is selected and recommended for beginners to hook up. Click Next
Four, at this time the project is built, will see a main.m file, all the calls in the MAIN.M completed.
Create a new class, Command+n, select the parent class NSObject, name people
After clicking Next, the following interface appears, remember the following √ to play. Otherwise, a compilation error occurs when the file is not searched at compile time.
Add the variable in the People.h file interface, nsstring the string variable of OC.
Copy Code code as follows:
@interface Person:nsobject {
NSString * _NAME;
int _age;
}
Then define the initialization method and set method of the variable outside {}, Format:-(type) method name: (parameter type) argument 1 parameter name: (parameter type) parameter 2......,set method name generally is set variable name, variable name first letter capitalization, this is not the Apple Official document description
Copy Code code as follows:
-(void) Setage: (int) age;
-(void) SetName: (NSString *) name;
Define a Get method for a variable, and the Get method name is also a Get variable name
Copy Code code as follows:
-(int) age;
-(NSString *) name;
Implement the method declared in the People.h file in the PEOPLE.M file.
Copy Code code as follows:
-(void) Setage: (int) Age {
_age = age;
}
-(void) SetName: (NSString *) name {
_name = [Name Copy];//copy method involves the memory management mechanism of OC. Use copy to prevent memory leaks
}
-(int) Age {
return _age;
}
-(NSString *) name {
return _name;
}
Define a people class in MAIN.M and initialize its property variables.
Define a People class:
Copy Code code as follows:
Person *dwight = [[Person alloc] init];
Assign a value to its properties:
Copy Code code as follows:
[Dwight Setname:@ "Dwight Schrute"];
[Dwight Setage:38];
Print values:
NSLog (@ "%@ (%d)", [Dwight Name], [Dwight Age]);
PS: General Nsstring,nsarray,nsdictionary,nsdata and other types of value printing in%@,int and other types of C, in accordance with the C-language printing keywords, nsinteger,nsnumber printing with%d.
Print results:
protocol protocol, setting up agents
a protocol (protocol) is an important part of OBJC that defines a method that can be completed by any other class, and can be implemented by protocol when some functionality is required in this class
Protocol needs to be used in three different situations:
When you need to declare a method that other classes expect to complete
When declaring the interface of a class object while hiding its own class
The similarity between classes that have no inheritance relationship is obtained, that is, there is no inheritance between the two classes, but the same methods need to be implemented by proxy.
The protocol file only defines methods, and when you implement the protocol in a class, you need to supplement them, and the system will automatically call these methods
The method in protocol is divided into @required and @optional, in which the method under @required must be implemented, and the method in @optional is optional or not implemented.
The following is a simple example to illustrate the use of Protocol (OS x) under iOS
One, a new Objective-c class, named Ioscoder
①, define a protocol after ioscoder@end, name Myprotocoldelegate, and declare @required and @optional methods
②, after #import declaration @protocol myprotocoldelegate
③, declaring attributes in interface @property (nonatomic,assign) id<myprotocoldelegate> delegate;
The entire. h file is as follows
Copy Code code as follows:
#import <Cocoa/Cocoa.h>
@protocol myprotocoldelegate;
@interface Ioscoder:nsobject
Declaring delegate Properties
@property (nonatomic,assign) id<myprotocoldelegate> delegate;
@end
Copy Code code as follows:
Define Protocol
@protocol myprotocoldelegate <NSObject>
Methods must be implemented
@required
-(void) Startobjectivec;
-(void) startprograming;
Select the Implementation method
@optional
-(void) startjavascript;
-(void) Startpython;
@end
Second, in a file that needs to be used in the Ioscoder class, declare and initialize it.
If you want to use the delegate in Ioscoder, you must declare it in the. h file and declare the format
@interface Class Name: Inheriting class <MyprotocolDelegate>
Defining Ioscoder Objects
Copy Code code as follows:
Ioscoder *ios = [[Ioscoder alloc] init];
Ios.delegate = self;
if ([iOS respondstoselector: @selector (Startobjectivec)]) {
[Self Startobjectivec];
}
if ([iOS respondstoselector: @selector (startprograming)]) {
[Self startprograming];
}
Implement Required method
Copy Code code as follows:
-(void) Startobjectivec
{
NSLog (@ "@required, Startobjectivec");
}
-(void) startprograming
{
NSLog (@ "@required, startprograming");
}
Attached: When used in iOS Viewcontroller
If a to jump to the B page, you need to implement the Protocol method of a in page b
. h Files
Copy Code code as follows:
#import <UIKit/UIKit.h>
@class Aviewcontroller;
@protocol adelegate <NSObject>
-(void) Amethod
@end
Copy Code code as follows:
@interface Aviewcontroller:uiviewcontroller
{
Id<adelegate> _delegate;
}
@property (nonatomic,assign) id<adelegate> delegate;
@end
Before the page jumps, add
Copy Code code as follows:
if (_delegate && [_delegate respondstoselector: @selector (Amethod)]) {
[_delegate Amethod];
}
Declare proxy <adelegate> in page B, implement Amethod method, instantiate object A, and set proxy
Then the Amethod method is invoked on the B interface, which completes the implementation of the Protocol