Literal Creator For Xcode

Get code examples like 'c' instantly right from your google search results with the Grepper Chrome Extension.

A type that can be initialized using an array literal. An array literal is a simple way of expressing a list of values. Simply surround a comma-separated list of values, instances, or literals with square brackets to create an array literal. Object literals are very useful and they often make your code easier to read and understand. String literals are a bit different, though. It is true that a string literal is the easiest solution to create a `String` object. It is straightforward and everyone understands what is going on. But there is a price you pay every time you use a string literal. Xcode has the ability to embed colors directly in code. These colors are called Color Literal and allows you to use a GUI for editing the colors quickly using preset colors or the Mac color picker. We'll also learn how to create a Gradient and use an Overlay modifier to float an element on top.

How do you create User Interfaces with SwiftUI? In this tutorial, we’ll dive into the fundamental concepts and approaches that make SwiftUI work. We’ll discuss views and modifiers, and create a card view in the process.

Here’s what we’ll get into:

Literal creator for xcode editor
  • How SwiftUI’s syntax and composition works
  • What views and modifiers are, and how they work
  • How to work with VStack, HStack, Text, Spacer and Image views
  • Using modifiers like frame(), font(), padding() and shadow()
  • Tips and tricks to work productively with SwiftUI in Xcode
  • Customizing SwiftUI views with code, as well as SwiftUI Inspector
  • Working with Live Preview in Xcode
  • How SwiftUI uses closures and generics to enable a concise syntax

Ready? Let’s go.

Create a Card View UI with SwiftUI

We’re going to start building the Card View app by setting up a new project in Xcode. Simply choose the Single View App, make sure to choose SwiftUI for User Interface, and save the project in a convenient folder on your Mac.

In the new project, open ContentView.swift. You now see the following SwiftUI code:

Don’t see the Preview? Click Resume at the top of Xcode.

Before moving on, feel free to rename the ContentViewstruct to CardView. You can do this by right-clicking on ContentView in structContentView: View{ ··· and then choose Refactor → Rename. In the UI that appears, change the name of the view, and make sure that the name is changed throughout the project.

Awesome! Let’s get a move on with the first view in our Card View: Text. Here’s what you do:

  1. Find the Text view inside the CardView struct
  2. Change its text, i.e. the string, from Hello world! to your own name
  3. Check out your changes in the Preview on the right of Xcode

This is what the view looks like in code right now:

If your view didn’t have a Text view to begin with, simply add it yourself right within the body property. Make sure your code reflects the above example.

The Preview in Xcode updates automatically as you make changes, which is really neat. It works for almost every visual change you make in your SwiftUI views, which is an incredibly productive way to build views with SwiftUI.

As you’ve guessed, the Text view in SwiftUI displays… text. You can change its font, color, size, padding, and a whole lot more. The Text view in SwiftUI is the defacto component to display textual strings, much like UILabel for UIKit.

Next up, let’s add two more views to the CardView. Add the following code on the lines below the previous Text element:

Feel free to customize this view with your own name, job title and Twitter username.

What’s going on now? It looks like we’ve got an error in Xcode! Here’s what it says:

  • Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
  • Result of ‘Text’ initializer is unused

Hmm. This is a good opportunity to tell you: SwiftUI error messages in Xcode don’t always make sense. As for the above error messages, SwiftUI thinks we’ve made a mistake with the return type for CardView, and it’s also confused about what exactly we’re “returning” from the view.

Let’s fix that next.

Learn how to build iOS apps

Get started with iOS 14 and Swift 5

Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.

Build Views with Stacks

The real problem right here is that we’ve added 3 subviews to a View that’s only supposed to have one subview; one return value.

Here’s how we’re going to fix that. Change your code to reflect the following:

We’ve wrapped the 3 Text views in to a VStack, a vertical stack view. Each of the text views now shows up above the other, in a neat vertical stack. Stack views are centered by default, which you can see in the Preview on the right of Xcode.

As its name implies, a Stack displays multiple views on top of each other. In SwiftUI, we’ve got 3 types of stacks: the VStack for vertical stacking, the HStack for horizontal stacking, and ZStack for stacking by 3D depth.

Here’s what we got so far:

Next up, we’re going to add another stack: the HStack, or horizontal stack. We’ll use it to add an image to the right of the text views we’ve added.

Here’s how:

  1. While holding the Command key, click the VStack view
  2. Click Embed in HStack

This will wrap the VStack, that we created before, in a new HStack. We’re wrapping views in views, creating elaborate structures, which is what SwiftUI is all about!

This is the body of the view we’ve coded so far:

Here’s what you do next:

  1. Add an empty line right below the closing bracket } of the VStack
  2. Press Command + Shift + L or click the + button in the top-right of Xcode
  3. The Library shows up now; Make sure you’ve selected the Views library, the left-most tab in the UI
  4. In the search field, start typing: image
  5. Double-click the Image view when it appears in the list, which inserts an Image() view into the CardView at the cursor position

