Created
February 17, 2023 23:37
-
-
Save yasirmturk/a577b211f93ba38497217f49aa6bc90b to your computer and use it in GitHub Desktop.
max size collection in swift like java and kotlin
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
@propertyWrapper | |
public struct MaxSizeCollection<Value: Collection> { | |
private let _maxSize: UInt? | |
public var _value: Value | |
public init(wrappedValue value: Value) { | |
_value = value | |
_maxSize = nil | |
} | |
public init(wrappedValue value: Value, maxSize: UInt) { | |
_value = value | |
_maxSize = maxSize | |
} | |
public var wrappedValue: Value { | |
get { _value } | |
set { | |
guard let _maxSize = _maxSize, _value.count <= _maxSize else { return } | |
_value = newValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage