1、把 "www.zhidao.baidu.com" 这样的字符串改成 "com/baidu/zhidao/www"
此题目来自百度的面试题,博爱不才,用了几分钟整理了答案,如果有问题,请及时联系本人更改,多谢!
/**
题目:把 "www.zhidao.baidu.com" 这样的字符串改成 "com/baidu/zhidao/www"
*/
- (void)test1
{
NSString *string1 = @"www.zhidao.baidu.com";
// 1、根据小数点 '.' 拆分字符串成数组
NSArray *stringArray = [string1 componentsSeparatedByString:@"."];
NSLog(@"拆分结果:%@", stringArray);
// 2、数组倒叙排列
NSArray *array = [[stringArray reverseObjectEnumerator] allObjects];
NSLog(@"拆分结果2:%@", array);
// 3、遍历倒叙后的数组,拼接 '/'
__block NSString *string2 = @"";
[array enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *str = obj;
// 4、判断最后一位时,不要拼接 '/'
if (idx != array.count-1)
{
str = [obj stringByAppendingString:@"/"];
}
string2 = [string2 stringByAppendingString:str];
NSLog(@"结果1:%@", string2);
}];
NSLog(@"最后结果:%@", string2);
// 最后结果:com/baidu/zhidao/www
}
// 简单写法,感谢 群里 @哈尔滨-吴迪 大神提供的方案!
- (void)test2
{
NSString *str = @"www.zhidao.baidu.com";
NSArray *array = [str componentsSeparatedByString:@"."];
NSArray *resultArr = [[array reverseObjectEnumerator] allObjects];
NSString *resultStr = [resultArr componentsJoinedByString:@"/"];
NSLog(@"最后结果:%@", resultStr);
}
// swift 写法
func test() -> Void {
let string1 = "www.zhidao.baidu.com"
let array = string1.components(separatedBy: ".")
let resultArray = array.reversed().joined(separator: "/")
print("结果1:\(array)\n结果2:\(resultArray)");
// 结果1:["www", "zhidao", "baidu", "com"]
// 结果2:com/baidu/zhidao/www
}
2、如果页面 A 跳转到 页面 B,A 的 viewDidDisappear 方法和 B 的 viewDidAppear 方法哪个先调用?
ViewController 为一级界面,BViewController 和 CViewController为两个二级界面
// 我在两个 VC 里面都写了如下代码:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%@ == %s", NSStringFromClass([self class]), __func__);
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%@ == %s", NSStringFromClass([self class]), __func__);
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"%@ == %s", NSStringFromClass([self class]), __func__);
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"%@ == %s", NSStringFromClass([self class]), __func__);
}
注意:这里的跳转有两种情况
1、A push B
2017-10-26 16:38:44.748403+0800 test2[3452:1288708] ViewController == -[ViewController viewWillDisappear:]
2017-10-26 16:38:44.748551+0800 test2[3452:1288708] BViewController == -[BViewController viewWillAppear:]
2017-10-26 16:38:45.304422+0800 test2[3452:1288708] ViewController == -[ViewController viewDidDisappear:]
2017-10-26 16:38:45.304572+0800 test2[3452:1288708] BViewController == -[BViewController viewDidAppear:]
从打印结果先后顺序看:【A-->B-->A-->B】
1、调用 A 的 viewWillDisappear 方法,
2、调用 B 的 viewWillAppear 方法
3、调用 A 的 viewDidDisappear 方法
4、调用 B 的 viewDidDAppear 方法
2、A present C
2017-10-26 16:45:17.957380+0800 test2[3456:1291552] ViewController == -[ViewController viewWillDisappear:]
2017-10-26 16:45:17.957632+0800 test2[3456:1291552] CViewController == -[CViewController viewWillAppear:]
2017-10-26 16:45:18.471787+0800 test2[3456:1291552] CViewController == -[CViewController viewDidAppear:]
2017-10-26 16:45:18.472829+0800 test2[3456:1291552] ViewController == -[ViewController viewDidDisappear:]
从打印结果先后顺序看:【A-->C-->C-->A】
1、调用 A 的 viewWillDisappear 方法,
2、调用 C 的 viewWillAppear 方法
3、调用 C 的 viewDidAppear 方法
4、调用 A 的 viewDidDisappear 方法
所以,如果遇到此面试题,大家注意回答 跳转方式,不同的跳转方式执行顺序会不同!
3、使用 GCD 如何实现这个需求:A、B、C 三个任务并发,完成后执行任务 D
- (void)test
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"=== A ===");
});
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"=== B ===");
});
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"=== C ===");
});
dispatch_group_t group = dispatch_group_create();
// dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
// NSLog(@"=== 任务 D 完成 ===");
// NSLog(@"=== 任务 E 完成 ===");
// NSLog(@"=== 任务 F 完成 ===");
// });
// 这里执行三次
dispatch_queue_t aaa = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(aaa,^{
dispatch_group_leave(group);
NSLog(@"=== 任务 D 完成 ===");
});
dispatch_queue_t bbb = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(bbb,^{
dispatch_group_leave(group);
NSLog(@"=== 任务 E 完成 ===");
});
dispatch_queue_t ccc = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(ccc,^{
dispatch_group_leave(group);
NSLog(@"=== 任务 F 完成 ===");
});
// 三次结束
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"=== 任务 M 完成 ===");
});
}
// 打印结果:
2017-10-26 16:19:07.898792+0800 BAKit[3447:1281688] === 任务 D 完成 ===
2017-10-26 16:19:07.898871+0800 BAKit[3447:1281688] === 任务 E 完成 ===
2017-10-26 16:19:07.898900+0800 BAKit[3447:1281633] BAUIKitViewController:-[BAUIKitViewController ba_base_viewWillDisappear]
2017-10-26 16:19:07.898914+0800 BAKit[3447:1281688] === 任务 F 完成 ===
2017-10-26 16:19:07.899002+0800 BAKit[3447:1281633] BAKitVC_BAGridView:-[BAKitVC_BAGridView ba_base_viewWillAppear]
2017-10-26 16:19:07.949885+0800 BAKit[3447:1281633] === A ===
2017-10-26 16:19:07.949950+0800 BAKit[3447:1281633] === B ===
2017-10-26 16:19:07.949961+0800 BAKit[3447:1281633] === C ===
2017-10-26 16:19:07.949985+0800 BAKit[3447:1281633] === 任务 M 完成 ===