Skip to content

Instantly share code, notes, and snippets.

@capttaco
Last active August 29, 2015 14:28
Show Gist options
  • Save capttaco/e348548d1dffefdc1e37 to your computer and use it in GitHub Desktop.
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
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
}
}
@capttaco
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment