Swift Hello World!

If you’re new to programming then Hello World is where it all begins. It’s usually the first line of code you’ll run to make sure you’re code works and to see output on the screen as a new coder. It’s nothing fanc but it’s traditional and usually the first line of code you’ll write learning any new language so lets use swift to show Hello World in Xcode

What You’ll Learn

How to write your first "Hello, World! in Swift

Writing Hello World in Xcode Terminal (MacOS)

Lets open up xcode and get started!

  1. Open Xcode.
  2. Go to File > New > Project.
  3. Choose macOS > Command Line Tool.
  4. Give your project a name like HelloWorld.
  5. In main.swift, replace the code with:
import Foundation

print("Hello, World!")

6.Press Command + R to run. You’ll see Hello, world! in the output at the bottom.

Writing Hello World in Swift Playground

  1. Open Xcode
  2. Click File > New > Playground
  3. Click “Blank” to start a new playground.
  4. Give your playground a name like HelloWorld.
  5. In the editor area, type:
print("Hello, world!")
  1. Press the Run button (play icon at the bottom).
  2. You’ll see Hello, world! in the console output.

Writing Hello World Xcode with SwiftUI

  1. Open Xcode.
  2. Go to File > New > Project.
  3. Select App under the iOS tab and click Next.
  4. Name your project (e.g., HelloWorldApp) and make sure SwiftUI is selected for Interface.
  5. Click Next, choose a location, and click Create.
import SwiftUI

struct ContentView: View {
    var body: some View {
        
        
        
            Text("Hello, world!")
        
        .padding()
    }
}

#Preview {
    ContentView()
}
  1. Run the App.
  2. Press Command + R to build and run the app.
  3. You’ll see a white screen with the text: “Hello, world!”

Now you’ve wrote your first Hello World in 3 different ways using swift.

Here is the Swift owls Version of Hello World using Xcode with SwiftUI added in a VStack with an Image and Text.

