欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

转:swift中结构体和类的区别(值类型和引用类型的区别)

程序员文章站 2024-02-20 16:06:52
...

在swift中结构体和类有着更多的相同之处,在一般的使用中能够做到互相替换。我们可以先看看官方文档的描述:

Unlike other programming languages, Swift doesn’t require you to create separate interface and implementation files for custom structures and classes. In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use.

An instance of a class is traditionally known as anobject. However, Swift structures and classes are much closer in functionality than in other languages, and much of this chapter describes functionality that applies to instances ofeithera class or a structure type. Because of this, the more general terminstanceis used.

Comparing Structures and Classes

Structures and classes in Swift have many things in common. Both can:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

简单翻译过来就是:

与其他编程语言不同,Swift不需要为自定义结构体和类创建单独的接口和实现文件。在Swift中,您在单个文件中定义一个结构体或类,该类或结构体的外部接口将自动提供给其他代码使用。

类的实例传统上称为对象。然而,Swift结构体和类在功能上比在其他语言中更接近

比较结构和类

Swift中的结构体和类有很多共同点。都可以:

  • 定义属性来存储值
  • 定义方法来提供功能
  • 定义下标以使用下标语法提供对其值的访问
  • 定义初始化器来设置它们的初始状态
  • 扩展以扩展其功能,使其超出默认实现
  • 遵守协议以提供某种标准功能

结构体和类最大的区别就是结构体是值类型,类是引用类型。

这里提醒一下Swift值类型列表:

  • 结构体
  • 枚举
  • 元组(tuple)
  • 基本类型(Int,Double,Bool等)
  • 集合(Array, String, Dictionary, Set)

引用类型最常用的就是类和闭包。

在 Swift 中,值类型,存放在栈区;引用类型,存放在堆区。但是有些值类型,如字符串或数组,会间接地将项保存在堆中。所以它们是由引用类型支持的值类型。

Swift 中,值类型的赋值为深拷贝(Deep Copy),值语义(Value Semantics)即新对象和源对象是独立的,当改变新对象的属性,源对象不会受到影响,反之同理。

引用类型的赋值是浅拷贝(Shallow Copy),引用语义(Reference Semantics)即新对象和源对象的变量名不同,但其引用(指向的内存空间)是一样的,因此当使用新对象操作其内部数据时,源对象的内部数据也会受到影响。

struct Boy {
    var name = "Lilei"
    var age = 12
}
var boyA = Boy()
print("boyA.name -> \(boyA.name)")
        
var boyB = boyA
boyB.name = "Tom"
print("boyA.name -> \(boyA.name)")

控制台输出结果为:boyA.name -> Lilei
               boyA.name -> Lilei

Boy为结构体的时候更改boyB的name boyA.name 并没有改变

class Boy: NSObject {
    var name = "Lilei"
    var age = 12
}
var boyA = Boy()
print("boyA.name -> \(boyA.name)")
        
var boyB = boyA
boyB.name = "Tom"
print("boyA.name -> \(boyA.name)")
print(Unmanaged.passUnretained(boyA).toOpaque())
print(Unmanaged.passUnretained(boyB).toOpaque())

控制台输出结果为:boyA.name -> Lilei
               boyA.name -> Tom
               0x0000600002e2b9e0
               0x0000600002e2b9e0

Boy为class的时候更改boyB的name boyA也会改变,同时boyA的地址和boyB的地址相同可见在class中拷贝为指针拷贝,从中即可发现,两个变量指向的是同一块内存空间。

在swift4.0中 可以用==来判断两个对象是否是同一个对象

if boyA == boyB {
    print("boyA == boyB")
 }
控制台输出结果为:boyA == boyB

嵌套类型

在使用过程中会存在嵌套的情况

值类型嵌套值类型:

struct Boy {
    var name = "Lilei"
    var age = 12
    var dog = Dog()
    
}

struct Dog {
    var weight = 40
    var name = "littleDog"
    
}
var boyA = Boy()
var boyB = boyA

boyB.name = "Tom"
boyB.dog.name = "bigDog"
boyB.dog.weight = 100
        
print(boyA,boyA.dog)
print(boyB,boyB.dog)

