Created
February 18, 2020 09:47
-
-
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
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 | |
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