This article mainly introduces the different places of C + +.
1, the definition of the class:
A class has two files one. h also has a. m file
1 #import<Foundation/Foundation.h>2 3 4 @interfaceMyclass:nsobject {5 @public6 int_a;//Public Methods7NSString *_name;8 @private9 int_b;//Private MethodsTen } One- (void) test;//instance Method A+ (void) Static_test;//Static Methods - @end
The contents of the header file are as above. The declaration of a class begins with @interface @end ends.
The member variable in the middle of the curly brace, which can represent public and private and protected. The recommended way of OBJC is to precede the member variable with "_".
Method is divided into two, if it is an instance method, that is, a non-static method must begin with "-", if it is a static method must start with "+".
1 #import "MyClass.h"2 3 4 @implementationMyClass {5 intA;6 floatb;7 8 }9- (void) Test {Ten One } A -+ (void) Static_test { - the } - - - @end
m files are similar to CPP files in C + +, with a file ending with @implementation beginning with @end.
You can declare some variables in the middle of the braces, which can only be used in this file, a bit like the global static variables of C, but I don't understand the difference between this and the H file's private variables?
2. Key value Code
@interface**pass; @end
1 User *user = [[user alloc] init]; 2 [User setValue:@ "Jack" forkey:@ "name"// Key value Code 3 [user setValue:@ "123" forkey:@ "Pass "];
A Java-like reflection gets member variables, but is more convenient.
3, Categories (category)
1 @interface User (U) 2 -(void) login; 3 @end
[User Login];
Similar to the expansion method of C #.
4, extension and category, but there is no () middle class
5. Commission
1 @protocolMyDelegate//Statement of the agreement2 3- (void) fun;4 5 @end6 7 @interfaceClassa:nsobject {8 9 ID<Mydelegate>Delegate;//declaring a delegate object, we're going to delegate things to the class that implements the MyDelegate protocol.Ten One } A@property (assign,nonatomic)ID<Mydelegate>Delegate; - -- (void) Funtoother; the @end
1 @implementationClassA {2 3 }4 5- (ID) init {6 7 Delegate=[ClassB alloc];8 returnSelf ;9 Ten } One A- (void) Funtoother { - -[DelegateFun];//Send a message to a delegate to process the - } - - @end
1 @implementation ClassB {23} 4 -(void) fun{5 NSLog (@ "bbbbbbb") ; 6 7 }8@end
1 ClassA *a1 = [[ClassA alloc] init]; 2 [A1 Funtoother];
This is the same thing as a delegate in C #.
iOS Language summary