乐信圣文

乐信圣文

编程题:

1
在输入框中输入“10+2”,点击确定按钮,输出“10+2=12”  支持+-*/ 要求:考虑面向对象、扩展性,如又来了第五种运算%。40分钟

基础:

1.@property weak 和 assign 的区别,assign 什么情况下修饰对象,unsafe_unretain 的作用

2.category 和 extension 的区别,多个 category 实现了同名方法,执行哪个?

3.通知是同步的还是异步的?

4.内存泄漏都有哪些场景,除了循环引用还有别的吗?

5.Runloop 和线程的关系,Mode 的作用,为什么这么设计?除了隔离还有什么作用?Runloop 什么时候停止?

项目:

1.构建提效

2.私有方法调用

3.MVCS 和 MVC 的区别

4.子孙组件传值

另一位面试官:

原生和跨端的占比

最近做的项目

参考答案

编程题:

1
在输入框中输入“10+2”,点击确定按钮,输出“10+2=12”  支持+-*/ 要求:考虑面向对象、扩展性,如又来了第五种运算%。40分钟

使用策略模式和工厂模式

策略模式:将一系列算法或行为定义成一组可以相互替换的策略类,并让它们在同一个接口下独立实现,从而使得算法的选择可以在运行时决定。

(1)抽象策略 定义统一接口

(2)具体策略 实现某个特定的算法逻辑

(3)上下文 持有策略,并通过它来执行算法

  • 策略模式:侧重于 同一个对象可以有不同的行为,运行时可切换。
  • 工厂模式:侧重于 对象如何被创建,屏蔽创建的复杂性。

  • 运算的 加减乘除 = 不同的策略(策略模式)

  • 运算类由 工厂统一创建(工厂模式)
  • 所以我们用了 工厂 + 策略 的组合。
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#import <Foundation/Foundation.h>

/// 运算协议
@protocol OperatorProtocol <NSObject>
- (double)calculateWithLeft:(double)left right:(double)right;
@end

/// 加法
@interface AddOperator : NSObject <OperatorProtocol>
@end
@implementation AddOperator
- (double)calculateWithLeft:(double)left right:(double)right {
return left + right;
}
@end

/// 减法
@interface SubOperator : NSObject <OperatorProtocol>
@end
@implementation SubOperator
- (double)calculateWithLeft:(double)left right:(double)right {
return left - right;
}
@end

/// 乘法
@interface MulOperator : NSObject <OperatorProtocol>
@end
@implementation MulOperator
- (double)calculateWithLeft:(double)left right:(double)right {
return left * right;
}
@end

/// 除法
@interface DivOperator : NSObject <OperatorProtocol>
@end
@implementation DivOperator
- (double)calculateWithLeft:(double)left right:(double)right {
if (right == 0) {
NSLog(@"除数不能为0");
return 0;
}
return left / right;
}
@end

/// 取模
@interface ModOperator : NSObject <OperatorProtocol>
@end
@implementation ModOperator
- (double)calculateWithLeft:(double)left right:(double)right {
return fmod(left, right);
}
@end

/// 工厂类
@interface OperatorFactory : NSObject
+ (id<OperatorProtocol>)operatorForSymbol:(NSString *)symbol;
@end

@implementation OperatorFactory
+ (id<OperatorProtocol>)operatorForSymbol:(NSString *)symbol {
static NSDictionary *map;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
map = @{
@"+": [AddOperator new],
@"-": [SubOperator new],
@"*": [MulOperator new],
@"/": [DivOperator new],
@"%": [ModOperator new]
};
});
return map[symbol];
}
@end

/// 主控制器逻辑
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 假设输入框的字符串
NSString *input = @"10+2";

// 简单解析:找到第一个运算符位置
NSCharacterSet *operators = [NSCharacterSet characterSetWithCharactersInString:@"+-*/%"];
NSRange range = [input rangeOfCharacterFromSet:operators];

if (range.location != NSNotFound) {
NSString *leftStr = [input substringToIndex:range.location];
NSString *rightStr = [input substringFromIndex:range.location + 1];
NSString *symbol = [input substringWithRange:range];

double left = [leftStr doubleValue];
double right = [rightStr doubleValue];

id<OperatorProtocol> op = [OperatorFactory operatorForSymbol:symbol];
if (op) {
double result = [op calculateWithLeft:left right:right];
NSLog(@"%@=%g", input, result);
} else {
NSLog(@"不支持的运算符: %@", symbol);
}
}
}
return 0;
}

基础:

1.@property weak 和 assign 的区别,assign 什么情况下修饰对象,unsafe_unretain 的作用

2.category 和 extension 的区别,多个 category 实现了同名方法,执行哪个?

3.通知是同步的还是异步的

同步的

4.内存泄漏都有哪些场景,除了循环引用还有别的吗?

循环引用:block、NSTimer、delegate

非循环引用:非 OC 对象,如 CGImage 等需要手动调用 Release

5.Runloop 和线程的关系,Mode的作用,为什么这么设计?除了隔离还有什么作用?Runloop 什么时候停止

  • 主线程的 RunLoop:几乎不停止,除非 App 结束。
  • 子线程的 RunLoop:
    • 没有事件源就退出;
    • 或者你显式 CFRunLoopStop();
    • 或者超时返回;
    • 或者线程销毁。

项目:

1.构建提效,类数量 删除2200+头文件,删除未使用的类100+

2.私有方法调用

3.MVCS 和 MVC 的区别

4.子孙组件传值

Your browser is out-of-date!

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

×