Learn the power and drawbacks of literals in code.
- [Instructor] You've probably seen them in the auto complete Nexus, but have you ever used the image literals? Image literals pop images directly into code, without initializing the UI image. While easy, they do have their drawbacks. Let's take a look at porting code with image literals. You're most likely going to use an image literal in a Swift Playground for Ipad, so I popped one up here, and you can download this project from GitHub, and install the Swift Playground into an Ipad Playground. I've set up a simple photo background in ViewDidLoad, and we're going to add one missing line to this code, which is ImageView dot image equals and then instead of putting a UI image, you'll find at the bottom auto complete a small icon with a mountain and the sun.
That's the image literal button. Go ahead and tap that, and up comes some images. You can get rid of the keyboard, and you'll see them a little better, and I'm going to go ahead and pick the first one here, which is a fish dinner, and go ahead and hit Use, and you'll see the literal now as the image for image view image. Now go ahead and run the code, and up in the live view shows the fish.
I can go ahead and hit that literal again, click the other picture, which is an ocean scene, click Use, run that code, and I get an ocean scene. So that's this code in Ipad Playgrounds. Now you may want to go ahead and take that to a Playground on XCode. You can save this, and then open it up in XCode, and you'll see the same code as you've got now. I usually have this set to manually run over here, so I'm going to turn that back on, and go ahead and run this Playground, and if you open up the live view with the assistant editor, you'll see our ocean scene, just like we had before.
And there's our little image. Close up the assistant editor. Go over here, Backspace over the image literal, and type Fish, and you'll notice in the auto complete there is no Fish. That's because for XCode Playgrounds, you have to use UI image named, but this requires a string, so to get that string, if you hit Command + One, you'll get a navigator, and you'll see over here there's a folder called Resources, which holds those images in there, and you'll see there's one called Fish.
All I have to do in the UI image is make that Fish. I'll run this Playground, show the assistant editor, and there's my fish. So for an XCode Playground, you can't use an image literal, but you can in Ipad Playground, so let's add one more element to this, and that's XCode itself. And we're going to start a new file, New, XCode project.
We'll use the single view application, hit Next, and go ahead and name this Image Literals. Leave the team, organization name, organization identifier, all the same as you normally would have them, we'll use Swift for the language, and leave all the stuff on the bottom unchecked. Go ahead and hit Next. I'm going to save it to the desktop, and there ya go. We're not going to need any attributes here, so I'm going to shut that off right away, and head over to the view controller, and I'm going to replace this view controller with the view controller I had from my Playground, so I'll go back over to Window, pull up the Playground, copy this view controller, Window, go to the original XCode one, and paste that into place.
Alright, with that done, we can go ahead and run this, and I'm going to set this to a simulator for Iphone 8 plus, run this, and you see nothing. Well that's because you don't have any images yet. So go ahead and stop this. Go back, Window, Image Literals, to the fish and ocean, okay.
I'm going to go to Resources here, and I'm going to show in Finder, so I can have those two open, and in Resources, there's our fish and ocean. Now that I have that into place, I'm going to go back to XCode, and copy them into my Assets folder. They have to be in the Assets folder in order to use the image literals. So I can go to Window, Image Literals, Assets, pull up my finder, throw the fish in there, throw the ocean in there.
Okay, now I've got the fish and the ocean. Go ahead and run. And there's my fish. Stop the simulator, and I'm going to go ahead and change that to ocean now. Now here you can actually use image literals, OC, and you can just see even with OC, it already tells me ocean, and there it is, I can just click it here, and then run again, and you can see it's got the nice little image in there.
And the ocean appears. Now image literals are very convenient, but they are inconsistent in use, particularly with the XCode Playgrounds. When porting a project from an Ipad Playground, know where the resource folder is, and the resources you used, so you can convert your code to UI image named for XCode Playgrounds. When working with an XCode project, feel free to use any image you put into the Asset folder. Just remember to move the Asset folder when moving code around.
Author
Updated
4/20/2021Released
1/23/2018Note: Because this is an ongoing series, viewers will not receive a certificate of completion.
Skill Level Beginner
Duration
Views
Related Courses
-
Swift: Delegations and Data Sources
with Steven Lipton1h 6m Intermediate
-
New This Week:
-
What is Hashable?6m 11s
-
-
Introduction
-
Welcome1m 10s
-
-
iOS Development Tips Weekly
-
Use Xcode Git repositories6m 59s
-
Read and write text files6m 47s
-
Use the Xcode 9 simulators4m 24s
-
Layout and iPhone X5m 35s
-
Read JSON files7m 59s
-
Make a camera on iPad7m 50s
-
Motion effects7m 27s
-
Tricks with Asset Manager6m 40s
-
Array tricks10m 56s
-
Image and color literals6m 48s
-
Why use uint8?10m 14s
-
Using hex color9m 39s
-
Deep dive into dictionaries9m 11s
-
Tapping a map7m 3s
-
Update old apps3m 47s
-
Using Apple Pencil8m 42s
-
Using property lists7m 44s
-
Using popovers8m 23s
-
Attributed string jazz9m 18s
-
3D text with SceneKit5m 54s
-
UIColor class extensions5m 46s
-
Timer accuracy6m 25s
-
Animate maps5m 17s
-
Xcode 10 storyboard changes7m 46s
-
Use code snippets8m 40s
-
Customizing the tab bar8m 31s
-
The Settings bundle9m 26s
-
Use the speech synthesizer6m 45s
-
Make round buttons7m 45s
-
Embed table views6m 36s
-
Make a toggle button7m 37s
-
Drop shadows6m 24s
-
Blur and vibrancy effects8m 48s
-
Views in playgrounds6m 52s
-
Launch the Settings app4m 45s
-
Draw paths in UIViews8m 4s
-
CALayer masks7m 14s
-
Protocols and time pickers6m 38s
-
Sliders as knobs7m 36s
-
Fix stackview disasters8m 9s
-
Using haptics4m 57s
-
Test flight9m 8s
-
Replacing segmented controls9m 50s
-
Ranges6m 48s
-
Unicode characters8m 36s
-
Thrown errors9m 41s
-
Actions in table views7m 5s
-
Make and use closures7m 4s
-
Using ARC6m 20s
-
Get BASIC with swift strings7m 56s
-
Convert CSV files10m 7s
-
Strings to doubles8m 14s
-
Enums13m 4s
-
Selection in text views5m 30s
-
Generate QR codes5m 13s
-
String interpolation6m 59s
-
Xcode changes5m 52s
-
Use Dark Mode on storyboards4m 45s
-
Use SF Symbols8m 42s
-
Structs6m 18s
-
Quick SwiftUI8m 55s
-
Working with sets7m 28s
-
Segue Actions4m 38s
-
Checkboxes in SwiftUI6m 15s
-
To-do lists in SwiftUI4m 27s
-
Quick Help in SwiftUI6m 29s
-
Models in SwiftUI6m 38s
-
SwiftUI in Xcode Playgrounds6m 39s
-
Basic PencilKit5m 45s
-
Setter and Getters5m 15s
-
UITextViews and coordinators6m 22s
-
Dark Mode in code6m 1s
-
Candle effects7m 53s
-
RealityKit5m 44s
-
NumberFormatter6m 7s
-
Tuples4m 42s
-
Protocols6m 16s
-
Property wrappers5m 19s
-
iPad to macOS6m 11s
-
Type properties5m 11s
-
SwiftUI animation8m 24s
-
@Binding in public6m 38s
-
SwiftUI ScrollView5m 30s
-
MapKit points of interest6m 14s
-
SF Symbols configuration5m 44s
-
Custom SF Symbols8m 7s
-
Use SwiftUI GeometryReader4m 15s
-
Proportional SwiftUI views8m 22s
-
Custom modifiers in SwiftUI6m 22s
-
Dependency injection6m 28s
-
Xcode themes7m 16s
-
Basic generics5m 58s
-
Selections in watchOS7m 23s
-
SwiftUI borders and shapes8m 54s
-
SwiftUI gradients11m 13s
-
Special parameters8m 49s
-
Overriding operators8m 29s
-
Basic measurements9m 26s
-
Key paths7m 9s
-
Recursive structs8m 38s
-
Outline views in SwiftUI5m 42s
-
TextEditor and Scribble4m 51s
-
Color pickers8m 54s
-
Changes to enums in Swift6m 15s
-
Date pickers6m 54s
-
SwiftUI labels3m 26s
-
SwiftUI maps6m 5s
-
Collection views for SwiftUI3m 50s
-
Scribble bugs4m 48s
-
The joys of UUID5m 8s
-
SwiftUI swipe gesture4m 2s
-
Holiday special: Let it snow6m 17s
-
Embed images in SwiftUI text5m 44s
-
UICollectionView lists11m 24s
-
UICollectionView layout9m 41s
-
Menus on UIButtons10m 27s
-
Multicolor symbols7m 33s
-
Multiplatform Swift UI8m 10s
-
Documents in SwiftUI8m 51s
-
Photos picker7m 51s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Image and color literals