Binding+convert.swift (2007B)
1 // MIT License 2 // Copyright https://stackoverflow.com/users/168179/mark-a-donohoe 3 // https://stackoverflow.com/questions/65736518/how-do-i-create-a-slider-in-swiftui-bound-to-an-int-type-property 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software 6 // and associated documentation files (the "Software"), to deal in the Software without restriction, 7 // including without limitation the rights to use, copy, modify, merge, publish, distribute, 8 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 9 // furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in all copies or 12 // substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 15 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 17 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 // 20 21 import SwiftUI 22 23 public extension Binding { 24 25 static func convert<TInt, TFloat>(_ intBinding: Binding<TInt>) -> Binding<TFloat> 26 where TInt: BinaryInteger, 27 TFloat: BinaryFloatingPoint{ 28 29 Binding<TFloat> ( 30 get: { TFloat(intBinding.wrappedValue) }, 31 set: { intBinding.wrappedValue = TInt($0) } 32 ) 33 } 34 35 static func convert<TFloat, TInt>(_ floatBinding: Binding<TFloat>) -> Binding<TInt> 36 where TFloat: BinaryFloatingPoint, 37 TInt: BinaryInteger { 38 39 Binding<TInt> ( 40 get: { TInt(floatBinding.wrappedValue) }, 41 set: { floatBinding.wrappedValue = TFloat($0) } 42 ) 43 } 44 }