For Loop in Apple Swift

前端 未结 5 2042
眼角桃花
眼角桃花 2021-02-14 03:24

Apple\'s newly released language Swift has an example on the official documentation. Example is like this;

let interestingNumbers = [
    \"Prime\": [2, 3, 5, 7,         


        
相关标签:
5条回答
  • 2021-02-14 03:27
    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 (kind, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                largest = number
            }
        }
    }
    largest
    

    This will return pair of (String,Int) which we have in Our Dictionary similar to function return multiple value as below,

       func refreshWebPage() -> (status:String,code:Int){
        //refresh logic
                    return ("Success",200)
        }
    
    0 讨论(0)
  • 2021-02-14 03:30

    I can't seem to figure out what is "(kind,numbers)" here represents

    It's a key-value pair (a tuple) containing the kind of the number. This syntax is called decomposition, basically, inside the loop you can access kind as the kind and numbers as the numbers that map for it.

    For example, in some iteration:

    kind  // "Prime"
    numbers // [2, 3, 5, 7, 11, 13]
    

    Quoting the guide:

    You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a (key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple’s members as explicitly named constants for use within in the body of the for-in loop.

    0 讨论(0)
  • 2021-02-14 03:44

    Swift allows you to loop over a dictionary with tuple-syntax (key, value). So in every iteration of the for-loop Swift cares about reassigning the specified tuple-variables (kind and number in your case) to the actual dictionary-record.

    To figure out which Key includes the highest number in your example you can extend your code as follows:

    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
    var largestKey = ""
    for (kind, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                largest = number
                largestKey = kind
            }
        }
    }
    largest     // =25
    largestKey  // ="Square"
    

    Or if you want to practice the tuple-syntax try that (with the same result):

    var largest = 0
    var largestKey = ""
    for (kind, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                (largestKey, largest) = (kind, number)
            }
        }
    }
    largest     // =25
    largestKey  // ="Square"
    
    0 讨论(0)
  • 2021-02-14 03:47
    for (kind, numbers) in interestingNumbers{}
    

    This for loop actually enumerating the key/value pairs of dictionary interestingNumbers. Where kind is the key and numbers is the correspoding value

    kind:Prime       //Key
    numbers: [2, 3, 5, 7, 11, 13] //Value  
    

    Here the complete solution of the exercise

    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
    var type: String = ""
    for (kind, numbers) in interestingNumbers {
        for number in numbers {
            if number > largest {
                largest = number
                type = kind
            }
        }
    }
    largest
    type
    
    0 讨论(0)
  • 2021-02-14 03:51

    However, I can't seem to figure out what is "(kind,numbers)" here represent

    The loop iterates over the dictionary, and every iteration gives you a key and associated value. Those are called kind (key) and numbers (value) here. You can choose any name you want.

    and how should I make my for-loop to go through all Dictionary(interestingNumbers) keys and find which key has the largest number.

    You get each key in turn in the kind loop variable.

    Once you find one that results in a new largest, you can assign that to a result variable, say largestKind.

    At the end of the loop, largestKind will contain the key of the array with the largest number (that number being the largest you already have).

    0 讨论(0)
提交回复
热议问题