📕 iOS/100 Days of Swift

[Day 1] variables, simple data types, and string interpolation

이오🐥 2023. 4. 5. 10:08
As Mark Twain once said,
“the secret to getting ahead is getting started.”

 

 1. Variables 

Variables are places where you can store program data.

They are called variables because they can vary

– you can change their values freely.

 

변수는 data를 저장할 수 있는 곳이다.

variables라고 부르는 이유는 vary 할 수 있기 때문!

 

 


 2. Strings and integers 

Swift is what’s known as a type-safe language,

which means that every variable must be of one specific type.

 

Swfit는 type-safe launguage입니다.

모든 변수는 하나의 type을 가져야 합니다.

 

It becomes impossible to keep the types of your variables

in your head at all times,

so we’re effectively shifting that work on to Swift instead.

 

모든 변수의 type을 기얻하고 있는 건 힘들기 때문에,

Swift가 대신 할 수 있도록 하는 것입니다.

 


 3. Multi-line strings 

If you want multi-line strings you need slightly different syntax:

start and end with three double quote marks, like this:

var str1 = """
This goes
over multiple
lines
"""

If you only want multi-line strings to format your code neatly,

and you don’t want those line breaks to actually be in your string,

end each line with a \, like this:

var str2 = """
This goes \
over multiple \
lines
"""

 

 


 4. Doubles and Booleans 

 


 5. String interpolation 

You can place any type of variable inside your string

– all you have to do is write a backslash, \,

followed by your variable name in parentheses.

 

문자열 안에 어떤 type의 변수든 넣을 수 있습니다.

\(변수 이름)과 같이 사용하면 됩니다.

 

For example:

var score = 85
var str = "Your score was \(score)"

 


 6. Constants 

The let keyword creates constants,

which are values that can be set once and never again.

For example:

let taylor = "swift"

 


 7. Type annotations 

Type inference:

Swift is able to infer the type

of something based on how you created it.

 

Swift는 내가 할당한 값에 따라 type을 추론할 수 있다.

 

If you want you can be explicit

about the type of your data

rather than relying on Swift’s type inference,

 

만약 Swift가 추론한 type 대신에 type을 정하고 싶다면,

 

like this:

let album: String = "Reputation"
let year: Int = 1989
let height: Double = 1.78
let taylorRocks: Bool = true

 


 8. Simple types: Summary 

 

Day 1 – 100 Days of Swift

Follow the 100 Days of Swift and learn to build apps for free.

www.hackingwithswift.com