当前位置:网站首页>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;
}
边栏推荐
- I stepped on a foundation pit today
- 機器學習基礎:用 Lasso 做特征選擇
- Add token validation in swagger
- Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
- Code Execution Vulnerability - no alphanumeric rce create_ function()
- How to use STR function of C language
- Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
- Development of digital collection trading platform development of digital collection platform
- 查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
猜你喜欢

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

Summary of Chinese remainder theorem

Code Execution Vulnerability - no alphanumeric rce create_ function()

Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami

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

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex

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

(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)

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

Setting methods, usage methods and common usage scenarios of environment variables in postman
随机推荐
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
static hostname; transient hostname; pretty hostname
96% of the collected traffic is prevented by bubble mart of cloud hosting
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
JVM family -- heap analysis
Handler source code analysis
MySQL is dirty
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?
Rhcsa day 2
Unity controls the selection of the previous and next characters
Want to do something in production? Then try these redis commands
CSCI 2134
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
Easy to win insert sort
Practical multifunctional toolbox wechat applet source code / support traffic master
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
Setting methods, usage methods and common usage scenarios of environment variables in postman
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
深入浅出对话系统——使用Transformer进行文本分类