Ios 在Swift中替换数组值后,数组值会发生什么变化?

Ios 在Swift中替换数组值后,数组值会发生什么变化?,ios,arrays,swift,memory,Ios,Arrays,Swift,Memory,我想知道,当我用另一个数组替换它们时,数组(任何类型)的值是否自动为零。例如,UIImage数组的第一个值将替换为另一个图像。数组先前包含的第一个图像会发生什么变化?是否仅从RAM中删除了引用或图像本身 谢谢你的回答 只有在不再引用图像时才会发布图像 查看以获取更多信息。只有在没有更多引用的情况下才会发布该图像 查看更多信息。Swift使用自动参考计数(ARC)。您必须记住swift中类和结构之间的差异 从苹果的文档中: 引用计数仅适用于类的实例。结构和枚举是值类型,而不是引用类型,并且不通过引

我想知道,当我用另一个数组替换它们时,数组(任何类型)的值是否自动为零。例如,UIImage数组的第一个值将替换为另一个图像。数组先前包含的第一个图像会发生什么变化?是否仅从RAM中删除了引用或图像本身


谢谢你的回答

只有在不再引用图像时才会发布图像


查看以获取更多信息。

只有在没有更多引用的情况下才会发布该图像


查看更多信息。

Swift使用自动参考计数(ARC)。您必须记住swift中类和结构之间的差异

从苹果的文档中:

引用计数仅适用于类的实例。结构和枚举是值类型,而不是引用类型,并且不通过引用存储和传递

简而言之,ARC跟踪引用(强引用)类实例的常量、变量和属性的数量。如果没有引用,ARC将释放实例并释放内存

class Person {
   var name: String

   init(name: String) {
     self.name = name
   }
}

var steve: Person? = Person(name: "Steve") // Increasing ref counter -> 1
var sameSteve: Person? = steve // Increasing ref counter -> 2

steve = nil // Decreasing ref counter -> 1

print(sameSteve?.name) // prints "Steve"

sameSteve = nil // Decreasing ref counter -> 1

print(sameSteve?.name) // prints nil because it was deallocated

因此,由于
UIImage
是一种参考类型,如果从数组中删除或替换,ARC将减少其参考计数器。如果没有其他变量、常量或属性持有该图像,则图像将被解除分配。

Swift使用自动引用计数(ARC)。您必须记住swift中类和结构之间的差异

从苹果的文档中:

引用计数仅适用于类的实例。结构和枚举是值类型,而不是引用类型,并且不通过引用存储和传递

简而言之,ARC跟踪引用(强引用)类实例的常量、变量和属性的数量。如果没有引用,ARC将释放实例并释放内存

class Person {
   var name: String

   init(name: String) {
     self.name = name
   }
}

var steve: Person? = Person(name: "Steve") // Increasing ref counter -> 1
var sameSteve: Person? = steve // Increasing ref counter -> 2

steve = nil // Decreasing ref counter -> 1

print(sameSteve?.name) // prints "Steve"

sameSteve = nil // Decreasing ref counter -> 1

print(sameSteve?.name) // prints nil because it was deallocated

因此,由于
UIImage
是一种参考类型,如果从数组中删除或替换,ARC将减少其参考计数器。如果没有其他变量、常量或属性保存它,则图像将被释放。

此处将使用ARC的概念。 第一个图像也是UIImage实例,当您将第一个UIImage对象替换为第二个UIImage对象时,将删除对第一个对象的强引用。 案例1:(现在,我假设您没有在任何其他行中使用第一个UIImage实例,也没有分配给任何其他变量。) 因为,对象没有任何对它的强引用。当您将其替换为第二个图像的对象时,ARC会自动将其从内存中删除

案例2:但如果您已将其分配给下一个变量,则不会将其删除。 让我举例说明:


案例1:

var images = [UIImage]()
images.append(UIImage(named: "elcapitan"))
//now this first image is stored in index number 0 of the array
//images[0] gives you access to this first image in this case

//now i will replace the first image
images[0] = UIImage(named: "yosemite")
在这种情况下,圆弧会自动删除第一个图像实例,因为它不是从任何地方强引用的


案例2:

var images = [UIImage]()
var firstImage = UIImage(named: "elcapitan")
images.append(firstImage)
//now this first image is stored in index number 0 of the array
//images[0] gives you access to this first image in this case

//now i will replace the first image
images[0] = UIImage(named: "yosemite")
在这种情况下,ARC无法删除first image实例,因为它仍然被变量firstImage强引用,尽管您已经在数组中替换了它。只有将下一个映像指定给firstImage变量后,才会删除该实例。i、 e.仅当您这样做时:firstImage=UIImage(名称:“Windows”)


希望你能得到答案:)

这里要用到弧的概念。 第一个图像也是UIImage实例,当您将第一个UIImage对象替换为第二个UIImage对象时,将删除对第一个对象的强引用。 案例1:(现在,我假设您没有在任何其他行中使用第一个UIImage实例,也没有分配给任何其他变量。) 因为,对象没有任何对它的强引用。当您将其替换为第二个图像的对象时,ARC会自动将其从内存中删除

案例2:但如果您已将其分配给下一个变量,则不会将其删除。 让我举例说明:


案例1:

var images = [UIImage]()
images.append(UIImage(named: "elcapitan"))
//now this first image is stored in index number 0 of the array
//images[0] gives you access to this first image in this case

//now i will replace the first image
images[0] = UIImage(named: "yosemite")
在这种情况下,圆弧会自动删除第一个图像实例,因为它不是从任何地方强引用的


案例2:

var images = [UIImage]()
var firstImage = UIImage(named: "elcapitan")
images.append(firstImage)
//now this first image is stored in index number 0 of the array
//images[0] gives you access to this first image in this case

//now i will replace the first image
images[0] = UIImage(named: "yosemite")
在这种情况下,ARC无法删除first image实例,因为它仍然被变量firstImage强引用,尽管您已经在数组中替换了它。只有将下一个映像指定给firstImage变量后,才会删除该实例。i、 e.仅当您这样做时:firstImage=UIImage(名称:“Windows”)

希望你得到你的答案:)