i have a Problem with a Class that createds some Questions for a TableView. There is a Class Question and a Class Questions, which hold an array of Question.
The Problem is, that the binding betweeen these classes are strong at the moment, and so the ARC could not dealloc the memory. Ive tried the Things described in the Swift Book about weak or unowned references, but i am not able to fix the leak in my Code.
Can anyone help me please, how to prevent Swift from creating a strong Relation between the Objects, so the ARC can dealloc the used Memory?
Thanks in Advance
class Questions {
var questions: [Question] = []
init() {
let validJSONData = NSData(contentsOfFile : NSBundle(forClass:ViewController.self).pathForResource("data", ofType: "json"))
let json = JSONValue(validJSONData)
for item in json["questions"].array!
{
var rowhHeight:CGFloat = 44.00
if(item["template"].string! == "textViewCell") {
rowhHeight = 100.00
}
var newQuestion = Question(qIndex: item["qid"].integer!, qText: item["qtext"].string!, qTemplate: item["template"].string!, qHidden: item["hidden"].bool!, qRowHeight: rowhHeight)
questions.append(newQuestion)
}
}
func getQuestions() -> NSArray {
return questions
}
deinit{
println("questionS deinitialized")
}
}
class Question: NSObject {
var qIndex: Int
var qText : String
var qTemplate : String
var qHidden : Bool
var qRowHeight : CGFloat
init(qIndex: Int, qText: String, qTemplate: String, qHidden: Bool, qRowHeight: CGFloat) {
self.qIndex = qIndex
self.qText = qText
self.qTemplate = qTemplate
self.qHidden = qHidden
self.qRowHeight = qRowHeight
}
deinit{
println("question deinitialized")
}
}
Questionobject doesn't have a reference to the array. The array needs a strong reference to its elements. Releasing theQuestionsobject will allow the elements to be released. - Paulw11