当前位置:网站首页>Object initialization
Object initialization
2022-07-25 09:36:00 【Hills and valleys】
List of articles
Chapter one OC The singleton model
Chapter two OC Initialization of
The third chapter OC And agreements
Chapter four OC And main Some operations and basic concepts of functions
Preface
Talking about initialization method and rewriting of initialization method
One 、 Object initialization ?
- What is object initialization ?
After we create an object , Assigning values to its properties is called object initialization
- Allocate space for objects :
[[ Class name alloc]init]
[ Class name new]
These two methods are essentially the same .
- Reset the memory space of each instance variable to 0;
The shaping variable is reset to 0, Floating point objects are reset to 0.0.BOOL Type reset to NO.
- The function of initialization
Objects that only allocate memory cannot be used , Must be initialized ,OC The most common initialization method is init.
init Method can complete initialization , But it just completes the most basic initialization , therefore , All member variables of the object are still 0
Let's see if Not initialized What will happen :
Class interface :
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface User : NSObject
@property(nonatomic,copy) NSString*name;
@property(nonatomic,assign) int age;
@end
NS_ASSUME_NONNULL_END
The realization of the class :
Insert a code chip here #import "User.h"
@implementation User
@synthesize name;
@synthesize age;
@end;
test :
#import"User.h"
#import<Foundation/Foundation.h>
int main() {
@autoreleasepool {
User* user = [[User alloc]init];
NSLog(@"user.name = %@,user.age = %d", user.name, user.age);
}
}
result :
Here we see the importance of initialization .
Two 、 Initialization method
The interface and implementation of the class are the same as above , Give the test part :
#import"User.h"
#import<Foundation/Foundation.h>
int main(){
@autoreleasepool {
User* user = [[User alloc]init];
user.name = @" Nana ";
user.age = 18;
NSLog(@"user.name = %@,user.age = %d", user.name, user.age);
}
}
result :
3、 ... and 、 Override initialization method
Class interface :
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface User : NSObject
@property(nonatomic,copy) NSString*name;
@property(nonatomic,assign) int age;
@end
NS_ASSUME_NONNULL_END
The realization of the class :
#import "User.h"
@implementation User
@synthesize name;
@synthesize age;
-(id)init{
if(self = [super init]){
self.name = @"wang";
self.age = 18;
}
return self;
}
@end
Test part :
#import"User.h"
#import<Foundation/Foundation.h>
int main(){
@autoreleasepool {
User* user = [[User alloc]init];
NSLog(@"user.name = %@,user.age = %d", user.name, user.age);
User* m = [[User alloc]init];
NSLog(@"m.name = %@,m.age = %d", user.name, user.age);
}
}
result :
We found that , These two objects are calling init When the method is used , Will automatically assign values to objects . Because we rewrite init Method , What we use here is init The method is no longer NSObject Provided , But we rewrite . Before we call the rewritten init After the method , Will automatically assign values to objects .
边栏推荐
- 单例模式(Singleton)
- ~2 ccf 2022-03-1 未初始化警告
- 梦想启航(第一篇博客)
- CoreData存储待办事项
- 类(2) 和 协议
- 粗柳簸箕细柳斗,谁嫌爬虫男人丑 之 异步协程半秒扒光一本小说
- *7-1 CCF 2015-09-1 数列分段
- How to obtain location information (longitude and latitude) by uni app
- Numpy - Construction of array
- Understand why we should rewrite the equals method and hashcode method at the same time + example analysis
猜你喜欢

Flex layout syntax and use cases

How many regions can a positive odd polygon be divided into

Redis installation (Ubuntu)

Install MySQL in Ubuntu and create new users

TCP network application development process

@5-1 CCF 2019-12-1 报数

A brief introduction to the interest of convolutional neural networks

Swagger2显示get接口有问题,加注解就能解决

What are stand-alone, cluster and distributed?

The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
随机推荐
什么是脑裂问题?
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
Flex 布局语法与用例
@4-1 CCF 2020-06-1 线性分类器
Go foundation 3
【代码源】每日一题 三段式
Redis installation (Ubuntu)
【代码源】每日一题 分数拆分
[GPLT] 2022 大众情人(floyd)
OC--Foundation--字符串+日期和时间
最短路问题 Bellman-Ford(单源最短路径)(图解)
对象数据如何转化成数组
OC--对象复制
Learn redis Linux and install redis
Laravel calls a third party to send mail (PHP)
OC -- category extension agreement and delegation
laravel 调用第三方 发送邮件 (php)
OC--Foundation--数组
~3 ccf 2022-03-2 出行计划
~1 ccf 2022-06-2 寻宝!大冒险!