当前位置:网站首页>[sel object of Objective-C language]
[sel object of Objective-C language]
2022-07-28 00:10:00 【Breezy morning】
Objective-C Linguistic SEL object
One 、SEL What is the object ?
SEL Objects are used to store methods , It includes the following attributes :
1. Class method or object method ,BOOL type ;
2. return type ,NSString * type ;
3. Method name ,NSString * type ;
4. Method parameter ,NSString * type ;
5. Method body ,NSString * type ;
Two 、 How to store methods ?
1. So let's create one SEL object
2. Let's put this SEL Object as Class object ( Class object ) Properties of
Be careful ,Class object ( Class object ) Is stored in code snippets , It is used to store a class , It has the following properties :
1. Class name ,NSString * type ;
2. Attribute array ,NSString * An array type ;
3. Methods the array ,SEL An array type ;
3. How to get the storage method SEL object
1. To get this SEL object , You should use one first SEL Take the pointer ?
SEL c1 =
2. Why? c1 No addition *, because SEL This data type is a custom data type , It's a typedef, It is defined as follows :
typedef struct objc_selector *SEL;
3. Because the pointer type of this structure is defined as SEL When these three words , Has added * 了 , So the statement SEL Objects are not added * 了 .
4. How to get storage - (void)sayHi; Methodical SEL What about objects? ?
1). One @ Symbol , Pick up a selector, Followed by a parenthesis , Write the name of the method in this parenthesis sayHi that will do ;
As shown below :
SEL c1 = @selector(sayHi);
after ,c1 Just point to the storage sayHi Methodical SEL object ;
It can be used %p hold c1 Output the value of :
NSLog(@" c1 = %p ",c1);
The output looks like this :
c1 = 0x100003f71
5. The essence of calling methods :
1)[p1 sayHi]; The internal principle :
a. Get the storage first sayHi Methodical SEL object ;
That is to get the storage first sayHi Address of method , That is to take
To the storage sayHi Methodical SEL data , Or someone also put
This becomes SEL news .
b. Put this SEL Message sent to p1 object .
c.p1 Object received this SEL After message , I knew it was to
Calling method .
d. Based on the iSA The pointer , Find the class object that stores the class , Then look in the class object , Do you have this SEL data , If there is , Is executed , without , Just find the parent class , Always find NSObject class , If not , Just report a mistake ;
6. Manually send... To the object SEL news :
1) Get the storage first sayHi Methodical SEL object , namely SEL news ;
SEL s1 = @selector(sayHi);
2) Put this SEL news , Send to p1 object ;
p1 The object has a way , be called performSelector:(SEL)
You can put SEL Message sent to p1 object :
[p1 performSelector:s1];
7. If SEL What if the object has parameters ?
such as , There's a way :- (void)eatWithFood:(NSString *)foodName;
- (void)eatWithFood:(NSString *)foodName
{
NSLog(@" You gave me %@, Really delicious ...);
}
How to manually give objects p1 send out SEL news ?
1)SEL c1 = @selector(eatWithFood:);
[p1 performSelector:c1];
That's not gonna work , Because you have to pass it parameters !
2) How to give SEL Object parameters ?
Use p1 Object method of object :
performSelector:(SEL) withObject:(id);
for example :
[p1 performSelector:c1 withObject:@“ Braised Pork ”];
8. If SEL Object with two parameters ?
call p1 Another object method of the object :
[p1 performSelector:(SEL) withObject:(id) withObject:(id)];
9. If SEL What if the object has multiple parameters ?
You can only encapsulate multiple parameters in one object , Pass to SEL object :
1) Write a class first :Params
2)Params The attribute of is all parameters , For example, there are four parameters ,NSString *_p1;NSString *_p2;NSString *_p3;NSString *_p4;
for example :
@interface Params:NSObject
{
@public
NSString *_p1;
NSString *_p2;
NSString *_p3;
NSString *_p4;
}
@end
3)- (void)testWith:(Params *)ps
{
NSLog(@“p1 = %@,p2=%@,p3=%@,p4=%@”,ps->_p1,ps->_p2,ps->_p3,ps->_p4);
}
3) Create a Params object :
Params *ps = [Params new];
ps->_p1 = @“p1”;
ps->_p2 = @“p2”;
ps->_p3 = @“p3”;
ps->_p4 = @“p4”;
4) Get SEL object :
SEL c4 = @selector(testWith:);
5) hold SEL Object is sent to p1 object :
[p1 performSelector:c4 withObject:ps];
summary
1. Call an object's method , There are two kinds of :
1)[p1 sayHi];
2)SEL c1 = @selector(sayHi);
[p1 performSelector:c1];
边栏推荐
- BUUCTF-bbbbbbrsa
- [NPUCTF2020]EzRSA
- How to deal with the website after it is hacked and how to delete batch malicious code
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
- [ACTF新生赛2020]crypto-aes
- (十二)51单片机----用DS18B20浅测一下工(江)西的室外温度
- Master data management theory and Practice
- Unity 实现简单画板画画功能(笔记)
- Common errors reported by ant sword
- [development tutorial 11] crazy shell arm function mobile phone timer experimental tutorial
猜你喜欢

org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.

Put cloudflare on the website (take Tencent cloud as an example)

【zer0pts CTF 2022】 Anti-Fermat

物联网有助于应对气候变化的 3 种方式

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

好漂亮的彩虹

泵站远程监控

Notes on Shangpin project in shangsilicon Valley (I)
![[极客大挑战 2019]RCE ME](/img/ff/aff58f40f2051f7415d1e16517f824.png)
[极客大挑战 2019]RCE ME

How Flink uses savepoint
随机推荐
Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
Explain the idempotence of distributed system in detail
BUUCTF-RSA roll
org.junit.runners.model. InvalidTestClassError: Invalid test class ‘com.zhj.esdemo. MysqlTests‘: 1.
Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)
阿金的思考
MQTT----mqtt.fx客户端软件
Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
窗口函数over
Master data management theory and Practice
The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
BUUCTF-RSA roll
基于mediapipe的姿态识别和简单行为识别
Use of date
Why does redis cluster use reverse proxy? Just read this one
Binary conversion method
基于原生js实现今日新闻网站
JS ATM机输出
[C language] address book (dynamic version)
数据中台的那些“经验与陷阱”