Familiarize yourself with OC syntax. Write some classes and properties from scratch
In OC, every class is associated with two files, a header file and an implementation file. The header file represents the public interface and the implementation file holds method implementations as well as any properties we'd like to be private.
1.Declare a house class with 3 properties:
Swift Version
import Foundation
class house: NSObject {
var address = String()
var numberOfBedrooms = Int()
var hasHotTub: Bool = false
}
OC Version
#import <Foundation/Foundation.h>
@interface House : NSObject
@property (nonatomic) NSString *address;
@property (nonatomic) int numberOfBedrooms;
@property (nonatomic) BOOL hasHotTub;
@end
说明:
@interface
: 声明一个class,要在前面加上@interface关键字;
@property
:声明属性,要在前面加上@property关键字;
*address
:星符号指明一个指针指向address;
Nonatomic/Atomic
:Nonatomic is a property attribute. Properties can be either atomic or nonatomic; this distinction relates to how properties are handled in multithreading. It is beyond the scope of this course to get into the specifics of the difference, but the vast majority of properties you will use will be nonatomic. The default is atomic, so you need to write “nonatomic” almost every time you declare a property.
2.在main.m文件中
#import <Foundation/Foundation.h>
#import "House.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
House *myHouse = [[House alloc] init];
NSLog(@"%@", myHouse);
}
return 0;
}
说明:
#import "House.h"
:要先import
[[House alloc] init]
:Every time you create an object, you type alloc
, which allocates some memory space and then init
to initialize the object instance. alloc
and init
are always written together.
3.Syntax of method definitions
-(returnType)methodName: (parameter Type*)parameterName {
body;
}
// House.m
#import "House.h"
@interface House()
@property (nonatomic, readwrite) int numberOfBedrooms;
@end
@implementation House
-(instancetype)initWithAddress: (NSString*)address {
self = [super init];
if(self) {
_address = [address copy];
_numberOfBedrooms = 2;
_hasHotTub = false;
}
return self;
}
@end
说明:
self
: First of all, what is self? You saw self from time to time in Swift, but in Objective-C its use is much more widespread. Self is a pointer to the object at hand. When you are writing code inside of a class, self is a pointer to an object of that class. Self is a pointer to the object at hand.
Second of all, what's an instance variable? An instance variable is what lies underneath a property. The word “property” technically refers to an instance variable's accessor methods, the getter and the setter that are automatically created when a property is declared. The thing itself, the address, in this case, is an instance variable. We actually almost never interact directly with instance variables. In fact, it's not recommended that you interact directly with instance variables outside of custom initializers.
:
Every statement in Objective-C must end with a semicolon.
Quiz1:Create an Objective-C class with accompanying header file. The class will represent a book, and should have a title, author, and a year of publication. Be sure to provide a designated initializer.
// Book.h
@interface Book : NSObject
@property (nonatomic) NSString *title;
@property (nonatomic) NSString *author;
@property (nonatomic) int yearOfPublication;
-(instancetype)initWithTitle:(NSString*)title
author:(NSString*)author
year:(int)year;
@end
// Book.m
@implementation Book
-(instancetype)initWithTitle:(NSString*)title
author:(NSString*)author
year:(int)year {
self = [super init];
if(self) {
_title = title;
_author = author;
_yearOfPublication = year;
}
return self;
}
@end
4.Enum in OC
Enum definitions go at the top of your header file. The preferred way to create an enum is using the NS_ENUM
macro, where NSInterger
is the enum type and Direction
is the enum name. All enums in OC are of type NSInteger.
// Bedroom.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, Direction) {
North,
South,
East,
West
};
NS_ASSUME_NONNULL_BEGIN
@interface Bedroom : NSObject
@property (nonatomic) BOOL privatePath;
@property (nonatomic) Direction directionWindowFaces;
@end
NS_ASSUME_NONNULL_END
Important!---Use weak reference for: 1.delegate 2.subviews of the main view
More about memory cycle:
Apple's explanation of object ownership
An example of a retain cycle from stack overflow
Why we use weak pointers for delegation, from stack overflow
Debugging memory cycles: four likely culprits
Using Instruments to find memory leaks