6

When I try using literals in a Playground, it shows me like this (doesn't show my image):

anotherExample in above i add image or UIImage literal but doesn't show their contents.\

and also this with colors (I can't even see colors):

another example

3 Answers3

20

Type #colorLiteral() and #imageLiteral() to see a color picker or image picker respectively.

color literal or image literal in xcode placeholder

Note: Image picker will list your images at Assets.xcassets.

Esmaeil
  • 558
  • 5
  • 11
14

When you type Color Literal, a white box appears. In order to set the required colour, tap on that white box to select the colour from Color Picker.

This is what it looks like:

enter image description here

pkamb
  • 33,281
  • 23
  • 160
  • 191
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • i did that before, as you can see in my image, i think there is something wrong in my Xcode –  Oct 23 '17 at 10:12
  • Clean and run your project. Sometimes this also causes unusual issues. – PGDev Oct 23 '17 at 10:27
  • i'm did that but doesn't change, even in Playground –  Oct 23 '17 at 10:40
  • 2
    @MasihKia are you double clicking on the white box? When you first add the Colour Literal it will be white. You then have to actually set it. You don't get an automatic drop down of different colours. – Fogmeister Oct 23 '17 at 11:01
  • 1
    @Fogmeister Thanks, thats work with double click, Now my Problem is with images literal, i can't see their contents in their little square, but image is there. –  Oct 23 '17 at 11:09
  • @MasihKia did you double click the image icon thing? Also, image literals only work with image in Asset catalogs. Is your image in an asset catalog? – Fogmeister Oct 23 '17 at 11:10
  • @MasihKia Can you add a screenshot of your asset catalog to the OP. – Fogmeister Oct 23 '17 at 11:13
  • @Fogmeister i do this in playground, and edit my question with new image –  Oct 23 '17 at 11:25
  • @MasihKia OK, so going back to my question and statement... “Are you using an asset catalog? Image Literals only work with images in asset catalogs”... You are not using an asset catalog because you can’t use them in playgrounds. And so image literals won’t work. – Fogmeister Oct 23 '17 at 11:26
  • @MasihKia Except that isn’t a playground and if you double click the icon in that screenshot there you’ll be able to pick the image. This isn’t hard to understand. Image literals require asset catalogs. – Fogmeister Oct 23 '17 at 11:37
  • @Fogmeister it shows but dosen't shows image inside square like this picture : https://i.stack.imgur.com/dZpFk.png –  Oct 23 '17 at 11:41
  • @MasihKia are you reading the parts where I said “Image Literals require an asset catalog”? You seem to not be understanding that. In order to use Image Literals you need to have your images in an asset catalog. Have a google for Xcode asset catalog. You need on of those if you want to use image literals. If you don’t have an asset catalog you can’t use image literals. I’m not sure how else I can phrase this? – Fogmeister Oct 23 '17 at 11:42
  • @Fogmeister i know & do this now & sent picture for you: i.stack.imgur.com/Y4EKw.png but doesn't change anything –  Oct 23 '17 at 11:46
  • @MasihKia I give in. You said you are in a playground yes? You cannot (AFAIK) use Asset catalogs in a playground. You have shown a screenshot of your playground. It shows an image that is not in an asset catalog. You then seem to have shown a selection of random images from the internet. If you are not in a playground and you are using an asset catalog then double click the icon. It will show a way to select the image you want to use. – Fogmeister Oct 23 '17 at 11:48
  • @MasihKia The first lime of your question “When I try using image literals in a playground...”. You can’;t use image literals in a playground because you need an asset catalog to use image literals and you can’t use asset catalogs in playgrounds (AFAIK). – Fogmeister Oct 23 '17 at 11:49
  • @MasihKia If you have got something wrong in your question then update your question. I am going now. This is getting too ciurcular. – Fogmeister Oct 23 '17 at 11:50
  • @Fogmeister no i do that in project application not in the playground, did you see my image send you, that is my work –  Oct 23 '17 at 11:50
  • Not working any more. Out dated – alegelos Oct 05 '22 at 17:54
5

Make sure you add to the class import UIKit

fullmoon
  • 8,030
  • 5
  • 43
  • 58