当前位置:网站首页>[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];
边栏推荐
- [actf freshmen 2020] crypto AES
- J9 Digital Science Popularization: how does the double consensus of Sui network work?
- Unity 实现简单画板画画功能(笔记)
- Why does redis cluster use reverse proxy? Just read this one
- 给网站套上Cloudflare(以腾讯云为例)
- [December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
- BUUCTF-RSA roll
- 29. Learn the stacked column chart of highcharts using percentage
- The txt file named according to the sequence number is renamed from the back to the front
- 【C语言】字符串逆序(递归实现)
猜你喜欢

TCP的粘包拆包问题+解决方案

Bank marketing predicts the success rate of a customer's purchase of financial products
![[NCTF2019]babyRSA1](/img/c1/52e79b6e40390374d48783725311ba.gif)
[NCTF2019]babyRSA1

What is the prospect of low code development? Are you really optimistic about low code development?

29. Learn the stacked column chart of highcharts using percentage
![[Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring](/img/cc/91ec9f2c2cd5d5bddd157e61191772.png)
[Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring

荣耀多款产品齐发,笔记本MagicBook V 14售价6199元起

【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)

Construction and application of super large scale knowledge map of ants

尚硅谷尚品项目汇笔记(一)
随机推荐
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
BUUCTF-RSA4
If we were the developer responsible for repairing the collapse of station B that night
[GWCTF 2019]BabyRSA1
UE4官方AEC蓝图案例课程学习笔记
Assertion mechanism in test class
Put cloudflare on the website (take Tencent cloud as an example)
JUC toolkit learning
How to use C WinForm to copy files and display progress
The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测
[RoarCTF2019]babyRSA威尔逊定理
(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West
The interviewer asked the thread safe list, and I'm not afraid after reading it!
Legendary Internet Setup tutorial with graphic explanation - GOM engine
Xss.haozi.me practice customs clearance
物联网有助于应对气候变化的 3 种方式
BUUCTF-RSA roll
CPU的控制方式