Apple Swift Language 6. Letters and Words (Characters & Strings)”
✉️ 6. Letters and Words (Characters & Strings)
Hello again! 😊 So far, we’ve learned about numbers, calculations, variables, and constants.
Today, we’ll learn how Swift works with letters and sentences using something called Characters and Strings.
We’ll even build a fun mini app that greets you using your name. Let’s go!
🆎 Character vs String
In programming, we use two types to represent letters and words:
Name |
Description |
Examples |
Swift Type |
---|---|---|---|
Character |
A single letter or symbol |
“A”, “가”, “3” |
Character |
String |
A word or sentence |
“Hello”, “Hi!” |
String |
Example:
var letter: Character = "A"
var message: String = "Hello, Swift!"
• A Character holds one single letter
• A String holds multiple letters or full sentences
🧪 Practice: Greeting App with Your Name
Let’s make a small app that says hello using your name!
var name: String = "Minjun"
print("Hello, \(name)! Nice to meet you 😊")
Output:
Hello, Minjun! Nice to meet you 😊
Now try changing the name:
name = "Jiyoo"
You’ll see:
Hello, Jiyoo! Nice to meet you 😊
💡 \(name) is called string interpolation. It’s how we add variables inside a sentence in Swift!
💬 Looping Through Each Letter
What if you want to look at each letter in a word?
let greeting = "Hi"
for char in greeting {
print(char)
}
Output:
H
i
That’s how we know a String is really a group of Characters!
✨ Wrapping Up
Today, we learned how to work with letters and words in Swift:
• Character = one letter
• String = a word or full sentence
• You can use print and \() to show personalized messages
Now you know how to build a greeting app using your name — how cool is that? 😄
Next time, we’ll talk about something called ✅ True or False (Bool).
We’ll learn how to answer questions like: “Did I finish my homework?” using true or false in code!
댓글 쓰기