当前位置:网站首页>Objective-C description method and type method
Objective-C description method and type method
2022-07-04 03:34:00 【Zyryck】
descprition Method
Person.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject
@property(strong, nonatomic) NSString* name;
@end
NS_ASSUME_NONNULL_END
Person.m
rewrite description Method
#import "Person.h"
@implementation Person
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@", self.name];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person* p1 = [Person new];
p1.name = @"dancer";
// One is called behind the scenes description Method
NSLog(@"p1:%@",p1);
}
return 0;
}

Type method
Person.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject
-(instancetype)initWithName:(NSString*)name;
+(instancetype)personWithName:(NSString*)name;
+(void)sayHello;
@end
NS_ASSUME_NONNULL_ENDPerson.m
#import "Person.h"
@interface Person()
@property(strong, nonatomic)NSString* name;
@end
@implementation Person
- (instancetype)initWithName:(NSString *)name{
self = [super init];
if(self){
_name = name;
}
return self;
}
+ (instancetype)personWithName:(NSString *)name{
Person* p = [[Person alloc]initWithName:name];
return p;
}
+ (void)sayHello{
// Member variables cannot be accessed in class methods
//_name = @"dancer";
// If the implementation logic of a method , Don't pay attention to the difference of details , It can be designed as a type method
NSLog(@"Hello world");
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person* p1 = [Person personWithName:@"dancer"];
// The feedback results are the same , You can use type methods
[Person sayHello];
}
return 0;
}
边栏推荐
- 【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
- Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
- JSON string conversion in unity
- System integration meets the three business needs of enterprises
- [PaddleSeg 源码阅读] PaddleSeg计算 mIoU
- Value transfer communication between components (parent to child, child to parent, brother component to value)
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Don't disagree, this is the most powerful "language" of the Internet
- Leecode 122. Zuijia timing of buying and selling stocks ②
- What is the difference between enterprise wechat applet and wechat applet
猜你喜欢

How to use websocket to realize simple chat function in C #

National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?

微信公众号网页授权

Rhcsa day 2

Want to do something in production? Then try these redis commands
![Backpropagation formula derivation [Li Hongyi deep learning version]](/img/ef/f76eae39c4f8716a0030a60c85b09c.gif)
Backpropagation formula derivation [Li Hongyi deep learning version]

Development of digital collection trading platform development of digital collection platform

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

Easy to win insert sort

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
随机推荐
Redis notes (I) Linux installation process of redis
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
Unity controls the selection of the previous and next characters
Site favorites
Which product is better if you want to go abroad to insure Xinguan?
Don't disagree, this is the most powerful "language" of the Internet
Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
Handler source code analysis
The difference between MCU serial communication and parallel communication and the understanding of UART
Baijia forum the founding of the Eastern Han Dynasty
1day vulnerability pushback skills practice (3)
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
Record a problem that soft deletion fails due to warehouse level error
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
warning: LF will be replaced by CRLF in XXXXXX
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
JS object definition
Jenkins configures IP address access
PMP 考試常見工具與技術點總結