控制台输出:Boy(name: "Lilei", age: 12, dog: swift_test.Dog(weight: 40, name: "littleDog")) Dog(weight: 40, name: "littleDog")
          Boy(name: "Tom", age: 12, dog: swift_test.Dog(weight: 100, name: "bigDog")) Dog(weight: 100, name: "bigDog")


结构体嵌套结构体,很显然赋值时创建了新的变量,两者是独立的,嵌套的值类型变量也会创建新的变量,这两者也是独立的。boyA和boyB 不同,dogA和dogB也不同

值类型嵌套引用类型:

把Dog改成Class

struct Boy {
    var name = "Lilei"
    var age = 12
    var dog = Dog()
}

class Dog:NSObject {
    var weight = 40
    var name = "littleDog"
}
var boyA = Boy()
var boyB = boyA
boyB.name = "Tom"
boyB.dog.name = "bigDog"
boyB.dog.weight = 100
        
print(boyA,boyA.dog,boyA.dog.name)
print(boyB,boyB.dog,boyB.dog.name)
控制台输出结果:Boy(name: "Lilei", age: 12, dog: <swift_test.Dog: 0x600001d110e0>) <swift_test.Dog: 0x600001d110e0> bigDog
             Boy(name: "Tom", age: 12, dog: <swift_test.Dog: 0x600001d110e0>) <swift_test.Dog: 0x600001d110e0> bigDog

可以看出boyB发生了变化,boyB的dog和boyA的dog为同一个对象,值类型嵌套引用类型时,赋值时创建了新的变量,两者是独立的,但嵌套的引用类型指向的是同一块内存空间,当改变值类型内部嵌套的引用类型变量值时(除了重新初始化),其他对象的该属性也会随之改变。

引用类型嵌套值类型:

Boy改成class,Dog为struct

class Boy: NSObject {
    var name = "Lilei"
    var age = 12
    var dog = Dog()
}

struct Dog {
    var weight = 40
    var name = "littleDog"
}
var boyA = Boy()
var boyB = boyA
boyB.name = "Tom"
boyB.dog.name = "bigDog"
boyB.dog.weight = 100
        
print(boyA,boyA.dog,boyA.dog.name)
print(boyB,boyB.dog,boyB.dog.name)
控制台输出:<swift_test.Boy: 0x600003853480> Dog(weight: 100, name: "bigDog") bigDog
          <swift_test.Boy: 0x600003853480> Dog(weight: 100, name: "bigDog") bigDog


引用类型嵌套值类型时,赋值时创建了新的变量,但是新变量和源变量指向同一块内存,因此改变源变量的内部值,会影响到其他变量的值。

引用类型嵌套引用类型:

class Boy: NSObject {
    var name = "Lilei"
    var age = 12
    var dog = Dog()
}

struct Dog {
    var weight = 40
    var name = "littleDog"
}
var boyA = Boy()
var boyB = boyA
boyB.name = "Tom"
boyB.dog.name = "bigDog"
boyB.dog.weight = 100
        
print(boyA,boyA.dog,boyA.dog.name)
print(boyB,boyB.dog,boyB.dog.name)
控制台输出:<swift_test.Boy: 0x6000029c4120> <swift_test.Dog: 0x6000027d0fa0> bigDog
          <swift_test.Boy: 0x6000029c4120> <swift_test.Dog: 0x6000027d0fa0> bigDog

引用类型嵌套引用类型时,赋值时创建了新的变量,但是新变量和源变量指向同一块内存,内部引用类型变量也指向同一块内存地址,改变引用类型嵌套的引用类型的值,也会影响到其他变量的值。

总结:

类和结构体的选择:

  • 该数据结构的主要目的是用来封装少量相关简单数据值;
  • 有理由预计该数据结构的实例在被赋值或传递时,封装的数据将会被拷贝而不是被引用;
  • 该数据结构中储存的值类型属性,也应该被拷贝,而不是被引用;
  • 该数据结构不需要去继承另一个既有类型的属性或者行为。

当有上面的一种情况或多种情况请选择结构体,结构体比类更简单,也就是更轻便
原文链接