swift 遍历

最简单的一个遍历数组

升级

上面的看不懂的话,这个应该会简单点

import UIKit
let interestingNumbers = [
    "prime": [2,3,5,6,11,13],
    "prime1": [1,1,2,3,5,8],
    "prime2": [1,4,9,16,25],
]
var largest = 0
for (kind,numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
print(largest)

//////////////////////////
let hehe = [1:[22,33],2:[12,32]]
var xx = 0
for (name,array) in hehe {
    for number in array {
        print(number)
    }
}
////////////////////////

var intArray = [1,23,4,5]
for int in intArray {
    print(int)
}
时间: 2024-10-18 15:42:31

swift 遍历的相关文章

[IOS]Swift 遍历预制的本地资源文件

我事先放了一堆svg文件,但是我是批量使用的,想要直接遍历他们加入到一个list中来,那我直接就遍历他们的名称,把他们的名字组成一个array. var ss:NSString = NSBundle.mainBundle().resourcePath!//6.0.1修改了要求感叹号 println(ss) var nsfilemange = NSFileManager.defaultManager() var filelist=NSArray.alloc() filelist=nsfileman

swift 遍历枚举

? // see at?http://swifter.tips/enum-enumerate/ // 貌似有些空格在粘贴的时候没有了... ? ?= =! import Foundation ? enum Suit: String { ? ? case Spades = "黑桃" ? ? case Hearts = "红桃" ? ? case Clubs = "草花" ? ? case Diamonds = "方片" } ?

Swift中实现Array数组和NSArray数组的相互转换与遍历

Array是Swift中的数组数据类型.而NSArray是OC中的数组数据类型.两者有差别有联系.在Swift中有时候难免会使用到OC中的一些东西.今天我们就来Swift中使用NSArray和Array,而且进行转化. (1)声明一个Array数组.并进行遍历.代码例如以下: let stringArray: Array<String> = ["10", "20","30","40","50"]

swift基本用法-for循环遍历,遍历字典,循环生成数组

// Playground - noun: a place where people can play import UIKit //------------------------------------------------------------------------------ // 1. for // 传统的for循环方式在swift中同样支持 var num = 0 for(var i = 0; i < 10 ; i++) { num += i } num //---------

swift-07-使用for-in 遍历数组

//for-in /* for 迭代变量 in集合变量 { 使用迭代变量便利所有数据 } */ //遍历数组 var arr = ["a" ,"b" ,"c" ,"d"] for tempin arr { print(temp) print("\t") } // var array:[(String,Int,String)] = [("张三",20,"男"),(&qu

[Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

[Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has