[背景]
想要把OC代码:
NSAssert(newViewControllers.count >= 2, "MHTabBarController requires at least two view controllers")
转换为swift代码,结果出错:
Use of unresolved identifier ‘NSAssert’
如图:
[解决过程]
1.搜:
swift Use of unresolved identifier NSAssert
swift NSAssert
参考:
Transitioning from Objective-C to Swift | b2cloud
去改为:
assert(newViewControllers.count >= 2, "MHTabBarController requires at least two view controllers")
即可通过编译。
[总结]
OC中,加了断言,当条件不满足时,编译期间会出错。但是当实际运行时,断言是没用的;
-》换言之:当编译为实际运行的app时,NSAssert都被去掉了。
而Swift中,其实也是类似的:
NSAssert被换为assert,也只是编译期间有用,运行时也已经被去掉了;
而OC中的NSAssert,Swift中改为了assert
并且,字符串之前加的@符号,也不需要了。
转载请注明:在路上 » [已解决]Swift代码出错:Use of unresolved identifier NSAssert