Swift元组 – 与结构体和彼此不同?

前端之家收集整理的这篇文章主要介绍了Swift元组 – 与结构体和彼此不同?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
结构中的元组有多么不同? (1)

据了解,元组和结构都可以通过值发送,而不是通过引用在函数调用中返回,对吗?

另外,我知道如果有的话

var A : StructureX
var B : StructureX

我知道结构A和B具有相同的Type,即StructureX.但…

let A : (Int,String)
let B : (Int,String)

A和B元组是否相同? (2)
使用元组而不是结构有什么优势? (3)

我发现将Swift元组概念化为“匿名结构”是最简单的,有一些关键的区别.它们的行为类似,但是一个结构体具有正式的定义,并允许更多的可变性控制,而元组允许模式匹配.

元组和结构之间的相似性

>两者都可以有任意数量的任何类型的成员,包括关闭
>两者都可以内联构造(请参见下面的代码中的typealias)
>如果声明为常数,则两者均可防止任何成员的突变
>如果一个元组已经标注了成员,那么这两个结构体和元组都允许成员通过标签进行访问

元组和结构之间的差异

> Structs需要使用前的定义
> Structs不允许与其成员进行模式匹配
>如果实例是一个变量,结构体允许被声明为变量的成员变异
>元组不允许引用其任何成员的变异功能功能
元组可能不实现协议
>如果元组具有匿名成员,则其成员可以通过索引访问,而不像结构体

说明这些差异和相似之处的游乐场的一些代码

// All commented code causes a compilation error. Uncomment to view error messages.

struct StructureX { let a: Int = 0 var b: String = "string" }

// // Struct member variability // var structureA: StructureX = StructureX() let structureB: StructureX = StructureX() //structureA.a = 2 // declared as a constant,instance is variable structureA.b = "allowed" // declared as a variable,instance is variable //structureB.a = 2 // declared as constant,instance is constant //structureB.b = "not allowed" // declared as constant,instance is constant structureA = structureB // these are the same type structureA

// // Typealias a labeled tuple and it can be constructed similarly to a struct // typealias StructureT = (a: Int,b: String) var structureD: StructureT = StructureT(a: 0,b: "asdf") structureD //structureD = structureA // but they are distinct types

let emptyTuple: () = () // philosophically,isn't this the definition of Void? let single: (Int) = (23) //let namedSingle: (a: Int) = (a: 42)

// // Tuple Labeled Member Access // var labeledTupleA: (a: Int,b: String) = (a: 0,b: "string") labeledTupleA.0 = 5 labeledTupleA.a labeledTupleA

var check: (a: Int,b: String) check = labeledTupleA // same type check

// // Tuples can have functions/closures // let labeledTupleB: (Int,String,fun: () -> Void) = (0,"string",{ () -> Void in println("hi") }) labeledTupleB.1 labeledTupleB.fun() //labeledTupleB.0 = 10 // this tuple is a constant,so all of its members are constant

// // Tuples with members of the same type,but differet labels are not of the same type // var labeledTupleC: (c: Int,d: String) = (c: -1,d: "fail") //labeledTupleC = labeledTupleA //labeledTupleC = labeledTupleB

// // Tuples with anonymous members matching the type pattern of a labeled member tuple are of equivalent type // var unlabeledTuple: (Int,String) = (0,"good") unlabeledTuple = labeledTupleA unlabeledTuple = labeledTupleC

// // Tuples with closures may not refer to sibling members // var labeledTupleD: (de: Int,df: (Int) -> Void) = (de: 0,df: { (num: Int) -> Void in //de += num //self.de += num println(num) })

labeledTupleD.de labeledTupleD.df(1)

// // Tuples allow pattern matching,Structs do not // //switch structureA { //case (let i,let s): // print(i,s) //default: // break //}

switch labeledTupleD { case (_,let closure): closure(123) default: break }

原文链接:https://www.f2er.com/swift/319643.html

猜你在找的Swift相关文章