스크린샷 2014-07-04 오전 10.29.25

Swift 매뉴얼의 기본 섹션만 번역하고자 한다.

원문은 iBooks에서 무료로 볼 수 있다.

https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

필자는 영어 전공자가 아니므로 다소 번역이 서툴더라도 양해바랍니다.

 

 

A Swift Tour

 

Tradition suggests that the first program in a new language should print the words “Hello, world” on the screen. In Swift, this can be done in a single line:

새로운 언어의 첫번째 프로그램은 화면에 “Hello, world”를 출력하는 것이 전통이다. Swift에서는 이것을 한 라인으로 수행할 수 있다.

 

println(“Hello, world”)

 

If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main function. You also don’t need to write semicolons at the end of every statement.

C 혹은 Object-C를 이용해 본 경험이 있다면 이 문법은 익숙할 것이다. Swift에서 이 코드 라인은 완성된 프로그램이다. 별도로 입출력이나 문자열 처리와 같은 별도의 라이브러리를 참조할 필요가 없다. 전역으로 작성된 코드는 프로그램의 진입점으로 사용되기 때문에 main 함수가 필요없다. 또한 문장의 끝마다 세미콜론을 입력할 필요가 없다.

 

This tour gives you enough information to start writing code in Swift by showing you how to accomplish a variety of programming tasks. Don’t worry if you don’t understand something—everything introduced in this tour is explained in detail in the rest of this book.

이 투어는 프로그래밍 작업의 다양한 방법으로 Swift 코드 작성을 시작하는데 충분한 정보를 제공한다. 일부 이해가 되지않더라고 걱정하지 말라. 모든 내용이 이 책의 나머지 부분에 자세히 설명되어 있다.

 

NOTE

For the best experience, open this chapter as a playground in Xcode. Playgrounds allow you to edit the code listings and see the result immediately.

최고의 경험을 위해 Xcode의 놀이터로서 본 챕터를 시작한다. 놀이터에서는 코드 목록을 편집하고 바로 결과를 확인할 수 있다.

 

‌Simple Values

Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.

상수를 만드는 “let”와 변수를 만드는 “var”을 사용한다. 상수 값은 컴파일 시점에 확인할 필요가 없지만 정확히 한 번에 값을 할당해야한다. 이는 한 번 선언하지만 해당 이름을 많은 곳에서 사용할 수 있다는 것을 의미한다.

 

 

var myVariable = 42

myVariable = 50

let myConstant = 42

 

 

A constant or variable must have the same type as the value you want to assign to it. However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above, the compiler infers that myVariable is an integer because its initial value is a integer.’

상수와 변수는 할당할 값에 해당하는 타입이어야 한다. 그러나, 항상 유형을 명시할 필요는 없다.컴파일 단계에서 상수나 변수의 형식을 유추하여 제공한다. 위 예제에서 초기값이 정수이므로 컴파일러는 myVariable을 정수로 간주한다.

 

If the initial value doesn’t provide enough information (or if there is no initial value), specify the type by writing it after the variable, separated by a colon.

초기 값이 충분한 정보를 제공하지 못하면(또는 초기 값을 지정하지 않은 경우), 변수 뒤에 콜론으로 구분하여 명시하라.

 

 

let implicitInteger = 70

let implicitDouble = 70.0

let explicitDouble: Double = 70

 

 

EXPERIMENT

Create a constant with an explicit type of Float and a value of 4.

실수형이고 4 값을 가진 상수를 만드시오.

 

 

Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.

값은 암묵적으로 다른 타입으로 절대 변하지 않는다. 다른 타입으로 변환이 필요한 경우, 명시적으로 변환할 형식의 인스턴스를 만든다.

 

 

let label = “The width is “

let width = 94

let widthLabel = label + String(width)

 

 

EXPERIMENT

Try removing the conversion to String from the last line. What error do you get?

위 예제 마지막 줄에서 String 변환을 제거하여 실행하라. 어떤 에러가 발생했는가?

 

There’s an even simpler way to include values in strings: Write the value in parentheses, and write a backslash (\) before the parentheses. For example:

문자열을 연결하는 더 간단한 방법 : 괄호 안에 값을 적고 앞에 역슬레시(\)를 붙인다. 예를들면:

 

let apples = 3

let oranges = 5

let appleSummary = “I have \(apples) apples.”

let fruitSummary = “I have \(apples + oranges) pieces of fruit.”

 

 

EXPERIMENT

Use \() to include a floating-point calculation in a string and to include someone’s name in a greeting.

문자열에 실수 계산이 포함된 \()을 사용하여 인사말에 있는 사람의 이름을 포함할 수 있다.

 

Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets.

대괄호([])를 사용하여 배열과 dictionary를 만들고, 괄호 안에 인덱스나 키를 적어 요소에 접근할 수 있다.

 

 

var shoppingList = [“catfish”, “water”, “tulips”, “blue paint”]

shoppingList[1] = “bottle of water”

 

var occupations = [

“Malcolm”: “Captain”,

“Kaylee”: “Mechanic”,

]

 

occupations[“Jayne”] = “Public Relations”

 

 

To create an empty array or dictionary, use the initializer syntax.

배열이나 dictionary 를 생성을 위해 초기화 문법을 사용하라.

 

 

let emptyArray = String[]()

let emptyDictionary = Dictionary<String, Float>()

 

 

If type information can be inferred, you can write an empty array as [] and an empty dictionary as [:]—for example, when you set a new value for a variable or pass an argument to a function.

타입이 유추가능하면 [:] 처럼 빈 배열과 빈 dictionary 를 만들 수 있다. — 예를들면, 변수의 새 값을 배정하거나 함수에 인자를 전달한다.

 

shoppingList = []   // Went shopping and bought everything.

카테고리: Development

1개의 댓글

kimsreal · 2015년 6월 16일 09:46

더 전문적인 번역서가 있네요. 더이상 번역안할라요.
http://seoh.github.io/Swift-Korean/

답글 남기기