Finally, import an image into your project’s asset catalog in Xcode. Doesn’t matter what it is, as long as it’s square and about 75 × 75 points. Change the name of the Image() view into the name of the asset you imported.

You’ll end up with the following code:

Let’s do a quick recap.

We started with just one Text view inside the CardView body. Then, we added 2 more Text views to the view – for 3 bits of text total. Because a SwiftUI view can technically only have one subview, we wrapped the Text views in a vertical stack: the VStack. This shows the text views vertically on top of each other.

We then wanted to add an image to the right of those text items. We embeded the VStack into a HStack. We added an Image view directly into the HStack, which stacks the VStack and Image views horizontally next to each other. Neat!

Next up, we’re going to adjust the views with modifiers.

You can use the Library with Command + Shift + L to insert SwiftUI views into your code, or you can just code them yourself. You can even edit your UI with the SwiftUI Inspector, which we’ll discuss next.

Configure Views with Modifiers

Building views with SwiftUI consists of 2 parts. We’ve discussed how you build views like the CardView with other views – subviews – that you compose to create a User Interface. The second step in building UIs happens with modifiers that allow you to configure views, in all sorts of ways.

So far we’ve created the following CardView:

In the next few sections, we’re going to adjust the view’s text, color, position, shadow, and corner radius. We’ll also discuss a few of the approaches you can use to configure views with Xcode and SwiftUI. Let’s move on!

Changing the Text Views

First, we’re changing the top Text view – the one with your name. Change the Text(···) code to this:

It’s straightforward to infer that the above code changes the Font of the text view to a system’s font, size 25, and it’s Font Weight is set to semibold.

Let’s take a closer look at the syntax. In SwiftUI, you can use modifiers to change the attributes of a view. In practical terms, modifiers are Swift functions that take input parameters and return new views. You can chain them together, like we’ve done.

Here, check this out. It’s the same code as before, but written on one line.

Reminds you a bit of Swift functions, right? It’s the same dot-style syntax you’ve used with “regular” Swift. The fontWeight(_:) function takes a parameter, .semibold, which is an enum of type Font.Weight, for example.

Take a look at this approach:

In the above code, we’ve deconstructed the chained SwiftUI syntax and assigned the Text view to a variable named text. Instead of chaining all modifiers together, we’re calling the font(_:) and fontWeight(_:) functions on text and assign the resulting view right back to text again. Each call to a modifier returns a View.

This approach is called the Builder Pattern, which is fundamental to SwiftUI’s syntax. You’re essentially “building” up a view, one modifier at a time, constantly creating new views in the process. The chained syntax is of course the most concise. The indentation helps you read the code more easily.

OK, back to the code. Here’s what we got so far:

Adjust the 2 other Text views to this:

That #colorLiteral code is a color literal, a literal value for a color that you can add and edit directly in Xcode. It’s super useful! Here’s how you can insert your own colors in Xcode’s editor:

  1. (Remove the color literal from your code)
  2. Make sure the cursor is placed inside the parentheses of Color()
  3. Press the Esc key (or your code completion trigger key)
  4. Start typing Color Literal, then select that item from the dropdown list
  5. In the color gizmo that appears, select your favorite color (if the color picker doesn’t show, double-click the color literal/square itself)

Changing the VStack

Awesome! Next up, we’re going to change the VStack view. Right now, the stack views are centered. This is their default state. We want to align the text to the left and add some vertical spacing between them.

As we’ve discussed before, modifiers change the attributes of SwiftUI views. You can modify a view with a function, like font(), but you can also use the view’s initializer directly.

