{"id":118,"date":"2012-02-25T08:40:22","date_gmt":"2012-02-25T08:40:22","guid":{"rendered":"http:\/\/ootips.org\/yonat\/?p=118"},"modified":"2016-04-17T07:26:09","modified_gmt":"2016-04-17T07:26:09","slug":"objective-c-protocol-implementation","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/objective-c-protocol-implementation\/","title":{"rendered":"How to Keep Your Protocols Private"},"content":{"rendered":"<p>Don&#8217;t you just hate when a small change in the innards of your view controller forces you to change its header file just to conform to a <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/General\/Conceptual\/DevPedia-CocoaCore\/Delegation.html\" target=\"_blank\">delegate<\/a> protocol? For example, adding emailing functionality requires you to implement the <tt>MFMailComposeViewControllerDelegate<\/tt> protocol and <tt>@import &lt;MessageUI\/MessageUI.h&gt;<\/tt>. Talk about breaking encapsulation&#8230;<\/p>\n<p>Thankfully, you can do that in your .m implementation file instead. (Even though Apple sample code doesn&#8217;t.) All you need to do is use <b>the empty category<\/b>:<\/p>\n<pre class=\"lang:objc decode:true \">\r\n\/\/ MyViewController.m\r\n#import \"MyViewController.h\"\r\n#import <MessageUI\/MessageUI.h>\r\n\r\n@interface MyViewController ()\r\n    <MFMailComposeViewControllerDelegate> \/\/ privately conform to protocol\r\n@property (nonatomic, strong) UIView *somePrivateSubview;\r\n@end\r\n\r\n@implementation MyViewController\r\n\/\/ synthesize and methods implementations\r\n@end\r\n<\/pre>\n<p>The empty category <tt>MyViewController ()<\/tt> allows you to define private ivars, properties, methods, and even protocols \u2013 all in the privacy of your .m file, transparent to your clients.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t you just hate when a small change in the innards of your view controller forces you to change its header file just to conform to a delegate protocol? For example, adding emailing functionality requires you to implement the MFMailComposeViewControllerDelegate &hellip; <a href=\"https:\/\/ootips.org\/yonat\/objective-c-protocol-implementation\/\">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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[7,6,15,13,11],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ios","tag-iphone","tag-object-oriented-design","tag-objective-c","tag-programming-idioms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-1U","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/118","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=118"}],"version-history":[{"count":9,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/118\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}