我的问题非常类似于iOS应用程序崩溃,而UIImage加载(虚拟内存不清理)因为我有一个对象数组,每个对象都有一个图片作为它的一个变量。在上面的帖子的答案似乎是,对象仍然存在,即使他们从数组中删除。然而,我似乎不能弄清楚他到底做了什么来解决这个问题。我正在使用Swift,没有Objective-C的经验。
谁能告诉我他做了什么来解决他的问题?他在回答中的示例代码对我来说没有意义。
这是我的类,它包含了image属性:
open class Cards {
var name = ""
var image : UIImage!
var manaCost = 0
var rarity = ""
var whichClass = ""
var cardText = ""
var tribe = ""
var cardType = "" //Minion or Spell
var set = ""
init(name : String, image : UIImage!, manaCost : Int, rarity : String, whichClass : String, cardType : String, tribe : String, cardText : String, set : String)
{
self.name = name
self.image = image
self.manaCost = manaCost
self.rarity = rarity
self.whichClass = whichClass
self.cardType = cardType
self.tribe = tribe
self.cardText = cardText
self.set = set
}
deinit {
print("deinit " + name);
}
}
对象的初始化看起来有点像这样:
var ArgeonHighmayne = Cards(name: "Argeon Highmayne", image : UIImage( named : "ArgeonHighmayne")!, manaCost : 0, rarity : "Basic", whichClass : "Lyonar", cardType : "General", tribe : "None", cardText : "Bloodborn Spell: Give a minion nearby your general +2 Attack.", set : "Core")
和这些对象所在的数组如下所示:
public var newestCards = [DayWatcher, NightWatcher, DustWailer, QuartermasterGauj]
我试图通过简单地在声明前添加"weak"并添加所有"!"one_answers"?"来将对象本身转换为弱引用。这是Xcode要求我添加的,但这只是导致图像没有出现在我的集合视图中。我还尝试在类声明本身中添加"weak",如下所示:
weak var image : UIImage!
但这也导致图像根本不显示。谢谢你的帮助。
结果是它崩溃了,因为图像是数组的一部分。与其把UIImage放在类中,不如把图像的名字放在那里,然后在collectionView功能中调用那个名字的UIImage。