{"id":36246,"date":"2022-06-16T15:38:31","date_gmt":"2022-06-16T19:38:31","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36246"},"modified":"2025-10-29T09:38:45","modified_gmt":"2025-10-29T13:38:45","slug":"parsableformatstyle","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/06\/16\/parsableformatstyle\/","title":{"rendered":"ParsableFormatStyle"},"content":{"rendered":"<p><a href=\"https:\/\/ampersandsoftworks.com\/posts\/from-strings-to-data-using-parseableformatstyle\/index.html\">Brett Ohland<\/a>:<\/p>\n<blockquote cite=\"https:\/\/ampersandsoftworks.com\/posts\/from-strings-to-data-using-parseableformatstyle\/index.html\"><p>Apple&rsquo;s modern Swift replacement system for <code>Formatter<\/code> is a set of protocols: <code>FormatStyle<\/code> and <code>ParseableFormatStyle<\/code>. The former handles the conversion to strings, and the latter strings to data.<\/p><p>[&#8230;]<\/p><p>The most direct way of parsing a string into it&rsquo;s respective data type is to create an instance of a <code>ParseableFormatStyle<\/code> that&rsquo;s set up to understand the structure of the incoming string. From there you access it&rsquo;s <code>parseStrategy<\/code> property, and call the <code>parse()<\/code> method on it.<\/p><p>This is a bit cumbersome, so Apple has included custom initializers onto each of the supported data types that take the string and either a <code>ParseableFormatStyle<\/code> or a <code>ParseStrategy<\/code> instance to do the parsing. What&rsquo;s interesting is that Apple includes initializers that can accept <em>any<\/em> input type, as long as you provide a <code>ParseStrategy<\/code> that informs the type how to parse it. Aren&rsquo;t constrained generics neat?<\/p>\n<p>[&#8230;]<\/p>\n<p>New for iOS 16, you can now parse URLs using this exact manner[&#8230;]<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/06\/10\/formatstyles-in-ios-16-and-macos-13\/\">FormatStyles in iOS 16 and macOS 13<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Brett Ohland: Apple&rsquo;s modern Swift replacement system for Formatter is a set of protocols: FormatStyle and ParseableFormatStyle. The former handles the conversion to strings, and the latter strings to data.[&#8230;]The most direct way of parsing a string into it&rsquo;s respective data type is to create an instance of a ParseableFormatStyle that&rsquo;s set up to understand [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-06-16T19:38:39Z","apple_news_api_id":"4de742e7-f4cb-4342-805e-1cca792bb3ae","apple_news_api_modified_at":"2025-10-29T13:38:47Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/ATedC5_TLQ0KAXhzKeSuzrg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,1667,2078,30,2077,2223,568,270,71,2854,901,489],"class_list":["post-36246","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-13","tag-ios-15","tag-mac","tag-macos-12","tag-macos-13-ventura","tag-nsdateformatter","tag-parser","tag-programming","tag-swift-foundation","tag-swift-programming-language","tag-url"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=36246"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36246\/revisions"}],"predecessor-version":[{"id":36247,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36246\/revisions\/36247"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}