当前位置:网站首页>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;
}
边栏推荐
- PMP 考試常見工具與技術點總結
- 機器學習基礎:用 Lasso 做特征選擇
- What kind of experience is it when the Institute earns 20000 yuan a month!
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
- Webhook triggers Jenkins for sonar detection
- 2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
- Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
- 2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
- SQL injection (1) -- determine whether there are SQL injection vulnerabilities
- Leetcode51.n queen
猜你喜欢

What are the virtual machine software? What are their respective functions?

选择排序与冒泡排序模板

1day vulnerability pushback skills practice (3)

Have you entered the workplace since the first 00???

Rhcsa day 3

MySQL data query optimization -- data structure of index

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。

Add IDM to Google browser

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

logistic regression
随机推荐
Why is it recommended that technologists write blogs?
[untitled]
Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
JS object definition
MySQL query
If you have just joined a new company, don't be fired because of your mistakes
1day vulnerability pushback skills practice (3)
[database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
Record a problem that soft deletion fails due to warehouse level error
XSS prevention
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
JSON string conversion in unity
CSP drawing
Résumé des outils communs et des points techniques de l'examen PMP
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
Unity controls the selection of the previous and next characters
Package details_ Four access control characters_ Two details of protected