var A : StructureXvar B : StructureX@H_419_1@我知道结构A和B具有相同的Type,即StructureX.但…
let A : (Int,String)let B : (Int,String)@H_419_1@A和B元组是否相同? (2)
使用元组而不是结构有什么优势? (3) 我发现将Swift元组概念化为“匿名结构”是最简单的,有一些关键的区别.它们的行为类似,但是一个结构体具有正式的定义,并允许更多的可变性控制,而元组允许模式匹配. @H_419_1@元组和结构之间的相似性 @H_419_1@>两者都可以有任意数量的任何类型的成员,包括关闭
>两者都可以内联构造(请参见下面的代码中的typealias)
>如果声明为常数,则两者均可防止任何成员的突变
>如果一个元组已经标注了成员,那么这两个结构体和元组都允许成员通过标签进行访问 @H_419_1@元组和结构之间的差异 @H_419_1@> Structs需要使用前的定义
> Structs不允许与其成员进行模式匹配
>如果实例是一个变量,结构体允许被声明为变量的成员变异
>元组不允许引用其任何成员的变异功能或功能
元组可能不实现协议
>如果元组具有匿名成员,则其成员可以通过索引访问,而不像结构体 @H_419_1@说明这些差异和相似之处的游乐场的一些代码
// All commented code causes a compilation error. Uncomment to vIEw error messages.@H_419_1@struct StructureX { let a: Int = 0 var b: String = "string" }@H_419_1@// // 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@H_419_1@// // 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@H_419_1@let emptyTuple: () = () // philosophically,isn't this the deFinition of VoID? let single: (Int) = (23) //let namedSingle: (a: Int) = (a: 42)@H_419_1@// // Tuple Labeled Member Access // var labeledTupleA: (a: Int,b: String) = (a: 0,b: "string") labeledTupleA.0 = 5 labeledTupleA.a labeledTupleA@H_419_1@var check: (a: Int,b: String) check = labeledTupleA // same type check@H_419_1@// // 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@H_419_1@// // 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@H_419_1@// // 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@H_419_1@// // 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) })@H_419_1@labeledTupleD.de labeledTupleD.df(1)@H_419_1@// // Tuples allow pattern matching,Structs do not // //switch structureA { //case (let i,let s): // print(i,s) //default: // break //}@H_419_1@switch labeledTupleD { case (_,let closure): closure(123) default: break }总结
以上是内存溢出为你收集整理的Swift元组 – 与结构体和彼此不同?全部内容,希望文章能够帮你解决Swift元组 – 与结构体和彼此不同?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)