Skip to content

Instantly share code, notes, and snippets.

@mrugeshtank
Created February 18, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrugeshtank/722da808c8e5f6434bcf63bff337410e to your computer and use it in GitHub Desktop.
Save mrugeshtank/722da808c8e5f6434bcf63bff337410e to your computer and use it in GitHub Desktop.
by this way you can always get trimmed value no more extension or method required
@propertyWrapper
struct Trimmed {
private(set) var value: String?
var wrappedValue: String? {
get {
return value
}
set {
value = newValue?.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
init(wrappedValue initialValue: String?) {
self.wrappedValue = initialValue
}
}
struct MyStruct {
@Trimmed var title: String?
@Trimmed var body: String?
}
///Usage
//var obj = MyStruct()
//obj.title = " test "
//obj.title // Will print "test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment