-1

I have that code that works in Obj-C, but impossible to find the way to use a Obj-C in swift with the NSClassFromString function :

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;
        }
    }

If I want to use that:

if let vcObj = obj as? myViewController I have an error message by importing the #import "myViewcontroller.h" class in the bridge:

<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'

Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • Your question isn't clear. What is the Swift part of your question? Are you trying to translate this Objective-C code into Swift? If so, edit your question to include your attempted Swift code. – rmaddy Nov 07 '17 at 18:58
  • this might help https://stackoverflow.com/a/24808338/1570343 – Pranjal Bikash Das Nov 07 '17 at 19:01
  • Why do you need `NSClassFromString`? Why not simply `[ViewController class]`? Anyhow, this should translate to Swift quite nicely as `if let vcObj = obj as? ViewController`. – Losiowaty Nov 07 '17 at 19:30
  • `UINavigationController`'s `viewControllers` array will be typed as `[UIViewController]` in Swift, so there shouldn't be any need for type checking at all – Connor Neville Nov 07 '17 at 19:46
  • Losiowaty Because when I add the #import "ViewController.h" in the bridge, I have an error message ". I put thte message in the ticket – ΩlostA Nov 07 '17 at 20:59

1 Answers1

1

Assuming self.navigationController exists in this controller, here is your swift version of the code:

    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
    }

You do not need NSClassFromString to find out if type is of a ViewController, it a typed array in Swift therefore is guaranteed to be of type ViewController. Make sure to check viewControllers.count > 0 before looping over it if you expect it to be empty.

Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53
  • Hello I just added in my ticket that the ViewController, is the name of my class. I changed it to myViewController to be more understandable. – ΩlostA Nov 07 '17 at 20:57