一点资讯

一点资讯

1.用过 SwiftUI 吗?

2.组件化

3.内存管理?

(1)TaggedPointer 是值类型还是指针类型? Tag 和值是怎么区分的?

(2)union 共用体里还有哪些?

(3)autorelease 和 Runloop 是怎么协作的?

4.[UIView a]; 是否能调用成功?

1
2
3
4
5
6
7
8
9
10
11
12
@interface NSObject (Ext)

- (void)a;

@end
@implementation NSObject (Ext)

- (void)a {
NSLog(@"aaaa");
}

@end

5.问是否能打印 aaaa。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@interface A : NSObject

- (void)a;

@end
@implementation A

- (void)a {
NSLog(@"aaaa");
}

@end

@interface B : A

- (void)a;

@end

@implementation B

- (void)a {
[super performSelector:@selector(a)];
}

@end

B *b = [[B alloc] init];
[b a];

6.Swift
(1)defer 关键字的作用
(2)gard else的作用

7.接触过 AI 吗?

参考答案

1.用过 SwiftUI 吗?

2.组件化

3.内存管理?

(1)TaggedPointer 是值类型还是指针类型? Tag 和值是怎么区分的?

Tagged Pointer 的“指针”其实是 一个经过编码的值(Value Encoding)

但在 类型系统层面(Objective-C / Swift Runtime)它仍然是一个 指针类型(id)

TaggedPointer 的结构(以 64 位为例)

位段 含义
[63] 是否为 Tagged Pointer 的标志位(通常为 1)
[62..60] tag 值,用于区分类型(NSNumber、NSDate、NSString等)
[59..0] 存储的实际数据(value bits)

Tag 与 Value 如何区分

关键就在 最高位(bit 63):

  • 普通对象:指针最高位为 0,表示它是一个真正的内存地址。
  • Tagged Pointer: 最高位为 1,表示它不是地址,而是编码值。

(2)union 共用体里还有哪些?

(3)autorelease 和 Runloop 是怎么协作的?

autorelease 延迟对象释放,RunLoop 决定释放的时机。

4.[UIView a]; 是否能调用成功?

不能

5.问是否能打印 aaaa。

[super performSelector:@selector(a)] 实际上是先用 performSelector: 发送消息给 self,也就是当前对象 b,然后 runtime 会根据 self 的类 查找实现。

  • 这里 self 的类是 B。
  • 所以它会找到 B 自己的 -a 方法,再次执行 [super performSelector:@selector(a)]。

不会打印 aaaa,而是导致无限递归崩溃。

6.Swift
(1)defer 关键字的作用
(2)gard else的作用

7.接触过 AI 吗?

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×