Objective C is used in the development of iOS, which is an object-oriented language, so it is very simple for programmers who have mastered the knowledge of object-oriented language. The files completed in Objective are called interface files, and the definition of such files is called implementation files. A simple interface file, MyClass.h, will look like this: 执行MyClass.m文件,如下所示 Finish creating the object, as shown below The methods declared in Objective C are as follows: An example is shown below: You may wonder what an andBreadth string is, but its optional string can help us read and understand methods, especially when methods are called. To call this method in the same class, we use the following statement. As mentioned above, the use of andBreath helps us understand that breath is 20. Self is used to specify that it is a class method. 类方法(class methods) You can access class methods directly without creating objects. They don’t have any variables associated with it. Examples are as follows: It can be accessed by using the class name (assumed as the MyClass class name), as follows: 实例方法 The object of the class that can be created only accesses the instance method and the instance variable to which memory is allocated. The method of the instance is as follows: After creating an object of the class, it can access it. As follows: Serial number Data type 1 NSString string 2 Basic types of CGfloat floating point values 3 NSInteger integer 4 BOOL Boolean type NSLog is used to print a declaration that will be printed on the device log and the debug version of the console and on the separate debug mode. Such as NSlog (@ “”) With a few additional provisions, most of the control structures are the same as those of C and C++ 属性(properties) Used to access the variable properties of the external class of the class For example: @ property (non-atomic, strong) NSString*myString 访问属性 You can access a property using the dot operator, and to access the previous property, you can do the following You can also use the method of set, as follows: 类别(categories) Class is used to add methods to an existing class. In this way, you can add methods to the class and even define the actual class in it without even executing the file. The sample category of MyClass, as follows: NSMutableArray and NSArray are array classes used in ObjectiveC, the former is a mutable array, and the latter is an immutable array. It is as follows: NSMutableDictionary and NSDictionary are dictionaries used in Objective, the former mutable dictionary and the latter immutable dictionary, as follows: 9.4.1. Interface and implementation ¶
@interface MyClass:NSObject{
// 类变量声明
}
// 类属性声明
// 类方法和声明
@end
@implementation MyClass
// 类方法定义
@end
9.4.2. Create object ¶
MyClass *objectName = [[MyClass alloc]init] ;
9.4.3. Method (methods) ¶
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
-(void)calculateAreaForRectangleWithLength:(CGfloat)length
andBreadth:(CGfloat)breadth;
[self calculateAreaForRectangleWithLength:30 andBreadth:20];
+(void)simpleClassMethod;
[MyClass simpleClassMethod];
-(void)simpleInstanceMethod;
MyClass *objectName = [[MyClass alloc]init] ;
[objectName simpleInstanceMethod];
9.4.4. Important data types of Objective C ¶
9.4.5. Print log ¶
9.4.6. Control structure ¶
self.myString = @"Test";
[self setMyString:@"Test"];
@interface MyClass(customAdditions)
- (void)sampleCategoryMethod;
@end
@implementation MyClass(categoryAdditions)
-(void)sampleCategoryMethod{
NSLog(@"Just a test category");
}
9.4.7. Array ¶
NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];
[anArray addObject:@"firstobject"];
NSArray *aImmutableArray = [[NSArray alloc]
initWithObjects:@"firstObject",nil];
9.4.8. Dictionaries ¶
NSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init];
[aMutableDictionary setObject:@"firstobject" forKey:@"aKey"];
NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:
@"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]];