当前位置:网站首页>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;
@end
3. 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_END
Use 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
边栏推荐
- Unity knapsack system (code to center and exchange items)
- Consul of distributed service registration discovery and unified configuration management
- SQL injection (1) -- determine whether there are SQL injection vulnerabilities
- 2006 translation
- No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
- warning: LF will be replaced by CRLF in XXXXXX
- Recent learning fragmentation (14)
- Explain AI accelerator in detail: why is this the golden age of AI accelerator?
- PMP 考試常見工具與技術點總結
- Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
猜你喜欢
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
New year's first race, submit bug reward more!
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
functools下的reduce函数
Session learning diary 1
What is the difference between enterprise wechat applet and wechat applet
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
Want to do something in production? Then try these redis commands
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
随机推荐
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
Practical multifunctional toolbox wechat applet source code / support traffic master
CSCI 2134
JVM family -- heap analysis
Typical applications of minimum spanning tree
Which product is better for 2022 annual gold insurance?
Formulaire day05
Experience summary of the 12th Blue Bridge Cup (written for the first time)
PMP 考試常見工具與技術點總結
logistic regression
How much does it cost to open a futures account in China? Where is it safe to open an account at present?
Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
Third party login initial version
96% of the collected traffic is prevented by bubble mart of cloud hosting
PID of sunflower classic
Unity controls the selection of the previous and next characters
How to use websocket to realize simple chat function in C #
Imperial cms7.5 imitation "D9 download station" software application download website source code
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
Recent learning fragmentation (14)