ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Swift/Tour
    코딩/Swift 2024. 3. 2. 10:56
    728x90

    이 글은 아래 링크를 번역한 것입니다.

    원본을 참조하세요.

    https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/

    A Swift Tour

    Explore the features and syntax of Swift.

    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:

    Swift의 기능과 구문을 살펴보세요.

    전통에 따르면 새로운 언어의 첫 번째 프로그램은 "Hello, world!"라는 단어를 화면에 인쇄해야 합니다. Swift에서는 이 작업을 한 줄로 수행할 수 있습니다.

    print("Hello, world!")
    // Prints "Hello, world!"

    This syntax should look familiar if you know another language — in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like outputting text or handling strings. 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.

    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에서 이 코드 줄은 완전한 프로그램입니다. 텍스트 출력이나 문자열 처리와 같은 기능을 위해 별도의 라이브러리를 가져올 필요가 없습니다. 전역 범위에서 작성된 코드는 프로그램의 진입점으로 사용되므로 main() 함수가 필요하지 않습니다. 또한 모든 명령문 끝에 세미콜론을 쓸 필요가 없습니다.

    이 둘러보기는 다양한 프로그래밍 작업을 수행하는 방법을 보여줌으로써 Swift에서 코드 작성을 시작하는 데 충분한 정보를 제공합니다. 이해하지 못하는 부분이 있어도 걱정하지 마세요. 이 둘러보기에서 소개된 모든 내용은 이 책의 나머지 부분에서 자세히 설명됩니다.

    728x90

    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 an integer.

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

    상수 또는 변수는 할당하려는 값과 동일한 유형을 가져야 합니다. 그러나 항상 유형을 명시적으로 작성할 필요는 없습니다. 상수나 변수를 생성할 때 값을 제공하면 컴파일러가 해당 유형을 추론할 수 있습니다. 위의 예에서 컴파일러는 myVariable의 초기 값이 정수이기 때문에 정수라고 추론합니다.

    초기값이 충분한 정보를 제공하지 못하는 경우(또는 초기값이 없는 경우) 변수 뒤에 콜론으로 구분하여 유형을 지정합니다.

    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.

    명시적인 유형의 Float와 값 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.

    문자열에 부동 소수점 계산을 포함하고 인사말에 누군가의 이름을 포함하려면 \()를 사용합니다.

    Use three double quotation marks (""") for strings that take up multiple lines. Indentation at the start of each quoted line is removed, as long as it matches the indentation of the closing quotation marks. For example:

    여러 줄을 차지하는 문자열에는 세 개의 큰따옴표(""")를 사용합니다. 닫는 따옴표의 들여쓰기와 일치하는 한 인용된 각 줄의 시작 부분에 있는 들여쓰기가 제거됩니다. 예를 들면 다음과 같습니다.

    let quotation = """
    Even though there's whitespace to the left,
    the actual lines aren't indented.
    Except for this line.
    Double quotes (") can appear without being escaped.
    
    I still have \(apples + oranges) pieces of fruit.
    """

    Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.

    대괄호([])를 사용하여 배열과 사전을 만들고 대괄호 안에 인덱스나 키를 작성하여 해당 요소에 액세스합니다. 마지막 요소 뒤에는 쉼표가 허용됩니다.

    var fruits = ["strawberries", "limes", "tangerines"]
    fruits[1] = "grapes"
    
    var occupations = [
        "Malcolm": "Captain",
        "Kaylee": "Mechanic",
    ]
    occupations["Jayne"] = "Public Relations"

    Arrays automatically grow as you add elements.

    요소를 추가하면 배열이 자동으로 늘어납니다.

    fruits.append("blueberries")
    print(fruits)
    // Prints "["strawberries", "grapes", "tangerines", "blueberries"]"

    You also use brackets to write an empty array or dictionary. For an array, write [], and for a dictionary, write [:].

    또한 대괄호를 사용하여 빈 배열이나 사전을 작성합니다. 배열의 경우 [], 사전의 경우 [:]를 씁니다.

    fruits = []
    occupations = [:]

    If you’re assigning an empty array or dictionary to a new variable, or another place where there isn’t any type information, you need to specify the type.

    빈 배열이나 사전을 새 변수에 할당하거나 유형 정보가 없는 다른 위치에 할당하는 경우 유형을 지정해야 합니다.

    let emptyArray: [String] = []
    let emptyDictionary: [String: Float] = [:]

    Control Flow

    Use if and switch to make conditionals, and use for-in, while, and repeat-while to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required.

    조건문을 만들려면 if 및 스위치를 사용하고, 루프를 만들려면 for-in, while 및 반복-while을 사용하세요. 조건 또는 루프 변수 주위의 괄호는 선택 사항입니다. 본문 주위에 {}가 필요합니다.

    let individualScores = [75, 43, 103, 87, 12]
    var teamScore = 0
    for score in individualScores {
        if score > 50 {
                teamScore += 3
            } else {
                teamScore += 1
        }
    }
    print(teamScore)
    // Prints "11"

    In an if statement, the conditional must be a Boolean expression — this means that code such as if score { ... } is an error, not an implicit comparison to zero.

    You can write if or switch after the equal sign (=) of an assignment or after return, to choose a value based on the condition.

    if 문에서 조건은 부울 표현식이어야 합니다. 즉, if Score { ... }와 같은 코드는 0에 대한 암시적 비교가 아니라 오류입니다.

    할당의 등호(=) 뒤에 또는 return 뒤에 if 또는 스위치를 써서 조건에 따라 값을 선택할 수 있습니다.

    let scoreDecoration = if teamScore > 10 {
        "🎉"
    } else {
        ""
    }
    print("Score:", teamScore, scoreDecoration)
    // Prints "Score: 11 🎉"

    You can use if and let together to work with values that might be missing. These values are represented as optionals. An optional value either contains a value or contains nil to indicate that a value is missing. Write a question mark (?) after the type of a value to mark the value as optional.

    누락될 수 있는 값에 대해 작업하려면 if와 let을 함께 사용할 수 있습니다. 이러한 값은 선택 사항으로 표시됩니다. 선택적 값은 값을 포함하거나 값이 누락되었음을 나타내는 nil을 포함합니다. 값을 선택 사항으로 표시하려면 값 유형 뒤에 물음표(?)를 씁니다.

    var optionalString: String? = "Hello"
    print(optionalString == nil)
    // Prints "false"
    
    var optionalName: String? = "John Appleseed"
    var greeting = "Hello!"
    if let name = optionalName {
        greeting = "Hello, \(name)"
    }

    Experiment

    Change optionalName to nil. What greeting do you get? Add an else clause that sets a different greeting if optionalName is nil.

    optionName을 nil로 변경합니다. 어떤 인사말을 받나요? optionName이 nil인 경우 다른 인사말을 설정하는 else 절을 추가하세요.

    If the optional value is nil, the conditional is false and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.

    Another way to handle optional values is to provide a default value using the ?? operator. If the optional value is missing, the default value is used instead.

    선택적 값이 nil이면 조건은 false이고 중괄호 안의 코드는 건너뜁니다. 그렇지 않으면 선택적 값이 래핑 해제되고 let 뒤의 상수에 할당되어 코드 블록 내에서 래핑 해제된 값을 사용할 수 있게 됩니다.

    선택적 값을 처리하는 또 다른 방법은 ?? 연산자를 사용하여 기본값을 제공하는 것입니다. 선택적 값이 누락된 경우 기본값이 대신 사용됩니다.

    let nickname: String? = nil
    let fullName: String = "John Appleseed"
    let informalGreeting = "Hi \(nickname ?? fullName)"

    You can use a shorter spelling to unwrap a value, using the same name for that unwrapped value.

    래핑 해제된 값에 대해 동일한 이름을 사용하여 더 짧은 코드를 사용하여 값을 래핑 해제할 수 있습니다.

    if let nickname {
        print("Hey, \(nickname)")
    }
    // Doesn't print anything, because nickname is nil.

    Switches support any kind of data and a wide variety of comparison operations — they aren’t limited to integers and tests for equality.

    스위치는 모든 종류의 데이터와 다양한 비교 작업을 지원합니다. 이는 정수 및 동등성 테스트에만 국한되지 않습니다.

    let vegetable = "red pepper"
    switch vegetable {
        case "celery":
            print("Add some raisins and make ants on a log.")
        case "cucumber", "watercress":
            print("That would make a good tea sandwich.")
        case let x where x.hasSuffix("pepper"):
            print("Is it a spicy \(x)?")
        default:
            print("Everything tastes good in soup.")
    }
    // Prints "Is it a spicy red pepper?"

    Experiment

    Try removing the default case. What error do you get?

    기본 케이스를 제거해 보세요. 어떤 오류가 발생합니까?

    Notice how let can be used in a pattern to assign the value that matched the pattern to a constant.

    After executing the code inside the switch case that matched, the program exits from the switch statement. Execution doesn’t continue to the next case, so you don’t need to explicitly break out of the switch at the end of each case’s code.

    You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.

    패턴과 일치하는 값을 상수에 할당하기 위해 패턴에서 let을 어떻게 사용할 수 있는지 확인하세요.

    일치하는 스위치 케이스 내에서 코드를 실행한 후 프로그램은 스위치 문에서 종료됩니다. 실행은 다음 케이스로 계속되지 않으므로 각 케이스의 코드 끝에서 스위치를 명시적으로 중단할 필요가 없습니다.

    for-in을 사용하면 각 키-값 쌍에 사용할 이름 쌍을 제공하여 사전의 항목을 반복할 수 있습니다. 사전은 순서가 지정되지 않은 컬렉션이므로 해당 키와 값은 임의의 순서로 반복됩니다.

    let interestingNumbers = [
        "Prime": [2, 3, 5, 7, 11, 13],
        "Fibonacci": [1, 1, 2, 3, 5, 8],
        "Square": [1, 4, 9, 16, 25],
    ]
    var largest = 0
    for (_, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                largest = number
            }
        }
    }
    print(largest)
    // Prints "25"

    Experiment

    Replace the _ with a variable name, and keep track of which kind of number was the largest.

    _를 변수 이름으로 바꾸고 어떤 숫자가 가장 큰지 추적합니다.

    Use while to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.

    조건이 변경될 때까지 코드 블록을 반복하려면 while을 사용합니다. 대신 루프의 조건이 끝에 있을 수 있으므로 루프가 적어도 한 번 실행되도록 할 수 있습니다.

    var n = 2
    while n < 100 {
        n *= 2
    }
    print(n)
    // Prints "128"
    
    var m = 2
    repeat {
        m *= 2
    } while m < 100
    print(m)
    // Prints "128"

    Experiment

    Change the condition from m < 100 to m < 0 to see how while and repeat-while behave differently when the loop condition is already false.

    조건을 m < 100에서 m < 0으로 변경하여 루프 조건이 이미 false인 경우 while과 반복 while이 어떻게 다르게 동작하는지 확인하세요.

    You can keep an index in a loop by using ..< to make a range of indexes.

    ..<를 사용하여 인덱스 범위를 만들어 루프에서 인덱스를 유지할 수 있습니다.

    var total = 0
    for i in 0..<4 {
        total += i
    }
    print(total)
    // Prints "6"

    Use ..< to make a range that omits its upper value, and use ... to make a range that includes both values.

    ..<를 사용하면 상위 값을 생략하는 범위를 만들 수 있고, ...를 사용하면 두 값을 모두 포함하는 범위를 만들 수 있습니다.

    Functions and Closures

    Use func to declare a function. Call a function by following its name with a list of arguments in parentheses. Use -> to separate the parameter names and types from the function’s return type.

    함수를 선언하려면 func를 사용하세요. 괄호 안의 인수 목록과 함께 이름 뒤에 함수를 호출합니다. 매개변수 이름과 유형을 함수의 반환 유형에서 분리하려면 ->를 사용하세요.

    func greet(person: String, day: String) -> String {
        return "Hello \(person), today is \(day)."
    }
    greet(person: "Bob", day: "Tuesday")

    Experiment

    Remove the day parameter. Add a parameter to include today’s lunch special in the greeting.

    일 매개변수를 제거합니다. 인사말에 오늘의 점심 특선 요리를 포함하려면 매개변수를 추가하세요.

    By default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write _ to use no argument label.

    기본적으로 함수는 매개변수 이름을 인수의 레이블로 사용합니다. 매개변수 이름 앞에 사용자 정의 인수 레이블을 쓰거나, 인수 레이블을 사용하지 않으려면 _를 씁니다.

    func greet(_ person: String, on day: String) -> String {
        return "Hello \(person), today is \(day)."
    }
    greet("John", on: "Wednesday")

    Use a tuple to make a compound value — for example, to return multiple values from a function. The elements of a tuple can be referred to either by name or by number.

    튜플을 사용하여 복합 값을 만듭니다. 예를 들어 함수에서 여러 값을 반환하는 경우입니다. 튜플의 요소는 이름이나 번호로 참조될 수 있습니다.

    func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
        var min = scores[0]
        var max = scores[0]
        var sum = 0
    
        for score in scores {
            if score > max {
                max = score
            } else if score < min {
                min = score
            }
            sum += score
        }
        return (min, max, sum)
    }
    let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
    print(statistics.sum)
    // Prints "120"
    print(statistics.2)
    // Prints "120"

    Functions can be nested. Nested functions have access to variables that were declared in the outer function. You can use nested functions to organize the code in a function that’s long or complex.

    함수는 중첩될 수 있습니다. 중첩 함수는 외부 함수에 선언된 변수에 액세스할 수 있습니다. 중첩된 함수를 사용하여 길거나 복잡한 함수의 코드를 구성할 수 있습니다.

    func returnFifteen() -> Int {
        var y = 10
        func add() {
            y += 5
        }
        add()
        return y
    }
    returnFifteen()

    Functions are a first-class type. This means that a function can return another function as its value.

    함수는 일급 유형입니다. 이는 함수가 다른 함수를 값으로 반환할 수 있음을 의미합니다.

    func makeIncrementer() -> ((Int) -> Int) {
        func addOne(number: Int) -> Int {
            return 1 + number
        }
        return addOne
    }
    var increment = makeIncrementer()
    increment(7)

    A function can take another function as one of its arguments.

    함수는 다른 함수를 인수 중 하나로 사용할 수 있습니다.

    func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
        for item in list {
            if condition(item) {
                return true
            }
        }
        return false
    }
    func lessThanTen(number: Int) -> Bool {
        return number < 10
    }
    var numbers = [20, 19, 7, 12]
    hasAnyMatches(list: numbers, condition: lessThanTen)

    Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it’s executed — you saw an example of this already with nested functions. You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.

    함수는 실제로 클로저의 특별한 경우입니다. 나중에 호출할 수 있는 코드 블록입니다. 클로저의 코드는 클로저가 실행될 때 다른 범위에 있더라도 클로저가 생성된 범위에서 사용 가능한 변수 및 함수와 같은 것에 액세스할 수 있습니다. 중첩된 함수가 있는 예를 이미 본 적이 있습니다. 코드를 중괄호({})로 묶어서 이름 없이 클로저를 작성할 수 있습니다. in을 사용하여 인수와 반환 유형을 본문에서 분리합니다.

    numbers.map({ (number: Int) -> Int in
        let result = 3 * number
        return result
    })

    Experiment

    Rewrite the closure to return zero for all odd numbers.

    모든 홀수에 대해 0을 반환하도록 클로저를 다시 작성하세요.

    You have several options for writing closures more concisely. When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.

    클로저를 더 간결하게 작성하기 위한 몇 가지 옵션이 있습니다. 대리자에 대한 콜백과 같이 클로저의 유형이 이미 알려진 경우 매개변수 유형, 반환 유형 또는 둘 다를 생략할 수 있습니다. 단일 명령문 클로저는 암시적으로 유일한 명령문의 값을 반환합니다.

    let mappedNumbers = numbers.map({ number in 3 * number })
    print(mappedNumbers)
    // Prints "[60, 57, 21, 36]"

    You can refer to parameters by number instead of by name — this approach is especially useful in very short closures. A closure passed as the last argument to a function can appear immediately after the parentheses. When a closure is the only argument to a function, you can omit the parentheses entirely.

    이름 대신 숫자로 매개변수를 참조할 수 있습니다. 이 접근 방식은 특히 매우 짧은 클로저에 유용합니다. 함수의 마지막 인자로 전달된 클로저는 괄호 바로 뒤에 나타날 수 있습니다. 클로저가 함수의 유일한 인수인 경우 괄호를 완전히 생략할 수 있습니다.

    let sortedNumbers = numbers.sorted { $0 > $1 }
    print(sortedNumbers)
    // Prints "[20, 19, 12, 7]"

    Objects and Classes

    Use class followed by the class’s name to create a class. A property declaration in a class is written the same way as a constant or variable declaration, except that it’s in the context of a class. Likewise, method and function declarations are written the same way.

    클래스를 생성하려면 클래스 뒤에 클래스 이름을 사용하세요. 클래스의 속성 선언은 클래스 컨텍스트에 있다는 점을 제외하면 상수 또는 변수 선언과 동일한 방식으로 작성됩니다. 마찬가지로 메서드와 함수 선언도 같은 방식으로 작성됩니다.

    class Shape {
        var numberOfSides = 0
        func simpleDescription() -> String {
            return "A shape with \(numberOfSides) sides."
        }
    }

    Experiment

    Add a constant property with let, and add another method that takes an argument.

    let을 사용하여 상수 속성을 추가하고 인수를 사용하는 다른 메서드를 추가합니다.

    Create an instance of a class by putting parentheses after the class name. Use dot syntax to access the properties and methods of the instance.

    클래스 이름 뒤에 괄호를 넣어 클래스의 인스턴스를 만듭니다. 인스턴스의 속성과 메서드에 액세스하려면 점 구문을 사용하세요.

    var shape = Shape()
    shape.numberOfSides = 7
    var shapeDescription = shape.simpleDescription()

    This version of the Shape class is missing something important: an initializer to set up the class when an instance is created. Use init to create one.

    이 버전의 Shape 클래스에는 중요한 사항, 즉 인스턴스가 생성될 때 클래스를 설정하는 초기화 프로그램이 누락되어 있습니다. init를 사용하여 하나를 생성합니다.

    class NamedShape {
        var numberOfSides: Int = 0
        var name: String
    
        init(name: String) {
            self.name = name
        }
    
        func simpleDescription() -> String {
            return "A shape with \(numberOfSides) sides."
        }
    }

    Notice how self is used to distinguish the name property from the name argument to the initializer. The arguments to the initializer are passed like a function call when you create an instance of the class. Every property needs a value assigned — either in its declaration (as with numberOfSides) or in the initializer (as with name).

    Use deinit to create a deinitializer if you need to perform some cleanup before the object is deallocated.

    Subclasses include their superclass name after their class name, separated by a colon. There’s no requirement for classes to subclass any standard root class, so you can include or omit a superclass as needed.

    Methods on a subclass that override the superclass’s implementation are marked with override — overriding a method by accident, without override, is detected by the compiler as an error. The compiler also detects methods with override that don’t actually override any method in the superclass.

    name 속성과 초기화 프로그램의 name 인수를 구별하기 위해 self가 어떻게 사용되는지 주목하세요. 초기화에 대한 인수는 클래스의 인스턴스를 생성할 때 함수 호출처럼 전달됩니다. 모든 속성에는 선언(numberOfSides와 같이)이나 초기화 프로그램(name과 같이)에서 할당된 값이 필요합니다.

    객체 할당이 취소되기 전에 정리 작업을 수행해야 하는 경우 deinit를 사용하여 초기화 해제 프로그램을 생성하세요.

    하위 클래스에는 클래스 이름 뒤에 콜론으로 구분된 슈퍼클래스 이름이 포함됩니다. 클래스가 표준 루트 클래스를 하위 클래스화할 필요가 없으므로 필요에 따라 슈퍼클래스를 포함하거나 생략할 수 있습니다.

    슈퍼클래스의 구현을 재정의하는 하위 클래스의 메서드는 override로 표시됩니다. 즉, 재정의 없이 우연히 메서드를 재정의하는 것은 컴파일러에서 오류로 감지됩니다. 컴파일러는 실제로 슈퍼클래스의 어떤 메서드도 재정의하지 않는 재정의가 있는 메서드도 감지합니다.

    class Square: NamedShape {
        var sideLength: Double
    
        init(sideLength: Double, name: String) {
            self.sideLength = sideLength
            super.init(name: name)
            numberOfSides = 4
        }
    
        func area() -> Double {
            return sideLength * sideLength
        }
    
        override func simpleDescription() -> String {
            return "A square with sides of length \(sideLength)."
        }
    }
    let test = Square(sideLength: 5.2, name: "my test square")
    test.area()
    test.simpleDescription()

    Experiment

    Make another subclass of NamedShape called Circle that takes a radius and a name as arguments to its initializer. Implement an area() and a simpleDescription() method on the Circle class.

    반경과 이름을 초기화 프로그램에 대한 인수로 사용하는 Circle이라는 NamedShape의 또 다른 하위 클래스를 만듭니다. Circle 클래스에 area() 및 simpleDescription() 메서드를 구현합니다.

    In addition to simple properties that are stored, properties can have a getter and a setter.

    저장된 단순 속성 외에도 속성에는 getter 및 setter가 있을 수 있습니다.

    class EquilateralTriangle: NamedShape {
        var sideLength: Double = 0.0
    
        init(sideLength: Double, name: String) {
            self.sideLength = sideLength
            super.init(name: name)
            numberOfSides = 3
        }
    
        var perimeter: Double {
            get {
                return 3.0 * sideLength
            }
            set {
                sideLength = newValue / 3.0
            }
        }
    
        override func simpleDescription() -> String {
            return "An equilateral triangle with sides of length \(sideLength)."
        }
    }
    var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
    print(triangle.perimeter)
    // Prints "9.3"
    triangle.perimeter = 9.9
    print(triangle.sideLength)
    // Prints "3.3000000000000003"

    In the setter for perimeter, the new value has the implicit name newValue. You can provide an explicit name in parentheses after set.

    Notice that the initializer for the EquilateralTriangle class has three different steps:

    Setting the value of properties that the subclass declares.

    Calling the superclass’s initializer.

    Changing the value of properties defined by the superclass. Any additional setup work that uses methods, getters, or setters can also be done at this point.

    If you don’t need to compute the property but still need to provide code that’s run before and after setting a new value, use willSet and didSet. The code you provide is run any time the value changes outside of an initializer. For example, the class below ensures that the side length of its triangle is always the same as the side length of its square.

    경계 설정자에서 새 값의 암시적 이름은 newValue입니다. 설정 후 괄호 안에 명시적인 이름을 제공할 수 있습니다.

    EquitralTriangle 클래스의 초기화 프로그램에는 세 가지 다른 단계가 있습니다.

    하위 클래스가 선언하는 속성 값을 설정합니다.

    슈퍼클래스의 초기화 프로그램을 호출합니다.

    슈퍼클래스에 의해 정의된 속성 값을 변경합니다. 메소드, getter 또는 setter를 사용하는 추가 설정 작업도 이 시점에서 수행할 수 있습니다.

    속성을 계산할 필요는 없지만 새 값 설정 전후에 실행되는 코드를 제공해야 하는 경우 willSet 및 didSet을 사용하세요. 제공한 코드는 초기화 외부에서 값이 변경될 때마다 실행됩니다. 예를 들어, 아래 클래스는 삼각형의 변 길이가 항상 정사각형의 변 길이와 동일하도록 보장합니다.

    class TriangleAndSquare {
        var triangle: EquilateralTriangle {
            willSet {
                square.sideLength = newValue.sideLength
            }
        }
        var square: Square {
            willSet {
                triangle.sideLength = newValue.sideLength
            }
        }
        init(size: Double, name: String) {
            square = Square(sideLength: size, name: name)
            triangle = EquilateralTriangle(sideLength: size, name: name)
        }
    }
    var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
    print(triangleAndSquare.square.sideLength)
    // Prints "10.0"
    print(triangleAndSquare.triangle.sideLength)
    // Prints "10.0"
    triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
    print(triangleAndSquare.triangle.sideLength)
    // Prints "50.0"

    When working with optional values, you can write ? before operations like methods, properties, and subscripting. If the value before the ? is nil, everything after the ? is ignored and the value of the whole expression is nil. Otherwise, the optional value is unwrapped, and everything after the ? acts on the unwrapped value. In both cases, the value of the whole expression is an optional value.

    선택적 값으로 작업할 때 메서드, 속성, 첨자 같은 작업 이전에 ?를 쓸 수 있습니다. ? 이전의 값이 nil이라면 ? 이후의 모든 것은 무시되고 전체 표현식의 값은 nil입니다. 그렇지 않으면 선택적 값이 래핑 해제되고 ? 이후의 모든 것이 래핑 해제된 값에 작용합니다. 두 경우 모두 전체 표현식의 값은 선택적 값입니다.

    let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
    let sideLength = optionalSquare?.sideLength

    Enumerations and Structures

    Use enum to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them.

    열거형을 만들려면 enum을 사용하세요. 클래스 및 기타 모든 명명된 유형과 마찬가지로 열거형에는 연관된 메서드가 있을 수 있습니다.

    enum Rank: Int {
        case ace = 1
        case two, three, four, five, six, seven, eight, nine, ten
        case jack, queen, king
    
        func simpleDescription() -> String {
            switch self {
                case .ace:
                return "ace"
                case .jack:
                return "jack"
                case .queen:
                return "queen"
                case .king:
                return "king"
                default:
                return String(self.rawValue)
            }
        }
    }
    let ace = Rank.ace
    let aceRawValue = ace.rawValue

    Experiment

    Write a function that compares two Rank values by comparing their raw values.

    두 개의 Rank 값을 원시 값을 비교하여 비교하는 함수를 작성하세요.

    By default, Swift assigns the raw values starting at zero and incrementing by one each time, but you can change this behavior by explicitly specifying values. In the example above, Ace is explicitly given a raw value of 1, and the rest of the raw values are assigned in order. You can also use strings or floating-point numbers as the raw type of an enumeration. Use the rawValue property to access the raw value of an enumeration case.

    Use the init?(rawValue:) initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil if there’s no matching Rank.

    기본적으로 Swift는 0부터 시작하여 매번 1씩 증가하는 원시 값을 할당하지만 값을 명시적으로 지정하여 이 동작을 변경할 수 있습니다. 위의 예에서 Ace에는 원시 값 1이 명시적으로 지정되고 나머지 원시 값은 순서대로 할당됩니다. 열거형의 원시 유형으로 문자열이나 부동 소수점 숫자를 사용할 수도 있습니다. 열거형 케이스의 원시 값에 액세스하려면 rawValue 속성을 사용합니다.

    init?(rawValue:) 초기화를 사용하여 원시 값에서 열거형 인스턴스를 만듭니다. 원시 값과 일치하는 열거형 케이스를 반환하거나 일치하는 순위가 없으면 nil을 반환합니다.

    if let convertedRank = Rank(rawValue: 3) {
        let threeDescription = convertedRank.simpleDescription()
    }

    The case values of an enumeration are actual values, not just another way of writing their raw values. In fact, in cases where there isn’t a meaningful raw value, you don’t have to provide one.

    열거형의 케이스 값은 원시 값을 작성하는 또 다른 방법이 아니라 실제 값입니다. 실제로 의미 있는 원시 값이 없는 경우에는 값을 제공할 필요가 없습니다.

    enum Suit {
        case spades, hearts, diamonds, clubs
    
        func simpleDescription() -> String {
            switch self {
                case .spades:
                    return "spades"
                case .hearts:
                    return "hearts"
                case .diamonds:
                    return "diamonds"
                case .clubs:
                    return "clubs"
            }
        }
    }
    let hearts = Suit.hearts
    let heartsDescription = hearts.simpleDescription()

    Experiment

    Add a color() method to Suit that returns “black” for spades and clubs, and returns “red” for hearts and diamonds.

    스페이드와 클럽에 대해 "검은색"을 반환하고 하트와 다이아몬드에 대해 "빨간색"을 반환하는 color() 메서드를 Suit에 추가합니다.

    Notice the two ways that the hearts case of the enumeration is referred to above: When assigning a value to the hearts constant, the enumeration case Suit.hearts is referred to by its full name because the constant doesn’t have an explicit type specified. Inside the switch, the enumeration case is referred to by the abbreviated form .hearts because the value of self is already known to be a suit. You can use the abbreviated form anytime the value’s type is already known.

    If an enumeration has raw values, those values are determined as part of the declaration, which means every instance of a particular enumeration case always has the same raw value. Another choice for enumeration cases is to have values associated with the case — these values are determined when you make the instance, and they can be different for each instance of an enumeration case. You can think of the associated values as behaving like stored properties of the enumeration case instance. For example, consider the case of requesting the sunrise and sunset times from a server. The server either responds with the requested information, or it responds with a description of what went wrong.

    위에서 열거형의 하트 케이스가 참조되는 두 가지 방식에 주목하세요. 하트 상수에 값을 할당할 때, 열거형 케이스인 Suit.hearts는 상수에 명시적인 유형이 지정되어 있지 않기 때문에 전체 이름으로 참조됩니다. 스위치 내에서 열거형 케이스는 self 값이 이미 슈트로 알려져 있으므로 축약형 .hearts로 참조됩니다. 값의 유형이 이미 알려져 있으면 언제든지 축약된 형식을 사용할 수 있습니다.

    열거형에 원시 값이 있는 경우 해당 값은 선언의 일부로 결정됩니다. 즉, 특정 열거형 사례의 모든 인스턴스는 항상 동일한 원시 값을 갖습니다. 열거 케이스의 또 다른 선택은 케이스와 연관된 값을 갖는 것입니다. 이러한 값은 인스턴스를 만들 때 결정되며 열거 케이스의 각 인스턴스마다 다를 수 있습니다. 연관된 값이 열거 케이스 인스턴스의 저장된 속성처럼 동작하는 것으로 생각할 수 있습니다. 예를 들어, 서버에서 일출 및 일몰 시간을 요청하는 경우를 생각해 보세요. 서버는 요청된 정보로 응답하거나 무엇이 잘못되었는지에 대한 설명으로 응답합니다.

    enum ServerResponse {
        case result(String, String)
        case failure(String)
    }
    
    let success = ServerResponse.result("6:00 am", "8:09 pm")
    let failure = ServerResponse.failure("Out of cheese.")
    
    switch success {
        case let .result(sunrise, sunset):
            print("Sunrise is at \(sunrise) and sunset is at \(sunset).")
        case let .failure(message):
            print("Failure...  \(message)")
    }
    // Prints "Sunrise is at 6:00 am and sunset is at 8:09 pm."

    Experiment

    Add a third case to ServerResponse and to the switch.

    ServerResponse 및 스위치에 세 번째 사례를 추가합니다.

    Notice how the sunrise and sunset times are extracted from the ServerResponse value as part of matching the value against the switch cases.

    Use struct to create a structure. Structures support many of the same behaviors as classes, including methods and initializers. One of the most important differences between structures and classes is that structures are always copied when they’re passed around in your code, but classes are passed by reference.

    스위치 케이스와 값을 일치시키는 과정의 일부로 ServerResponse 값에서 일출 및 일몰 시간이 어떻게 추출되는지 확인하세요.

    구조체를 생성하려면 struct를 사용하세요. 구조는 메서드 및 초기화 프로그램을 포함하여 클래스와 동일한 많은 동작을 지원합니다. 구조와 클래스의 가장 중요한 차이점 중 하나는 구조가 코드에서 전달될 때 항상 복사되지만 클래스는 참조로 전달된다는 것입니다.

    struct Card {
        var rank: Rank
        var suit: Suit
        func simpleDescription() -> String {
            return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
        }
    }
    let threeOfSpades = Card(rank: .three, suit: .spades)
    let threeOfSpadesDescription = threeOfSpades.simpleDescription()

    Experiment

    Write a function that returns an array containing a full deck of cards, with one card of each combination of rank and suit.

    각 등급과 모양 조합의 카드 한 장이 포함된 전체 카드 덱이 포함된 배열을 반환하는 함수를 작성하세요.

    Concurrency

    Use async to mark a function that runs asynchronously.

    비동기적으로 실행되는 함수를 표시하려면 async를 사용하십시오.

    func fetchUserID(from server: String) async -> Int {
        if server == "primary" {
            return 97
        }
        return 501
    }

    You mark a call to an asynchronous function by writing await in front of it.

    비동기 함수 앞에 await를 작성하여 호출을 표시합니다.

    func fetchUsername(from server: String) async -> String {
        let userID = await fetchUserID(from: server)
        if userID == 501 {
            return "John Appleseed"
        }
        return "Guest"
    }

    Use async let to call an asynchronous function, letting it run in parallel with other asynchronous code. When you use the value it returns, write await.

    async let을 사용하면 비동기 함수를 호출하여 다른 비동기 코드와 병렬로 실행할 수 있습니다. 반환된 값을 사용하는 경우 대기를 작성하세요.

    func connectUser(to server: String) async {
        async let userID = fetchUserID(from: server)
        async let username = fetchUsername(from: server)
        let greeting = await "Hello \(username), user ID \(userID)"
        print(greeting)
    }

    Use Task to call asynchronous functions from synchronous code, without waiting for them to return.

    Task를 사용하면 반환될 때까지 기다리지 않고 동기 코드에서 비동기 함수를 호출할 수 있습니다.

    Task {
        await connectUser(to: "primary")
    }
    // Prints "Hello Guest, user ID 97"
    Use task groups to structure concurrent code.
    
    let userIDs = await withTaskGroup(of: Int.self) { group in
        for server in ["primary", "secondary", "development"] {
            group.addTask {
                return await fetchUserID(from: server)
            }
        }
    
        var results: [Int] = []
        for await result in group {
            results.append(result)
        }
        return results
    }

    Actors are similar to classes, except they ensure that different asynchronous functions can safely interact with an instance of the same actor at the same time.

    액터는 다양한 비동기 함수가 동시에 동일한 액터의 인스턴스와 안전하게 상호 작용할 수 있다는 점을 제외하면 클래스와 유사합니다.

    actor ServerConnection {
        var server: String = "primary"
        private var activeUsers: [Int] = []
        func connect() async -> Int {
            let userID = await fetchUserID(from: server)
            // ... communicate with server ...
            activeUsers.append(userID)
            return userID
        }
    }

    When you call a method on an actor or access one of its properties, you mark that code with await to indicate that it might have to wait for other code that’s already running on the actor to finish.

    액터에 대한 메서드를 호출하거나 해당 속성 중 하나에 액세스할 때 해당 코드에 await를 표시하여 해당 액터에서 이미 실행 중인 다른 코드가 완료될 때까지 기다려야 할 수도 있음을 나타냅니다.

    let server = ServerConnection()
    let userID = await server.connect()

    Protocols and Extensions

    Use protocol to declare a protocol.

    프로토콜을 선언하려면 protocol을 사용하세요.

    protocol ExampleProtocol {
        var simpleDescription: String { get }
        mutating func adjust()
    }

    Classes, enumerations, and structures can all adopt protocols.

    클래스, 열거형 및 structure는 모두 프로토콜을 채택할 수 있습니다.

    class SimpleClass: ExampleProtocol {
        var simpleDescription: String = "A very simple class."
        var anotherProperty: Int = 69105
        func adjust() {
            simpleDescription += "  Now 100% adjusted."
        }
    }
    var a = SimpleClass()
    a.adjust()
    let aDescription = a.simpleDescription
    
    struct SimpleStructure: ExampleProtocol {
        var simpleDescription: String = "A simple structure"
        mutating func adjust() {
            simpleDescription += " (adjusted)"
        }
    }
    var b = SimpleStructure()
    b.adjust()
    let bDescription = b.simpleDescription

    Experiment

    Add another requirement to ExampleProtocol. What changes do you need to make to SimpleClass and SimpleStructure so that they still conform to the protocol?

    exampleProtocol에 또 다른 요구 사항을 추가합니다. SimpleClass 및 SimpleStructure가 여전히 프로토콜을 준수하도록 하려면 어떤 변경이 필요합니까?

    Notice the use of the mutating keyword in the declaration of SimpleStructure to mark a method that modifies the structure. The declaration of SimpleClass doesn’t need any of its methods marked as mutating because methods on a class can always modify the class.

    Use extension to add functionality to an existing type, such as new methods and computed properties. You can use an extension to add protocol conformance to a type that’s declared elsewhere, or even to a type that you imported from a library or framework.

    구조를 수정하는 메서드를 표시하기 위해 SimpleStructure 선언에서 mutating 키워드를 사용하는 것에 주목하세요. SimpleClass 선언에는 클래스의 메서드가 항상 클래스를 수정할 수 있으므로 mutating으로 표시된 메서드가 필요하지 않습니다.

    확장을 사용하여 새 메서드 및 계산된 속성과 같은 기존 유형에 기능을 추가합니다. 확장을 사용하여 다른 곳에서 선언된 유형이나 라이브러리나 프레임워크에서 가져온 유형에도 프로토콜 준수를 추가할 수 있습니다.

    extension Int: ExampleProtocol {
        var simpleDescription: String {
            return "The number \(self)"
        }
        mutating func adjust() {
            self += 42
        }
    }
    print(7.simpleDescription)
    // Prints "The number 7"

    Experiment

    Write an extension for the Double type that adds an absoluteValue property.

    AbsoluteValue 속성을 추가하는 Double 유형에 대한 확장을 작성합니다.

    You can use a protocol name just like any other named type — for example, to create a collection of objects that have different types but that all conform to a single protocol. When you work with values whose type is a boxed protocol type, methods outside the protocol definition aren’t available.

    다른 명명된 유형과 마찬가지로 프로토콜 이름을 사용할 수 있습니다. 예를 들어, 다양한 유형을 가지고 있지만 모두 단일 프로토콜을 준수하는 개체 컬렉션을 만드는 경우입니다. 유형이 박스형 프로토콜 유형인 값으로 작업할 때 프로토콜 정의 외부의 메소드를 사용할 수 없습니다.

    let protocolValue: any ExampleProtocol = a
    print(protocolValue.simpleDescription)
    // Prints "A very simple class.  Now 100% adjusted."
    // print(protocolValue.anotherProperty)  // Uncomment to see the error

    Even though the variable protocolValue has a runtime type of SimpleClass, the compiler treats it as the given type of ExampleProtocol. This means that you can’t accidentally access methods or properties that the class implements in addition to its protocol conformance.

    프로토콜 값 변수의 런타임 유형이 SimpleClass인 경우에도 컴파일러는 이를 지정된 유형의 예제프로토콜로 처리합니다. 즉, 프로토콜 준수 외에도 클래스가 구현하는 메서드나 속성에 실수로 액세스할 수 없습니다.

    Error Handling

    You represent errors using any type that adopts the Error protocol.

    Error 프로토콜을 채택하는 모든 유형을 사용하여 오류를 나타냅니다.

    enum PrinterError: Error {
        case outOfPaper
        case noToner
        case onFire
    }

    Use throw to throw an error and throws to mark a function that can throw an error. If you throw an error in a function, the function returns immediately and the code that called the function handles the error.

    오류를 발생시키려면 throw를 사용하고, 오류를 발생시킬 수 있는 함수를 표시하려면 throw를 사용하세요. 함수에서 오류가 발생하면 함수는 즉시 반환되고 함수를 호출한 코드가 오류를 처리합니다.

    func send(job: Int, toPrinter printerName: String) throws -> String {
        if printerName == "Never Has Toner" {
            throw PrinterError.noToner
        }
        return "Job sent"
    }

    There are several ways to handle errors. One way is to use do-catch. Inside the do block, you mark code that can throw an error by writing try in front of it. Inside the catch block, the error is automatically given the name error unless you give it a different name.

    오류를 처리하는 방법에는 여러 가지가 있습니다. 한 가지 방법은 do-catch를 사용하는 것입니다. do 블록 내에서 앞에 try를 작성하여 오류를 던질 수 있는 코드를 표시합니다. catch 블록 내에서 오류에는 다른 이름을 지정하지 않는 한 자동으로 error라는 이름이 지정됩니다.

    do {
        let printerResponse = try send(job: 1040, toPrinter: "Bi Sheng")
        print(printerResponse)
    } catch {
        print(error)
    }
    // Prints "Job sent"

    Experiment

    Change the printer name to "Never Has Toner", so that the send(job:toPrinter:) function throws an error.

    프린터 이름을 "Never Has Toner"로 변경하면 send(job:toPrinter:) 함수에서 오류가 발생합니다.

    You can provide multiple catch blocks that handle specific errors. You write a pattern after catch just as you do after case in a switch.

    특정 오류를 처리하는 여러 catch 블록을 제공할 수 있습니다. 스위치에서 case 뒤에 패턴을 작성하는 것처럼 catch 뒤에 패턴을 작성합니다.

    do {
        let printerResponse = try send(job: 1440, toPrinter: "Gutenberg")
        print(printerResponse)
    } catch PrinterError.onFire {
        print("I'll just put this over here, with the rest of the fire.")
    } catch let printerError as PrinterError {
        print("Printer error: \(printerError).")
    } catch {
        print(error)
    }
    // Prints "Job sent"

    Experiment

    Add code to throw an error inside the do block. What kind of error do you need to throw so that the error is handled by the first catch block? What about the second and third blocks?

    do 블록 내부에 오류를 발생시키는 코드를 추가합니다. 첫 번째 catch 블록에서 오류를 처리하려면 어떤 종류의 오류를 발생시켜야 합니까? 두 번째와 세 번째 블록은 어떻습니까?

    Another way to handle errors is to use try? to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is nil. Otherwise, the result is an optional containing the value that the function returned.

    오류를 처리하는 또 다른 방법은 try?를 사용하여 결과를 선택 사항으로 변환합니다. 함수에서 오류가 발생하면 특정 오류는 삭제되고 결과는 nil입니다. 그렇지 않으면 결과는 함수가 반환한 값을 포함하는 선택 사항입니다.

    let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
    let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")

    Use defer to write a block of code that’s executed after all other code in the function, just before the function returns. The code is executed regardless of whether the function throws an error. You can use defer to write setup and cleanup code next to each other, even though they need to be executed at different times.

    함수가 반환되기 직전, 함수의 다른 모든 코드 이후에 실행되는 코드 블록을 작성하려면 defer를 사용하세요. 함수에서 오류가 발생하는지 여부에 관계없이 코드가 실행됩니다. 서로 다른 시간에 실행되어야 하는 경우에도 defer를 사용하여 설정 및 정리 코드를 나란히 작성할 수 있습니다.

    var fridgeIsOpen = false
    let fridgeContent = ["milk", "eggs", "leftovers"]
    
    func fridgeContains(_ food: String) -> Bool {
        fridgeIsOpen = true
        defer {
            fridgeIsOpen = false
        }
    
        let result = fridgeContent.contains(food)
        return result
    }
    if fridgeContains("banana") {
        print("Found a banana")
    }
    print(fridgeIsOpen)
    // Prints "false"

    Generics

    Write a name inside angle brackets to make a generic function or type.

    일반 함수나 유형을 만들려면 꺾쇠 괄호 안에 이름을 쓰세요.

    func makeArray<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
        var result: [Item] = []
        for _ in 0..<numberOfTimes {
            result.append(item)
        }
        return result
    }
    makeArray(repeating: "knock", numberOfTimes: 4)

    You can make generic forms of functions and methods, as well as classes, enumerations, and structures.

    클래스, 열거형 및 구조뿐만 아니라 일반적인 형태의 함수 및 메서드를 만들 수 있습니다.

    // Reimplement the Swift standard library's optional type
    enum OptionalValue<Wrapped> {
        case none
        case some(Wrapped)
    }
    var possibleInteger: OptionalValue<Int> = .none
    possibleInteger = .some(100)

    Use where right before the body to specify a list of requirements — for example, to require the type to implement a protocol, to require two types to be the same, or to require a class to have a particular superclass.

    요구사항 목록을 지정하려면 본문 바로 앞에 where를 사용하세요. 예를 들어 프로토콜을 구현하기 위해 유형을 요구하거나, 두 유형이 동일하도록 요구하거나, 클래스가 특정 슈퍼클래스를 갖도록 요구하는 경우입니다.

    func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool
    where T.Element: Equatable, T.Element == U.Element
    {
        for lhsItem in lhs {
            for rhsItem in rhs {
                if lhsItem == rhsItem {
                    return true
                }
            }
        }
        return false
    }
    anyCommonElements([1, 2, 3], [3])

    Experiment

    Modify the anyCommonElements(_:_:) function to make a function that returns an array of the elements that any two sequences have in common.

    두 시퀀스가 공통으로 갖는 요소의 배열을 반환하는 함수를 만들려면 anyCommonElements(_:_:) 함수를 수정하세요.

    Writing <T: Equatable> is the same as writing <T> ... where T: Equatable.

    <T: Equatable>을 쓰는 것은 <T> ... where T: Equatable을 쓰는 것과 같습니다.

    728x90

    '코딩 > Swift' 카테고리의 다른 글

    Swift 정리 #9 구조체와 클래스  (0) 2022.12.28
    Swift 정리 #8 Enumerations  (0) 2022.12.28
    Swift 정리 #7 클로저  (1) 2022.12.14
    Swift 정리 #6 함수  (0) 2022.12.14
    Swift 정리 #5 Control flow  (1) 2022.12.14

    댓글

Designed by Tistory.