Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kyle Robertson
31,600 PointsHow do I use a background image without force unwrapping it?
On a project I'm working on, I have an image in the assets catalog that loads fine if I force unwrap it, but when I use the pattern Pasan teaches in the optional binding video, xcode keeps telling me I need to add an exclamation mark to it.
if let backgroundImage = UIColor(patternImage: UIImage(named: "login_bg.png")) {
self.view.backgroundColor = backgroundImage
}
The only way I can get it to work is with this
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "login_bg.png")!)
Thanks for any input you could give!
Kyle
1 Answer
Chris Shaw
26,676 PointsHi Kyle,
The video you're most likely watching was for Xcode 6.1 and earlier, since then Swift has undergone a lot of core changes which include the requirement to unwrap objects before they can be used.
Hope that helps.
Kyle Robertson
31,600 PointsKyle Robertson
31,600 PointsThanks Chris!