Being fairly new to Swift I decided I would look at extending Array
(or more specifically [SKTexture]
Arrays of SKTexture) with a function to add a specified number of frames from the application bundle.
// FRAMES
FuzzyRabbit_0001@2x.png
FuzzyRabbit_0002@2x.png
FuzzyRabbit_0003@2x.png
FuzzyRabbit_0004@2x.png
// CALL
var rabbitTextures = [SKTexture]()
self.rabbitTextures.textureFromFrames("FuzzyRabbit", count: 4)
My first attempt is listed below, I am getting the error Cannot invoke 'append' with an argument list of type '(SKTexture!)'
which from looking at the function fuzzyPush
is because I am trying to append an SKTexture
rather than the generic T
.
Is this possible, or am I limited by the fact that I don't want the function to be generic but rather specific to Arrays of SKTexture
.
extension Array {
// ONLY SKTexture
mutating func textureFromFrames(imageName: String, count: Int) {
if !(self[0] is SKTexture) { return }
for index in 1...count {
let image = String(format: "\(imageName)_%04d", index)
let texture = SKTexture(imageNamed: image)
self.append(texture) // ERROR: Cannot invoke append with an argument list of type SKTexture!
}
}
// WORKS FINE
mutating func fuzzyPush(newItem: T) {
self.append(newItem)
}
}
I was just curious if this is something I could do with an extension, its not a problem as I have this as a function that takes 3 parameters (imageName, count, arrayToAppend) so I can quite easily use that.