Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Use compact instead of flatMap and children instead of childViewControllers
Source Link
Sentry.co
  • 5.7k
  • 47
  • 41

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 45.1)

/** * - Returns: ViewController of a class Kind * ## Examples:  * UIView.vc(vcKind: CustomViewController.self) // ref to an instance of CustomViewController */  public static func vc<T: UIViewController>(vcKind: T.Type? = nil) -> T? {   guard let appDelegate:AppDelegate = UIApplication.shared.delegate, as?let AppDelegatewindow = appDelegate.window else { return nil }   if let vc = appDelegate.window?.rootViewController as? T {   return vc   } else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T {   return vc   } else if let vc = appDelegate.window?.rootViewController?.childViewControllers children {   return vc.lazy.flatMapcompactMap { $0 as? T }.first   }   return nil  } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

/** * - Returns: ViewController of a class Kind * ## Examples: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let vc = appDelegate.window?.rootViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.childViewControllers  { return vc.lazy.flatMap{$0 as? T}.first } return nil } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 5.1)

/** * - Returns: ViewController of a class Kind * ## Examples:  * UIView.vc(vcKind: CustomViewController.self) // ref to an instance of CustomViewController */  public static func vc<T: UIViewController>(vcKind: T.Type? = nil) -> T? {   guard let appDelegate = UIApplication.shared.delegate, let window = appDelegate.window else { return nil }   if let vc = window?.rootViewController as? T {   return vc   } else if let vc = window?.rootViewController?.presentedViewController as? T {   return vc   } else if let vc = window?.rootViewController?.children {   return vc.lazy.compactMap { $0 as? T }.first   }   return nil  } 
Swiftify doc
Source Link
Sentry.co
  • 5.7k
  • 47
  • 41

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

/** * Returns- aReturns: ViewController of a class Kind * EXAMPLE## Examples: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let vc = appDelegate.window?.rootViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.childViewControllers { return vc.lazy.flatMap{$0 as? T}.first } return nil } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

/** * Returns a ViewController of a class Kind * EXAMPLE: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let vc = appDelegate.window?.rootViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.childViewControllers { return vc.lazy.flatMap{$0 as? T}.first } return nil } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

/** * - Returns: ViewController of a class Kind * ## Examples: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let vc = appDelegate.window?.rootViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.childViewControllers { return vc.lazy.flatMap{$0 as? T}.first } return nil } 
added 14 characters in body
Source Link
Sentry.co
  • 5.7k
  • 47
  • 41

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

extension UIView{ /** * Returns a ViewController of a class Kind * EXAMPLE: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let viewControllervc = appDelegate.window?.rootViewController as? T {  return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return viewControllervc }else if let viewControllersvc = appDelegate.window?.rootViewController?.childViewControllers { return viewControllersvc.lazy.compactMapflatMap{$0 as? T}.first } return nil } } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

extension UIView{ /** * Returns a ViewController of a class Kind * EXAMPLE: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let viewController = appDelegate.window?.rootViewController?.presentedViewController as? T { return viewController }else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers { return viewControllers.lazy.compactMap{$0 as? T}.first } return nil } } 

Here is an extension I made on the basis of prev answers

Uses Generics + Extension (🔸Swift 4.1)

/** * Returns a ViewController of a class Kind * EXAMPLE: UIView.vc(vcKind: CustomViewController.self)//ref to an instance of CustomViewController */ public static func vc<T:UIViewController>(vcKind:T.Type? = nil) -> T?{ guard let appDelegate:AppDelegate = UIApplication.shared.delegate as? AppDelegate else {return nil} if let vc = appDelegate.window?.rootViewController as? T {  return vc }else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc }else if let vc = appDelegate.window?.rootViewController?.childViewControllers { return vc.lazy.flatMap{$0 as? T}.first } return nil } 
Source Link
Sentry.co
  • 5.7k
  • 47
  • 41
Loading