{"id":15941,"date":"2016-10-05T14:01:23","date_gmt":"2016-10-05T18:01:23","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15941"},"modified":"2016-10-05T14:02:47","modified_gmt":"2016-10-05T18:02:47","slug":"caveat-formatter","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/10\/05\/caveat-formatter\/","title":{"rendered":"Caveat Formatter"},"content":{"rendered":"<p><a href=\"http:\/\/lapcatsoftware.com\/articles\/caveat-formatter.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/lapcatsoftware.com\/articles\/caveat-formatter.html\">\n<p>However, if you attempt to return the original string by reference, it doesn&rsquo;t work right. The new, invalid string will still appear in the text field, despite the fact that <code>-isPartialStringValid:<\/code> returns <code>NO<\/code>. By experimentation &mdash; computer science! &mdash; I discovered that you have to provide a reference to a copy of the original string rather than the original string itself. This is probably some kind of bug in AppKit.<\/p>\n<\/blockquote>\n<p>This does seem to be consistent with <a href=\"https:\/\/developer.apple.com\/reference\/foundation\/nsformatter\/1415263-ispartialstringvalid?language=objc#\">the documentation<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/reference\/foundation\/nsformatter\/1415263-ispartialstringvalid?language=objc#\"><p>In a subclass implementation, evaluate <code>partialString<\/code> according to the context. Return <code>YES<\/code> if <code>partialStringPtr<\/code> is acceptable and <code>NO<\/code> if <code>partialStringPtr<\/code> is unacceptable. If you return <code>NO<\/code> and assign a new string to <code>partialStringPtr<\/code> and a new range to <code>proposedSelRangePtr<\/code>, the string and selection range are changed, otherwise, if no values are assigned to <code>partialStringPtr<\/code> or <code>proposedSelRangePtr<\/code>, the change is rejected. If you return <code>NO<\/code>, you can also return by indirection an <code>NSString<\/code> object (in <code>error<\/code>) that explains the reason why the validation failed; the delegate (if any) of the <code>NSControl<\/code> object managing the cell can then respond to the failure in <code>control:didFailToValidatePartialString:errorDescription:<\/code>.<\/p><\/blockquote>\n<p>But it&rsquo;s probably not what you would expect.<\/p>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: However, if you attempt to return the original string by reference, it doesn&rsquo;t work right. The new, invalid string will still appear in the text field, despite the fact that -isPartialStringValid: returns NO. By experimentation &mdash; computer science! &mdash; I discovered that you have to provide a reference to a copy of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[69,30,1381,71],"class_list":["post-15941","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-mac","tag-macos-10-12","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15941","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=15941"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15941\/revisions"}],"predecessor-version":[{"id":15943,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15941\/revisions\/15943"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}