Need advices on NSMutableArray in Swift

I am having coding design difficulties with Array in Swift, see this post. So I decided to turn to the old NSMutableArray.

I'd like to know if there are any known problems of this approach. Any pitfalls and known practices?

Answered by imneo in 769033022

It's an agony working with array/dictionary in Swift!

NSMutableArray does not support generics so I have to stick to Swift builtin array.

I finally have to employ a not-so-perfect-and-ugly workaround:

/// Act as a reference container for value types.
open class ValueBox<ValueType> {
    public var value: ValueType

    public init(_ value: ValueType) {
        self.value = value
    }
}

// Arrays in a dictionary
typealias TranslationUnitBox = ValueBox<[TranslationUnit]>
var translations = [String: TranslationUnitBox>()
// Later in some other code
if let box = translations["en"] {
    box.value[index].translatedText = "..."
}

Why a new post linking to another post ? It would be simpler to complement the initial post, isn't it ?

Accepted Answer

It's an agony working with array/dictionary in Swift!

NSMutableArray does not support generics so I have to stick to Swift builtin array.

I finally have to employ a not-so-perfect-and-ugly workaround:

/// Act as a reference container for value types.
open class ValueBox<ValueType> {
    public var value: ValueType

    public init(_ value: ValueType) {
        self.value = value
    }
}

// Arrays in a dictionary
typealias TranslationUnitBox = ValueBox<[TranslationUnit]>
var translations = [String: TranslationUnitBox>()
// Later in some other code
if let box = translations["en"] {
    box.value[index].translatedText = "..."
}
Need advices on NSMutableArray in Swift
 
 
Q