Last active
July 28, 2016 14:57
-
-
Save bryanluby/548beba8f8062c81ed68 to your computer and use it in GitHub Desktop.
Static variable inside of a function in Swift.
This file contains hidden or 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
func staticInsideFunction() { | |
struct Temp { static var hasAnimated = false } | |
if Temp.hasAnimated == false { | |
// ... do something only on the first function call. | |
Temp.hasAnimated = true | |
} else { | |
// ... do something on all subsequent function calls. | |
} | |
} |
It didn't work for me and I've got the following errors:
Type 'Temp' nested in generic function 'myFunction' is not allowed
Static stored properties not yet supported in generic types
Any other suggestions?
This looks genius. What are examples where you would use this pattern?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice. Thank you for sharing!