[String]常量

时间:2016-04-25 13:03:32

标签: ios swift memory-leaks

我有一个非常简单的Swift类只有一个静态方法,这个方法分配3个字符串数组,并通过附加这些数组中的元素来创建一个随机字符串。

这里是代码:

public static func generateText() -> String {

   let phraseComponent1 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

   let phraseComponent2 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

   let phraseComponent3 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

    let componentIndex1 = Int(arc4random_uniform(UInt32(phraseComponent1.count)))
    let componentIndex2 = Int(arc4random_uniform(UInt32(phraseComponent2.count)))
    let componentIndex3 = Int(arc4random_uniform(UInt32(phraseComponent3.count)))

    let phrase1 = phraseComponent1[componentIndex1]
    let phrase2 = phraseComponent2[componentIndex2]
    let phrase3 = phraseComponent3[componentIndex3]

    return "\(phrase1) \(phrase2) \(phrase3)"
}

正如您在屏幕截图中看到的那样,此代码会在第一个阵列上生成内存泄漏: enter image description here

有人可以告诉我一个理由吗?以及如何解决这个问题

1 个答案:

答案 0 :(得分:0)

我按照@ketiver提到的那样做了,添加了一个调用这个方法的巨大循环,并且堆没有移动,也没有泄漏的数量,所以正如他所提到的,这是一个误导性的诊断。不需要采取任何行动。