Trimmed

自動剪除字串前後的空白字元。

import Foundation   // string.trimmingCharacters(in:)

/// Automatically trims whitespaces and new lines from a `String` 
@propertyWrapper 
public struct Trimmed {
    // private vars
    private var _value: String
    // wrapped value
    public var wrappedValue: String {
        get { _value }
        set { _value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) }
    }
    // usage: @Trimmed var string: String = value
    public init(wrappedValue value: String) { 
        self._value = value.trimmingCharacters(in: .whitespacesAndNewlines) 
    } 
}

Last updated