3

Basically I'm trying to do this in Swift

// get the Detail view controller in our UISplitViewController (nil if not in one)
id detail = self.splitViewController.viewControllers[1];
// if Detail is a UINavigationController, look at its root view controller to find it
if ([detail isKindOfClass:[UINavigationController class]]) {
    detail = [((UINavigationController *)detail).viewControllers firstObject];
}

I've got as far as this;

var detail : AnyObject = self.splitViewController.viewControllers[1]

    if detail.isKindOfClass(UINavigationController) {
        detail = ((detail: UINavigationController).detail).

but I can't find what to do after this.

Another separate quick question. Is it considered good practice to have a lot of statements ending in as [type]. It's mainly resulting from the use of AnyObject's, like using valueForKeyPath for instance. It just seems a bit messy having it all over my code

Sawyer05
  • 1,604
  • 2
  • 22
  • 37

1 Answers1

3

Here's a way to do that in Swift using Optional Binding:

// get the Detail view controller in our UISplitViewController (nil if not in one)
var detail = self.splitViewController.viewControllers[1];
// if Detail is a UINavigationController, look at its root view controller to find it
if let nav = detail as? UINavigationController {
    detail = nav.viewControllers[0];
}

As for your question, yes its quite common to use as type all over the place in Swift when using ObjC APIs. That's a by-product of going from ObjC to the strongly-typed Swift language, it should get better when more libraries are written in Swift and less ObjC is used!

Jack
  • 16,677
  • 8
  • 47
  • 51
  • Thats perfect! Just drives my code OCD up the wall a bit! – Sawyer05 Jun 29 '14 at 20:51
  • @Sawyer05 I think it actually makes code more readable and cleaner tho. Currently I'm really enjoying type inference in Swift and my OCD now kicks in when looking at ObjC more so than Swift :p – Jack Jun 29 '14 at 21:01
  • I think I just like the code to be short and sweet. problem #100000 - I'm trying to link a refresh control to an IBAction function but it's only giving me the option to make an Outlet for it – Sawyer05 Jun 29 '14 at 21:30
  • @Sawyer05 It would be best if you created a new question for that. Try right clicking on the control and dragging the action to the View Controller object in IB – Jack Jun 30 '14 at 00:54