For Loop in Apple Swift

前端 未结 5 2060
眼角桃花
眼角桃花 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: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.

提交回复
热议问题