Objective-C Grammar I (Class)

Objective-C Grammar I (Class)

First of all, this article is for the author to learn "Objective-C Basic Course" notes, and add the author's own understanding and summary.

1. # import statement

# import ensures that the header file is included only once.
#import <Foundation/Foundation.h>

2. NSLog and @ "String"

The NSLog method, similar to printf, outputs to the console.
@ Symbols denote the NSString type.

3. Boolean Type (BOOL)

YES and NO

4. Categories

@ Interface means that this is the interface of the class, ending with @end.
@ implementation implements the interface defined by the class, ending with @end.
Each class is NSObject.
Define the various data members required by the class in {}.
The short line "-" declaration is a class method followed by the return type, and void means no return value.
The method parameter () defines the parameter type, followed by the parameter name.
@interface ShapeBounds : NSObject {
	int width, height;
}

- (void) setWidth: (int)width height: (int)height;
@end

@implementation ShapeBounds
- (void) setWidth: (int)w height: (int)h {
	width = w;
	height = h;
}

- (NSString*) description {
	return [NSString stringWithFormat: @"(%d, %d)", width, height];
}
@end

int main(int argc, const char* argv[]) {
	ShapeBounds* bounds = [ShapeBounds new];
	[bounds setWidth: 100 height: 60];
	NSLog(@"%@", bounds); // (100, 60)
}

5. Inheritance

When a class is declared, the identifier after the colon ":" is the class that needs to be inherited, and Objective-C only supports single inheritance.
The super keyword can call the method of the parent class.
@interface Shape : NSObject 
- (void) draw;
@end

@implementation Shape
- (void) draw {
	NSLog(@"draw Shape");
}
@end

@interface Circle : Shape
@end

@implementation Circle
- (void) draw {
	[super draw];
	NSLog(@"draw Circle");
}
@end

int main(int argc, const char* argv[]) {
	Circle* circle = [Circle new];
	[circle draw];
}

6. Compound

Shape has its own location, which can be represented by Shape Bounds.
@interface Shape : NSObject {
	ShapeBounds* bounds;
}

- (void) setBounds: (ShapeBounds*) bounds;
- (void) draw;
@end

@implementation Shape
- (void) setBounds: (ShapeBounds*) b {
	bounds = b;
}

- (void) draw {
	NSLog(@"draw Shape at %@", bounds);
}
@end

int main(int argc, const char* argv[]) {
	ShapeBounds* bounds = [ShapeBounds new];
	[bounds setWidth: 100 height: 60];
	Circle* circle = [Circle new];
	[circle setBounds: bounds];
	[circle draw];
}

7. Cross-file dependency

ShapeBounds can be split into ShapeBounds.h and ShapeBounds.m.
ShapeBounds.h file
#import <Foundation/Foundation.h>

@interface ShapeBounds : NSObject {
	int width, height;
}

- (void) setWidth: (int)Width height: (int)height;
@end
ShapeBounds.m file
#import "ShapeBounds.h"

@implementation ShapeBounds
- (void) setWidth: (int)w height: (int)h {
	width = w;
	height = h;
}

- (NSString*) description {
	return [NSString stringWithFormat: @"(%d, %d)", width, height];
}
@end
Shape classes are divided into Shape.h and Shape.m. ShapeBounds classes are referenced in Shape and dependencies must be declared in the file.
@ Class can declare a reference relationship, knowing at compile time that it is a class, but ShapeBounds is modified without recompiling the Shape.h file.
Shape.h file
#import <Foundation/Foundation.h>

@class ShapeBounds;

@interface Shape : NSObject {
	ShapeBounds* bounds;
}

- (void) setBounds: (ShapeBounds*) bounds;
- (void) draw;
@end
Shape.m file
#import "Shape.h"
#import "ShapeBounds.h"

@implementation Shape
- (void) setBounds: (ShapeBounds*) b {
	bounds = b;
}

- (void) draw {
	NSLog(@"draw Shape at %@", bounds);
}
@end
Circle classes are divided into Circle.h and Circle.m. Shape classes in Circle.h files cannot be declared with @class.
Circle.h file
#import "Shape.h"

@interface Circle : Shape
@end
Circle.m file
#import "Circle.h"

@implementation Circle
- (void) draw {
	[super draw];
	NSLog(@"draw Circle");
}
@end
main.m file
#import "Circle.h"
#import "ShapeBounds.h"

int main(int argc, const char* argv[]) {
	ShapeBounds* bounds = [ShapeBounds new];
	[bounds setWidth: 100 height: 60];
	Circle* circle = [Circle new];
	[circle setBounds: bounds];
	[circle draw];
}

Posted by philippe2 on Mon, 17 Jun 2019 12:20:42 -0700