{"id":1900,"date":"2015-09-02T19:15:13","date_gmt":"2015-09-02T13:45:13","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=1900"},"modified":"2023-01-20T18:56:01","modified_gmt":"2023-01-20T13:26:01","slug":"ios-usage-of-category","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/","title":{"rendered":"iOS &#8211; Usage of Category"},"content":{"rendered":"<h1><span style=\"color: #3366ff;\">Category<\/span><\/h1>\n<p>What is Category and why do we need this in iOS Development?<\/p>\n<p>First thing you need to know, Category is not a subclassing (Inheritance).\u00a0Category is simply a concept of adding methods to your existing class.\u00a0No need to create the instance of your category class as it will be called from your Main class instance.<\/p>\n<p>Not Getting?\u00a0Lets take an example&#8230;.<\/p>\n<p>We have class Car, inside it have methods getCarSpeed, getCarColor.<\/p>\n<pre class=\"lang:objc decode:true\" title=\"Creating a Category\">\/\/ Header File\r\n@interface Car : NSObject\r\n\r\n-(NSInteger) getCarSpeed;\r\n-(UIColor *) getCarColor;\r\n\r\n@end\r\n\/\/ Implement File\r\n#import \"Car.h\"\r\n\r\n@implementation Car\r\n-(NSInteger) getCarSpeed\r\n{\r\nreturn 100;\r\n}\r\n-(UIColor *) getCarColor\r\n{\r\nreturn [UIColor greenColor];\r\n}\r\n@end\r\n\r\n\/\/Now I need to create one category class where I am adding one method.\r\n\r\n#import &lt;Foundation\/Foundation.h&gt;\r\n#import \"Car.h\"\r\n\r\n@interface Car (Modified) \/\/This is the Category\r\n\r\n-(NSInteger) getCarMileage;\r\n\r\n@end\r\n\r\n#import \"Car+Modified.h\"\r\n\r\n@implementation Car (Modified)\r\n\r\n-(NSInteger) getCarMileage\r\n{\r\nreturn 12;\r\n}\r\n\r\n@end<\/pre>\n<p>We have created a Category of Car Class.<\/p>\n<p>Now let us call it from UIViewController.<\/p>\n<pre class=\"lang:objc decode:true\" title=\"Call Category class methods\">@implementation ViewController\r\n\r\n- (void)viewDidLoad {\r\n[super viewDidLoad];\r\nCar *car = [[Car alloc]init]; \/\/We are creating an instance of Car only\r\nNSInteger milage = [car getCarMilage];\r\n}<\/pre>\n<p>If you notice here we have created the instance of the Car but we are calling the method of Car+Modified.<\/p>\n<p>Try it with your self for better understanding.<\/p>\n<h2>Adding methods in existing Class NSString<\/h2>\n<pre class=\"lang:objc decode:true\" title=\"Use of Category in NSString\">\/\/ Header File\r\n#import &lt;Foundation\/Foundation.h&gt;\r\n@interface NSString (MyString)\r\n-(NSString *) reverseString;\r\n@end\r\n\r\n\/\/ Implementation File\r\n#import \"NSString+MyString.h\"\r\n@implementation NSString (MyString)\r\n-(NSString *) reverseString\r\n{\r\nNSMutableString *reversedString;\r\nNSInteger strLength = [self length];\r\nreversedString = [NSMutableString stringWithCapacity:strLength];\r\n\r\nwhile (strLength &gt; 0)\r\n{\r\n[reversedString appendString:[NSString stringWithFormat:@\"%C\", [self characterAtIndex:--strLength]]];\r\n}\r\n\r\nreturn reversedString;\r\n}\r\n@end<\/pre>\n<p>Here we have added the &#8216;reverseString&#8217; method to existing NSStirng Class. Now this method is available for use along with other methods of the NSSting.<\/p>\n<h1><span style=\"color: #3366ff;\">Scenarios \/ Situations were we use Category<\/span><\/h1>\n<p>&#8211; Split the code according to the module (For each module go for different category)<br \/>\n&#8211; Add methods to iOS SDK classes Like NSString, UIColor, UIFont (Frequently used)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Category What is Category and why do we need this in iOS Development? First thing you need to know, Category is not a subclassing (Inheritance).\u00a0Category is simply a concept of adding methods to your existing class.\u00a0No need to create the instance of your category class as it will be called from your Main class instance. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,71,8],"tags":[158,14,160,89,165],"class_list":["post-1900","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","category-mobile-architecture-and-design","tag-category-in-ios","tag-innovationm","tag-ios","tag-iphone","tag-mobile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>iOS - Usage of Category - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"iOS - Usage of Category - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Category What is Category and why do we need this in iOS Development? First thing you need to know, Category is not a subclassing (Inheritance).\u00a0Category is simply a concept of adding methods to your existing class.\u00a0No need to create the instance of your category class as it will be called from your Main class instance. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-02T13:45:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:26:01+00:00\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"iOS &#8211; Usage of Category\",\"datePublished\":\"2015-09-02T13:45:13+00:00\",\"dateModified\":\"2023-01-20T13:26:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/\"},\"wordCount\":187,\"commentCount\":0,\"keywords\":[\"Category in iOS\",\"InnovationM\",\"iOS\",\"iPhone\",\"Mobile\"],\"articleSection\":[\"iOS\",\"Mobile\",\"Mobile Architecture and Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/\",\"name\":\"iOS - Usage of Category - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-09-02T13:45:13+00:00\",\"dateModified\":\"2023-01-20T13:26:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/ios-usage-of-category\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"iOS &#8211; Usage of Category\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"iOS - Usage of Category - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/","og_locale":"en_US","og_type":"article","og_title":"iOS - Usage of Category - InnovationM - Blog","og_description":"Category What is Category and why do we need this in iOS Development? First thing you need to know, Category is not a subclassing (Inheritance).\u00a0Category is simply a concept of adding methods to your existing class.\u00a0No need to create the instance of your category class as it will be called from your Main class instance. [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/","og_site_name":"InnovationM - Blog","article_published_time":"2015-09-02T13:45:13+00:00","article_modified_time":"2023-01-20T13:26:01+00:00","author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"iOS &#8211; Usage of Category","datePublished":"2015-09-02T13:45:13+00:00","dateModified":"2023-01-20T13:26:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/"},"wordCount":187,"commentCount":0,"keywords":["Category in iOS","InnovationM","iOS","iPhone","Mobile"],"articleSection":["iOS","Mobile","Mobile Architecture and Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/","url":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/","name":"iOS - Usage of Category - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"datePublished":"2015-09-02T13:45:13+00:00","dateModified":"2023-01-20T13:26:01+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/ios-usage-of-category\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"iOS &#8211; Usage of Category"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/1900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=1900"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/1900\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=1900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=1900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=1900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}