我有那个在Obj-C中工作的代码,但是无法在NSClassFromString函数中找到在swift中使用Obj-C的方法:
NSArray *viewControllers = [[self navigationController] viewControllers];
BOOL viewExists = NO;
id obj;
for (int i = 0; i < [viewControllers count]; i ++)
{
obj = [viewControllers objectAtIndex:i];
if ([obj isKindOfClass:NSClassFromString(@"myViewController")])
{
if ([[obj valueForKey:@"synchroRunning"] boolValue])
{
[self performSelectorOnMainThread:@selector(startSpinner) withObject:nil waitUntilDone:NO];
[synchroBtn setHidden:YES];
}
else
{
[self synchroClick:nil];
}
viewExists = YES;
}
}
如果我想使用它:
如果让vcObj = obj为? myViewController 通过导入#import&#34; myViewcontroller.h&#34;我有一条错误消息。在桥上课:
<unknown>:0: error: failed to emit precompiled header '/Users/OlostA/Library/Developer/Xcode/DerivedData/Formbox-fpnftywlyjuvvubjjzpknxxdyhul/Build/Intermediates.noindex/PrecompiledHeaders/Formbox-Bridging-Header-swift_7N984CYB20BK-clang_28VAG4OSP9DZS.pch' for bridging header '/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/Planning/Formbox-Bridging-Header.h'
提前致谢。
答案 0 :(得分:1)
假设此控制器中存在self.navigationController
,这是您快速版本的代码:
let viewControllers = self.navigationController!.viewControllers
var viewExists = false
for obj in viewControllers {
if let running = obj.value(forKey: "synchroRunning") as? Bool {
self.performSelector(onMainThread: #selector(startSpinner), with: nil, waitUntilDone: false)
} else {
self.synchroClick(nil)
}
viewExists = true
}
您不需要NSClassFromString
来查明类型是否为ViewController
,因此Swift中的类型数组保证为ViewController
类型。如果你希望它是空的,请务必在循环之前检查viewControllers.count > 0
。