当前位置:网站首页>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
边栏推荐
- Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
- 基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
- XSS prevention
- 7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
- Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
- Record a problem that soft deletion fails due to warehouse level error
- System integration meets the three business needs of enterprises
- Third party login initial version
- Development of digital collection trading platform development of digital collection platform
- Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
猜你喜欢
Webhook triggers Jenkins for sonar detection
Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
What are the virtual machine software? What are their respective functions?
Have you entered the workplace since the first 00???
If you have just joined a new company, don't be fired because of your mistakes
Audio and video technology development weekly | 232
Teach you how to optimize SQL
Lichuang EDA learning notes 14: PCB board canvas settings
Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
随机推荐
Audio and video technology development weekly | 232
New year's first race, submit bug reward more!
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Imperial cms7.5 imitation "D9 download station" software application download website source code
Consul of distributed service registration discovery and unified configuration management
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
Why is it recommended that technologists write blogs?
[latex] production of complex tables: excel2latex and detail adjustment
[untitled]
Development of digital collection trading platform development of digital collection platform
Typical applications of minimum spanning tree
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
投资深度思考
Teach you how to optimize SQL
warning: LF will be replaced by CRLF in XXXXXX
What is cloud primordial?
[UE4] parse JSON string
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
数据库SQL语句汇总,持续更新......