Last active
August 29, 2015 14:28
-
-
Save capttaco/e348548d1dffefdc1e37 to your computer and use it in GitHub Desktop.
This was created to show an immutability error with swift properties declared in a protocol and access in a protocol extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Generator | |
{ | |
typealias ObjectType: Generatable | |
var objects: [ObjectType] { get set } | |
mutating func objectForDeltaTime(seconds: CFTimeInterval) -> ObjectType? | |
} | |
extension Generator | |
{ | |
mutating func objectForDeltaTime(seconds: CFTimeInterval) -> ObjectType? | |
{ | |
// This does not work | |
// error: immutable value of type 'Self.ObjectType' only has mutating members named 'removeAtIndex' | |
let newObject = objects.removeAtIndex(0) | |
// This works | |
var objs = objects | |
let newObject = objs.removeAtIndex(0) | |
objects = objs | |
return newObject | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to anyone looking at this, the line marked "This does not work" does in fact work. The compiler was playing tricks on me and I kept his here for historic reasons. Since
objects
is a var, you can mutate it directly.