Fell free to try to add your own image in your Hello World App by dragging your image into the app directory and typing the name out in Image like below.

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        
        VStack {
            Image("swift_owls_logo")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

How to loop through “Hello World” in different languages

A for loop allows you to repeat code multiple times usually while going through a list

Now we’re going to take Hello World to another level and print it out using swift in multiple languages.

import SwiftUI

let helloWorldTranslations: [(language: String, greeting: String)] = [
    ("English", "Hello, world!"),
    ("Spanish", "¡Hola, mundo!"),
    ("French", "Bonjour le monde!"),
    ("German", "Hallo, Welt!"),
    ("Italian", "Ciao, mondo!"),
    ("Portuguese", "Olá, mundo!"),
    ("Russian", "Привет, мир!"),
    ("Chinese (Simplified)", "你好,世界!"),
    ("Chinese (Traditional)", "你好,世界!"),
    ("Japanese", "こんにちは、世界!"),
    ("Korean", "안녕하세요, 세계!"),
    ("Arabic", "مرحبا بالعالم!"),
    ("Hindi", "नमस्ते दुनिया!"),
    ("Bengali", "ওহে বিশ্ব!"),
    ("Punjabi", "ਸਤਿ ਸ੍ਰੀ ਅਕਾਲ ਦੁਨਿਆ!"),
    ("Turkish", "Merhaba, dünya!"),
    ("Vietnamese", "Xin chào, thế giới!"),
    ("Polish", "Witaj, świecie!"),
    ("Ukrainian", "Привіт, світ!"),
    ("Romanian", "Salut, lume!"),
    ("Dutch", "Hallo, wereld!"),
    ("Greek", "Γειά σου, κόσμε!"),
    ("Thai", "สวัสดี, โลก!"),
    ("Czech", "Ahoj, světe!"),
    ("Swedish", "Hej, världen!"),
    ("Finnish", "Hei, maailma!"),
    ("Danish", "Hej, verden!"),
    ("Hungarian", "Helló, világ!"),
    ("Hebrew", "שלום עולם!"),
    ("Indonesian", "Halo, dunia!"),
    ("Norwegian", "Hei, verden!"),
    ("Malay", "Halo, dunia!"),
    ("Swahili", "Hujambo, dunia!"),
    ("Tagalog", "Kamusta, mundo!"),
    ("Slovak", "Ahoj, svet!"),
    ("Serbian", "Здраво, свете!"),
    ("Croatian", "Pozdrav, svijete!"),
    ("Bulgarian", "Здравей, свят!"),
    ("Persian", "سلام دنیا!"),
    ("Tamil", "வணக்கம், உலகம்!"),
    ("Telugu", "హలో, ప్రపంచం!"),
    ("Marathi", "हॅलो, जग!"),
    ("Gujarati", "હેલો, દુનિયા!"),
    ("Kannada", "ಹಲೋ, ಪ್ರಪಂಚ!"),
    ("Malayalam", "ഹലോ, ലോകം!"),
    ("Urdu", "ہیلو، دنیا!"),
    ("Nepali", "नमस्कार, संसार!"),
    ("Sinhala", "හෙලෝ, ලෝකය!"),
    ("Pashto", "سلام نړی!"),
    ("Amharic", "ሰላም ዓለም!"),
    ("Zulu", "Sawubona, mhlaba!"),
    ("Xhosa", "Molo, hlabathi!"),
    ("Afrikaans", "Hallo, wêreld!"),
    ("Icelandic", "Halló, heimur!"),
    ("Estonian", "Tere, maailm!"),
    ("Latvian", "Sveika, pasaule!"),
    ("Lithuanian", "Labas, pasauli!"),
    ("Slovenian", "Pozdravljen, svet!"),
    ("Macedonian", "Здраво, свету!"),
    ("Albanian", "Përshendetje, botë!"),
    ("Armenian", "Բարեւ աշխարհ!"),
    ("Georgian", "გამარჯობა, მსოფლიო!"),
    ("Mongolian", "Сайн байна уу, дэлхий!"),
    ("Kazakh", "Сәлем, әлем!"),
    ("Uzbek", "Salom, dunyo!"),
    ("Azerbaijani", "Salam, dünya!"),
    ("Turkmen", "Salam, dünýä!"),
    ("Kurdish", "Silav, cîhan!"),
    ("Hausa", "Sannu, duniya!"),
    ("Yoruba", "Pẹlẹ o, ayé!"),
    ("Igbo", "Ndewo, ụwa!"),
    ("Basque", "Kaixo, mundua!"),
    ("Galician", "Ola, mundo!"),
    ("Catalan", "Hola, món!"),
    ("Irish", "Dia duit, domhan!"),
    ("Scottish Gaelic", "Halò, a shaoghail!"),
    ("Welsh", "Helo, byd!"),
    ("Breton", "Demat, bed!"),
    ("Esperanto", "Saluton, mondo!"),
    ("Latin", "Salve, mundi!"),
    ("Sanskrit", "नमः संसार!"),
    ("Tibetan", "ཕལ་ཆེན་འཛམ་གླིང་།"),
    ("Lao", "ສະບາຍດີ, ທົ່ວໂລກ!"),
    ("Khmer", "សួស្តី​ពិភពលោក!"),
    ("Maori", "Kia ora, ao!"),
    ("Samoan", "Talofa, lalolagi!"),
    ("Tongan", "Mālō e lelei, māmani!"),
    ("Fijian", "Bula, vuravura!"),
    ("Haitian Creole", "Bonjou, mond!"),
    ("Quechua", "Rimaykullayki, pacha!"),
    ("Nahuatl", "Niltze, tlaltikpak!"),
    ("Inuktitut", "ᐊᔭᕙ, ᐅᑭᐅᖅ!"),
    ("Cherokee", "ᎣᏏᏲ, ᎡᎯ!"),
    ("Hawaiian", "Aloha, honua!"),
    ("Guarani", "Mba’éichapa, yvóra!"),
    ("Luxembourgish", "Moien, Welt!"),
    ("Faroese", "Hey, heimur!"),
    ("Malagasy", "Miarahaba, izao tontolo izao!"),
    ("Belarusian", "Прывітанне, свет!")
]


struct ContentView: View {
    var body: some View {
        
        
        VStack {
            Image("swift_owls_logo")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }.onAppear(){
            
            for (language, greeting) in helloWorldTranslations{
                
                
                print("\(language) | \(greeting)")
            }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

We’ve taken a for loop, iterated through an Array or list of items and displayed the language with the greeting Hello World in that respective language using the .onAppear function. Try it out on your screen.

What to learn next after “Hello, World!”

Swift is a powerful programming language that will give you the ability to take an idea from your head and build something cool. If you want to learn how to code in swift then sign up to my swift course for beginners.

Join the 5-Day Swift Series

Thanks! You’re in. Check your inbox.
Something went wrong. Try again.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top