Home Backend Development Python Tutorial Summary of some new changes in collection data structures in Swift 3.0

Summary of some new changes in collection data structures in Swift 3.0

Aug 04, 2016 am 08:55 AM
set swift dictionary array gather

1. Changes to Array

The modified API example in array array is as follows:

//创建大量相同元素的数组
//创建有10个String类型元素的数组,并且每个元素都为字符串"Hello"
//swift2.2
//var array3 = [String](count: 10, repeatedValue: "Hello")
//swift3.0
var array3 = [String](repeating: "Hello", count: 10)
//创建有10个Int类型元素的数组,且每个元素都为1
//swift2.2
//var array4 = Array(count: 10, repeatedValue: 1)
//swift3.0
var array4 = Array(repeating: 1, count: 10)

var array = [1,2,3,4,5,6,7,8,9]
//向数组中追加一组元素
//swift2.2
//array.appendContentsOf([11,12,13])
//swift3.0
array.append(contentsOf: [11,12,13])
//向数组中的某个位置插入一个元素
//swift2.2
//array.insert(0, atIndex: 0)
//swift3.0
array.insert(0, at: 0)
//向数组中的某个位置插入一组元素
//swift2.2
//array.insertContentsOf([-2,-1], at: 0)
//swift3.0
array.insert(contentsOf: [-2,-1], at: 0)
//移除数组中某个位置的元素
//swift2.2
//array.removeAtIndex(1)
//swift3.0
array.remove(at: 1)
//移除一个范围内的元素
//swift2.2
//array.removeRange(0...2)
//swift3.0
array.removeSubrange(0...2)
//修改一个范围内的元素
//swift2.2
//array.replaceRange(0...2, with: [0,1])
//swift3.0
array.replaceSubrange(0...2, with: [0,1])
//进行数组枚举遍历 将输出 (0,0) (1,1) (2,2) (3,3) (4,4)
//swift3.0 中将枚举属性enumerate 修改为enumerated()方法
for item in arrayLet.enumerated(){
 print(item)
}
var arraySort = [1,3,5,6,7]
//获取数组中的最大值
//swift2.2
//arraySort.maxElement()
//swift3.0
arraySort.max()
//获取数组中的最小值
//swift2.2
//arraySort.minElement()
//swift3.0
arraySort.min()
//从大到小排序
//swift2.2
//arraySort = arraySort.sort(>)
//swift3.0
arraySort = arraySort.sorted(isOrderedBefore: >)
//从小到大排序
//swift2.2
//arraySort = arraySort.sort(<)
//swift3.0
arraySort = arraySort.sorted(isOrderedBefore: <)


Copy after login

2. Changes in Set collection

The modification example in the Set collection is as follows:

//创建set集合
var set1:Set<Int> = [1,2,3,4]
//进行下标的移动
//获取某个下标后一个元素
//swlft2.2
//set1[set1.startIndex.successor()]
//swift3.0
set1[set1.index(after: set1.startIndex)]
//获取某个下标后几位的元素
//swift2.2
//set1[set1.startIndex.advancedBy(3)]
//swift3.0
set1[set1.index(set1.startIndex, offsetBy: 3)]
//获取集合中的最大值
//swift2.2
//set1.maxElement()
//swift3.0
set1.max()
//获取集合中的最小值
//swift2.2
//set1.minElement()
//swift3.0
set1.min()
//移除集合中某个位置的元素
//swift2.2
//set1.removeAtIndex(set1.indexOf(3)!)
//swift3.0
set1.remove(at: set1.index(of: 3)!)
var set3:Set<Int> = [1,2,3,4]
var set4:Set<Int> = [1,2,5,6]
//返回交集 {1,2}
//swift2.2
//var setInter = set3.intersect(set4)
//swift3.0
var setInter = set3.intersection(set4)
//返回交集的补集{3,4,5,6}
//swift2.2
//var setEx = set3.exclusiveOr(set4)
//swift3.0
var setEx = set3.symmetricDifference(set4)

var set5:Set = [1,2]
var set6:Set = [2,3]
var set7:Set = [1,2,3]
var set8:Set = [1,2,3]
//判断是否是某个集合的子集 set5是set7的子集 返回ture
//swift2.2
//set5.isSubsetOf(set7)
//swift3.0
set5.isSubset(of: set7)
//判断是否是某个集合的超集 set7是set5的超集 返回ture
//swift2.2
//set7.isSupersetOf(set5)
//swift3.0
set7.isSuperset(of: set5)
//判断是否是某个集合的真子集 set5是set7的真子集 返回ture
//swift2.2
//set5.isStrictSubsetOf(set7)
//swift3.0
set5.isStrictSubset(of: set7)
//判断是否是某个集合的真超集 set7不是set8的真超集 返回false
//swift2.2
//set7.isStrictSupersetOf(set8)
//swift3.0
set7.isStrictSuperset(of: set8)


Copy after login

3. Changes in Dictionary

The modification example in the Dictionary dictionary is as follows:

//通过键删除某个键值对
//swift2.2
//dic1.removeValueForKey(1)
//swift3.0
dic1.removeValue(forKey: 1)

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Jul 31, 2024 pm 01:10 PM

According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

See all articles