Hi Alex here from rebeloper.com and today I will talk about why Image Literals
are missing from Xcode. So without further ado let's dive into
the code!
Okay now if you open up an older project that has been created prior Xcode 10 and
you open it up in Xcode 10 you will see if you have images (UIImages) you will
see this default image instead of the actual image that has been added with
with Image Litterers. Now Image Literals are of course when you just type in the
name you just start typing in the name of the image and then you just
select the one that you want and then it hit return and it basically adds it into
the code with a little icon representing the actual image. Okay, now I will just
show you because basically Image Literals haven't been removed from Xcode 10
just the Auto completion has been removed. Now let me just create a new
project and show you what to do. So I just copy this and let's create a new
project go to file new and project and let it be a Single View Application.
Click on next and... let's say: ImageLiteralsAutoCompletionRemove because the auto completion
has been used removed. Okay a long product in but that's what it is. Okay and I will
just drop it onto the desktop. Okay let me just make this so you may see it
better. Now let's get go to our ViewController here and let's paste the code
that we have just copied from my previous
project. And you can see the default image is presented. Okay now how to fix
this well first of all we need to add our assets. Okay, so I will just drag and
drop this one and let me just see which one is it yeah it's the slowest. Okay, and
here we go slideshow slowest. Let's drag and drop it into our asset folder.
Great! There we go and if we just build this
project with command+B nothing really happens. Xcode
doesn't really get it that we have added that image. And if we just try to type in
iv.image = Slideshow you see that it doesn't really auto
complete and that is the feature that has been removed. Now the old way of
adding this is through the UIImage initialization. So UIImage and
we will choose with the one with the named and I would just add this
string here. Now this is kind of cumbersome and that is what Image
Literals basically sold this whole copy and pasting and it was very typo... well
it wasn't really typo proof what that's what I'm trying to say. And now we
do have it. But you know we have this SlideshowSlowest what is that really. We
would rather like to see the actual image a little thumbnail of that image
in our Xcode editor. Now that is what we are going to learn today. So if we just
comment out the first line you will see that actually basically the Image
Literal haven't been removed from Xcode and this is with the resource name
and of course with SlideshowSlowest. Now let's just comment out the old way of
doing it. And let's add the new way of doing it. So the Xcode 10 way.
So iv.image = and now we want to add our Image Literal.
Again typing in the name of the image that will not work as of Xcode10.
Now what we need to do is... and here's a little shortcut for our media library
because we want to bring up the media library that is pretty simple and
basically it's kind of the same and it's not that time consuming either. So
hit shift+command+m. Again shift+command+m and your a media library will pop up
and let me just take this here. It's a really nice to see it here. And of
course if you just simply tap away from your media library it will basically
disappear. Again shift+command+m and you will see. Now what you need to do is a
drag and drop it into your code and again what gives it still
is the basic old default thumbnail for this image. Now most
probably this is an issue with Xcode 10 maybe it will be fixed in later versions
but what I found out is that if you have more than one images in your assets
that this will work. So let us just and delete this for now... Not that one... this
default value here. And let's add another for SlideshowFastest. Okay let's go to
the assets folder and here we go SlideshowFastest there you go. Hit command+B
so everything compiles of course bit has failed because here we need to
drag our image. Okay hit shift+command+m again
and let's drag and drop the Slideshow. Okay most probably, if this doesn't
work for you then go ahead and it worked in other project of my own this is why I
think this is a bug that will be fixed sooner or later. Now what I found is
that if you double tap on it you will see what it actually is and you can
select from the media library. Now once you double tap you will see that it
actually is there the thumbnail of the image is there. So let me just type in
with RenderingMode (.alwaysOriginal). Great! Now after this double tapping and
maybe selecting. It is optional to select a new image. After this double
tapping everything goes into place we do have our Image Literal representations
of our images in there. And if we just comment this out you will see the first
line now autocompletes well it's not autocomplete... auto completion has been removed
it displays the image thumbnail as it should. So let me just comment out that
one and and just as a proof let me just copy and paste the other code here
SlideshowedFastest image view and just copy this and that one is with the rabbit and
I will just paste it in there. And as you can see the one with the rabbit also got
fixed. Again you will not be able to have that Auto Completion that was prior to
Xcode 10 so you need to hit shift+command+m to bring up the media library.
Now shift+command+m is a great shortcut for our media library but if you want to
change that simply just go to Xcode preferences. And go into
Key Bindings, yes it is. And under filter type in media and there you go
you can change the default shortcut for that. Now that is it for today for this
tutorial hope you enjoyed this video if you did go ahead and give it a thumbs up
and subscribe to our channel and hit that notification bell! I dare you and go
ahead and comment down below do you think that this is actually a kind of a
small bug introduced in Xcode 10?
Or do you think we will always have as default images in our upcoming projects? Go ahead
and comment down below what do you think and as always I will see you in the next one!