Pick one of the following 2 approaches:

  1. Adjust the VStack with code:
    1. Locate the VStack; inside the top-level HStack view of CardView
    2. Change its code to VStack(alignment: .leading, spacing: 2) {
  2. Adjust the VStack with SwiftUI Inspector:
    1. Locate the VStack; inside the top-level HStack view of CardView
    2. Hold the Command key and click the VStack view
    3. Choose Show SwiftUI Inspector in the menu that pops out
    4. Set spacing to 2 and alignment to Left (or “Leading”)

See what’s going on there? You can adjust views in SwiftUI with code, as well as use the SwiftUI Inspector. If you don’t exactly know the modifiers you’re looking for, the SwiftUI Inspector is an effective way to customize your views.

Quick Tip: You can also show the SwiftUI Inspector by holding Option + Command while clicking on a SwiftUI view. You can do this in the Live Preview area as well.

Working with Spacer

Right now, the text views and the images are a bit too close together. It’s better to give the UI more space, so it “breathes” some more.

Here’s how you do that:

  1. Locate the closing bracket of the VStack, and the Image view
  2. Add a new line between them; code Spacer() in there

You’ve got this code now:

See how the HStack has 3 subviews now? A VStack, a Spacer and an Image. The spacer is creating space between the VStack and the image, pressing them outwards. Using spacers a great way to distribute views evenly. We’ll add some constraints next, to limit the width of the card view.

Changing the HStack

The last thing we’ll change about the CardView is the HStack. We’re going to add a few modifiers that’ll change its layout.

First, locate the closing bracket of the HStack in the CardView. Add the following code below it:

You’ll end up with the following code:

It’s worth noting here that the frame(), padding() etc. modifiers affect the HStack. They’re tacked onto the HStack() { ··· } initializer function. The modifiers look like they’re “outside” the HStack, but the syntax here is no different from the Text() and .font() we’ve used before.

How do these modifiers work?

  • The frame() modifier adjusts the maximum width of the view. That maxWidth is just one of its parameters; it’s got minWidth, width, etc., too.
  • The padding() modifier adds some space around the view, on its inside, i.e. “padding”. The sensible default of this function is 20 points.
  • The background() modifier sets the background color of the view to white.
  • The cornerRadius() modifier adds round borders to the view. The distance between the start of the round edge to the corner of the view is 20 points.
  • The shadow() modifier adds a drop shadow to the view. It’s got a smoothing radius of 20 points and a default black/gray color.

Modifiers in SwiftUI often – not always – return a new view. Because of this, the order of modifiers is important. If you switch the places of the padding() and background() modifiers, for example, the white background won’t extend to the edge of the view.

Literal Creator For Xcode Mac

Why is that? It’s because the padding() function returns a View value on which background() is called. You want the background to extend behind the padding, which is why padding() needs to get called first. The order of modifiers is intuitive, you call modifiers from left-to-right or top-to-bottom.

Before we move on to the next section, quickly add another round border to the Image view – for good measure:

Awesome! Keepit.

A Closer Look at SwiftUI’s Syntax

Let’s circle back for a bit and discuss the composition of a view in SwiftUI. Here’s what we’ve made, sans modifiers:

First things first. Every SwiftUI view, which conforms to the Viewprotocol, has one property called body. This is a computed property that returns one value. We build our one subview inside that view body.

Thanks to the some keyword and opaque return types, the implementation of the view, i.e. the code that’s inside body, determines the concrete type of the body property. This way we don’t have to change the type of body when its implementation changes.

Now that we know that body is a computed property, what value does it return? The top-level view in CardView is a HStack. But… where’s the return keyword? Thanks to some syntactic sugar in Swift 5, that return keyword is omitted!

See? We’re just removing the return keyword to make the SwiftUI syntax more concise and readable. This obscures the fact that the body property returns one value – like any other Swift function – so that’s important to point out.

Remember when we tried to add 3 Text views to the CardView body? That won’t work!

That’s why we added the VStack, to show the Text views in a vertical stack. Like this:

But… why can’t a View have multiple subviews, but a VStack can? In the above code, the VStack clearly has 3 subviews! What’s going on?

At this point, it’s essential to know that the syntax for SwiftUI royally makes use of closures, trailing closure syntax, and generics. In reality, the above VStack amounts to the following explicit Swift code:

Whoah, what’s that mess!? What you’re looking at here is a VStackinitializer. It takes a parameter content, whose type is a closure that returns a TupleView. A TupleView is a SwiftUI view that’s created from a tuple of views, kinda like a container. It uses generics, so we’re specifying the concrete type of the values inside the tuple. Yes, those are double parentheses (()), one for the tuple, one for the initializer parameters…

Take a moment to appreciate that the above explicit notation of the VStack can change at any moment, when you change the CardView to something else. That’s the power behind the SwiftUI framework. Because of the generic nature of SwiftUI, a value’s type can change to something better, while remaining strongly typed.

It gets even more crazy than that, but we won’t go there. This much is clear: SwiftUI consists in part of syntax around generic types and closures. It’s syntactic sugar for composable types – views – which we can put together with as little code as possible.

Back to Syntactic Sugar World…

Aah, much better! But now you know: Those 3 Text views actually comprise a tuple that’s provided as the return type of a closure that’s provided to the VStack initializer. It’s mind-boggling…

Note: The term syntactic sugar means that a programming language has syntax that obscures some implementation with nicer, prettier syntax. The implementation doesn’t change, but the code and syntax itself does – which makes all the difference. Code is there for the programmer, to the computer it’s bits and bytes anyway.

Learn how to build iOS apps

Get started with iOS 14 and Swift 5

Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.


Further Reading

Awesome! We’ve taken a deep-dive into SwiftUI and how you can use views and modifiers to build UIs. Here’s what we’ve discussed:

  • How SwiftUI’s syntax and composition works
  • What views and modifiers are, and how they work
  • How to work with VStack, HStack, Text, Spacer and Image views
  • Using modifiers like frame(), font(), padding() and shadow()
  • Tips and tricks to work productively with SwiftUI in Xcode
  • Customizing SwiftUI views with code, as well as SwiftUI Inspector
  • Working with Live Preview in Xcode
  • How SwiftUI uses closures and generics to enable a concise syntax

Want to learn more? Check out these resources:

A type that can be initialized using an array literal.

An array literal is a simple way of expressing a list of values. Simply surround a comma-separated list of values, instances, or literals with square brackets to create an array literal. You can use an array literal anywhere an instance of an ExpressibleByArrayLiteral type is expected: as a value assigned to a variable or constant, as a parameter to a method or initializer, or even as the subject of a nonmutating operation like map(_:) or filter(_:).

Arrays, sets, and option sets all conform to ExpressibleByArrayLiteral, and your own custom types can as well. Here's an example of creating a set and an array using array literals:

The Set and Array types each handle array literals in their own way to create new instances. In this case, the newly created set drops the duplicate value ('Dave') and doesn't maintain the order of the array literal's elements. The new array, on the other hand, matches the order and number of elements provided.

Note: An array literal is not the same as an Array instance. You can't initialize a type that conforms to ExpressibleByArrayLiteral simply by assigning an existing array.

Whenever possible, Swift's compiler infers the full intended type of your array literal. Because Array is the default type for an array literal, without writing any other code, you can declare an array with a particular element type by providing one or more values.

In this example, the compiler infers the full type of each array literal.

An empty array literal alone doesn't provide enough information for the compiler to infer the intended type of the Array instance. When using an empty array literal, specify the type of the variable or constant.

Because many functions and initializers fully specify the types of their parameters, you can often use an array literal with or without elements as a parameter. For example, the sum(_:) function shown here takes an Int array as a parameter:

When you call a function that does not fully specify its parameters' types, use the type-cast operator (as) to specify the type of an array literal. For example, the log(name:value:) function shown here has an unconstrained generic value parameter.

Literal Creator For Xcode App

Literal Creator For Xcode

Add the capability to be initialized with an array literal to your own custom types by declaring an init(arrayLiteral:) initializer. The following example shows the array literal initializer for a hypothetical OrderedSet type, which has setlike semantics but maintains the order of its elements.

InheritanceView Protocol Hierarchy →
Associated Types
Importimport Swift



Creates an instance initialized with the given elements.

Literal Creator For Xcode Download


Literal Creator For Xcode Windows

init(arrayLiteral elements: Self.Element..)