Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 3 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 13 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 23 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 33 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 43 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 53 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 63 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 73 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 83 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 93 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 103 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 113 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 123 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/content.php on line 133

Properties in a Category

One of the many things I like with Objective-C is the possibility to extend any class without having to write a proper extension class that inherits from the original. The technique is called categories and what you is that you specify one or more new methods to be a part of a given class.

Below is an example of how to extend a standard NSString object with a new custom method that checks if a certain string is a part of the NSString.

To use the above category you simply import the NSString+Contains.h in your source code after that you can use the new method on all NSString objects. Great! But what if you also want to add a property? The category mechanism does not support properties but where there is a will, there is a way.

By declaring a property and then add your own getter and setter methods you can use the C-method objc_setAssociatedObject to connect and retrieve data to an object. Let me show you; In the following example I’ll create a category that will let you store any Objective-C object in a property called customData in a NSString object.

In many cases the best solution is to create a new class that extends another class to accomplish this, but for some situations this proves to be a highly efficient solution.

Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 3 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 13 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 23 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 33 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 43 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 53 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 63 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 73 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 83 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 93 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 103 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 113 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 123 Notice: Undefined index: HTTP_REFERER in /home/a/arainydayse/arainydayse/arainyday.se/www/wp-content/themes/twentytwelve/comments.php on line 133