UIColor attributes code error in the book — iOS 10 Programming for Beginners

I had a code error in the chapter 6 of the book “ iOS 10 Programming for Beginners”. The code is from part B of the section “Breaking Down our App Delegate”. Here is the code snippet

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:.darkGray], for:.normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: .black], for:.selected)

I reached to packt publishing about the code error. I got the response from the author. The author mentioned about XCODE having issue of seeing UIColor and suggested the adding UIColor to the colours. Here is the code snippet

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: UIControlState.normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.black], for: UIControlState.selected)

I tried the above code snippet. But, I got error in XCODE as the attributes are renamed in the new XCODE. I searched in stackoverflow and found this post https://stackoverflow.com/questions/46535574/fallbacks-for-renamed-properties-in-swift?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa useful. So I solved the issue by changing the NSForegroundColorAttributeName to NSAttributedStringKey.foregroundColor

After changing the attribute name to the new name solved the issue. Here is the code snippet

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.darkGray], for: UIControlState.normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.black], for: UIControlState.selected)

The credit goes to the user https://stackoverflow.com/users/1226963/rmaddy for solving this issue.

Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin