{"id":314,"date":"2019-09-12T13:08:29","date_gmt":"2019-09-12T13:08:29","guid":{"rendered":"http:\/\/ootips.org\/yonat\/?p=314"},"modified":"2019-09-12T13:08:29","modified_gmt":"2019-09-12T13:08:29","slug":"swiftui-binding-type-conversion","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/swiftui-binding-type-conversion\/","title":{"rendered":"SwiftUI: @Binding Type Conversion"},"content":{"rendered":"\n<p>What can you do if a <code>@Binding var<\/code> has a different type from the <code>@State var<\/code> you want to bind to it?<\/p>\n\n\n\n<p>For example, say you have a <code>Slider<\/code> which you bind to a <code>Double<\/code> state var, and you want to bind the same state var to an <code>Int<\/code> in another <code>View<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>struct<\/strong> MyView: View {\n &nbsp; &nbsp;@State <strong>private<\/strong> <strong>var<\/strong> <font color=\"teal\">currentStep<\/font>: Double = 0.0\n \n &nbsp; &nbsp;<strong>var<\/strong> body: <strong>some<\/strong> View {\n&nbsp; &nbsp; &nbsp; &nbsp; Slider(value: <font color=\"teal\">$currentStep<\/font>, in: 0.0 ... 9.0, step: 1.0)\n        ViewWithInt(bindingInt: <font color=\"teal\">$currentStep<\/font>) <font color=\"red\">\/\/ Error: 'Binding&lt;Double&gt;' is not convertible to 'Binding&lt;Int&gt;'<\/font>\n    }\n}<\/pre>\n\n\n\n<p>Solution: define an <code>Int<\/code> property on <code>Double<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>extension<\/strong> Double {\n &nbsp; &nbsp; <strong>var<\/strong> int: Int {\n &nbsp; &nbsp; &nbsp; &nbsp; <strong>get<\/strong> { Int(<strong>self<\/strong>) }\n &nbsp; &nbsp; &nbsp; &nbsp; <strong>set<\/strong> { <strong>self<\/strong> = Double(newValue) }\n &nbsp; &nbsp; }\n }<\/pre>\n\n\n\n<p>Then you can bind this int property:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ViewWithInt(bindingInt: <font color=\"teal\">$currentStep.int<\/font>) <font color=\"green\">\/\/ Works<\/font><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What can you do if a @Binding var has a different type from the @State var you want to bind to it? For example, say you have a Slider which you bind to a Double state var, and you want &hellip; <a href=\"https:\/\/ootips.org\/yonat\/swiftui-binding-type-conversion\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[7,17,22],"class_list":["post-314","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ios","tag-swift","tag-swiftui"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-54","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":7,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/314\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}