当前位置:网站首页>Objective C attribute keyword
Objective C attribute keyword
2022-07-04 03:34:00 【Zyryck】
Two ways to access and assign member variables :
One .get set The way
@interface Person : NSObject{
// Member variables
NSString* _name;
}
-(void)setName:(NSString*)name;
-(NSString*)name;
@end#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person* p1 = [Person new];
[p1 name];
[p1 setName:@"dancer"];
}
return 0;
}
Two . Attribute mode
[email protected](strong, nonatomic)
@interface Person : NSObject{
// Member variables
NSString* _name;
}
// attribute
@property(strong, nonatomic)NSString* name;
@end#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person* p1 = [Person new];
p1.name = @"dancer";
NSString* name = p1.name;
NSLog(@"name = %@",name);
}
return 0;
}
2. Statement property Statement synthesize
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject{
NSInteger _age;
}
// attribute
@property(assign, nonatomic)NSInteger age;
@end
NS_ASSUME_NONNULL_END
#import "Person.h"
@implementation Person
@synthesize age;
@end3. Omit attribute method of member variable
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject{
NSInteger _age;
}
// attribute
@property(strong, nonatomic)NSString* name;
@property(assign, nonatomic)NSInteger age;
// No corresponding member variable can automatically generate a private member variable _ Property name
@property(assign, nonatomic)NSInteger weight;
@end
NS_ASSUME_NONNULL_ENDUse synthesize Modify the name of the automatically generated member variable
#import "Person.h"
@implementation Person
@synthesize age;
// Use synthesize Modify the name of the automatically generated member variable
@synthesize weight = _myWeight;
@end
边栏推荐
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
- Site favorites
- JS object definition
- [Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
- Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
- [Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
- Have you entered the workplace since the first 00???
- Which product is better if you want to go abroad to insure Xinguan?
- How to pipe several commands in Go?
- WordPress collection WordPress hang up collection plug-in
猜你喜欢

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!

Webhook triggers Jenkins for sonar detection

Defensive programming skills

Monitoring - Prometheus introduction

Add token validation in swagger
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/98/3e5f1094141e34d7e77f908e12acda.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

Leetcode51.n queen

No clue about the data analysis report? After reading this introduction of smartbi, you will understand!

Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
随机推荐
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
機器學習基礎:用 Lasso 做特征選擇
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
How to pipe several commands in Go?
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
Add IDM to Google browser
Rhcsa day 2
Leetcode51.n queen
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Why is it recommended that technologists write blogs?
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
CSCI 2134
functools下的reduce函数
Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
投资深度思考
Management and thesis of job management system based on SSM