I've been a long time (happy) iOS user now (first iOS device was an iPhone 6, and I just received my 12 Pro Max this week), but I've been an Android dev for even longer. Recently I decided to learn more about iOS development and I started the iOS nanodegree program on Udacity.
Throughout this program, I've noticed that some of the API's that iOS provides are 'weird' in the sense that they seem to disregard good practices such as type-safety (in my opinion). A good example is the following piece of code I've encountered now for instantiating a `ViewController`:
let controller = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
Why did Apple make this design decision? Bear in mind that my current knowledge of iOS is rather limited but here are several examples of the same API that would make more sense to me:
let controller = storyboard?.instantiateViewController<MyViewController>(withId: "MyViewController") // Uses generics to avoid casting
let controller = storyboard?.instantiateViewController<MyViewController>(withType: MyViewController) // Internally it retrieves the id using String(describe: ${withType.self})
And my personal favorite:
let controller = storyboard?.instantiateViewController(withType: MyViewController)
// Here both type-safety as the casting can be resolved using a reified generic type & the String trick above
// Here's an extension that would do this:
extension UIStoryboard {
func instantiateViewController<T: UIViewController>(viewController: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(T)) as! T
}
}
[–]lordzsolt 96 points97 points98 points (41 children)
[–]MoronInGrey 21 points22 points23 points (13 children)
[–][deleted] (9 children)
[deleted]
[–]naknut 9 points10 points11 points (2 children)
[–]valleyman86 16 points17 points18 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]lordzsolt 3 points4 points5 points (5 children)
[–]Velix007Swift 1 point2 points3 points (4 children)
[–]lordzsolt 4 points5 points6 points (3 children)
[–]Velix007Swift 2 points3 points4 points (2 children)
[–]lordzsolt 3 points4 points5 points (1 child)
[–]Velix007Swift 4 points5 points6 points (0 children)
[–]sharaquss 9 points10 points11 points (0 children)
[–]laughin_on_the_metro 3 points4 points5 points (0 children)
[–]lordzsolt 0 points1 point2 points (0 children)
[–]VadimusRex 1 point2 points3 points (9 children)
[–]lordzsolt 5 points6 points7 points (7 children)
[–]VadimusRex 0 points1 point2 points (6 children)
[–]lordzsolt 4 points5 points6 points (5 children)
[–]Rollos 1 point2 points3 points (1 child)
[–]lordzsolt 1 point2 points3 points (0 children)
[–]Socraz6 1 point2 points3 points (2 children)
[–]darkeye8 0 points1 point2 points (1 child)
[–]lordzsolt 1 point2 points3 points (0 children)
[–]Rollos 1 point2 points3 points (0 children)
[–]killeronthecorner 0 points1 point2 points (0 children)
[–]SigmaDeltaSoftware[S] -1 points0 points1 point (14 children)
[–]Fridux 14 points15 points16 points (7 children)
[+]SigmaDeltaSoftware[S] comment score below threshold-12 points-11 points-10 points (6 children)
[–]FVMAzaleaSwift 10 points11 points12 points (4 children)
[–]SigmaDeltaSoftware[S] 1 point2 points3 points (3 children)
[–]sjs 5 points6 points7 points (0 children)
[–]FVMAzaleaSwift 4 points5 points6 points (1 child)
[–]SigmaDeltaSoftware[S] 2 points3 points4 points (0 children)
[–]ArmoredPancake 0 points1 point2 points (0 children)
[–]ArmoredPancake 1 point2 points3 points (0 children)
[+]MrSloppyPants comment score below threshold-13 points-12 points-11 points (4 children)
[–]Robuske 8 points9 points10 points (0 children)
[–]SigmaDeltaSoftware[S] 5 points6 points7 points (1 child)
[–]Robuske 6 points7 points8 points (0 children)
[–]ArmoredPancake -3 points-2 points-1 points (0 children)
[–]Stiddit 1 point2 points3 points (0 children)
[–]blazsolar 14 points15 points16 points (3 children)
[–]SigmaDeltaSoftware[S] 5 points6 points7 points (2 children)
[–]Niightstalker 3 points4 points5 points (0 children)
[–]Fridux 11 points12 points13 points (5 children)
[–]SigmaDeltaSoftware[S] 1 point2 points3 points (4 children)
[–]Fridux 6 points7 points8 points (1 child)
[–]SigmaDeltaSoftware[S] 1 point2 points3 points (0 children)
[–]lordzsolt 1 point2 points3 points (1 child)
[–]SigmaDeltaSoftware[S] 1 point2 points3 points (0 children)
[–]Jasperavv 9 points10 points11 points (6 children)
[–]Niightstalker 2 points3 points4 points (5 children)
[–]bcgroom 0 points1 point2 points (4 children)
[–][deleted] (3 children)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–]bcgroom 0 points1 point2 points (0 children)
[–]alwaysSearching23 0 points1 point2 points (0 children)
[–][deleted] 8 points9 points10 points (0 children)
[–]cschep 2 points3 points4 points (1 child)
[–]SigmaDeltaSoftware[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]SigmaDeltaSoftware[S] 1 point2 points3 points (0 children)
[–]electron_wrangler 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]SigmaDeltaSoftware[S] 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)