Skip to content

Instantly share code, notes, and snippets.

@yasirmturk
Created February 17, 2023 23:37
Show Gist options
  • Save yasirmturk/a577b211f93ba38497217f49aa6bc90b to your computer and use it in GitHub Desktop.
Save yasirmturk/a577b211f93ba38497217f49aa6bc90b to your computer and use it in GitHub Desktop.
max size collection in swift like java and kotlin
@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
}
}
}
@yasirmturk
Copy link
Author

yasirmturk commented Feb 17, 2023

Usage

@MaxSizeCollection(maxSize: 128)
var limitSizeArray: [Any] = []

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