tag:blogger.com,1999:blog-90878177466066173552024-03-13T10:42:58.272-07:00Core FascinationWelcome to my programming blog. This blog is about some of the fascinating things I learn in Objective-C, Swift and other programming languages. I'll generally write about cool stuff I come across in my everyday life as an iOS developer. Tielman van Vuurenhttp://www.blogger.com/profile/01529166828839882483noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-9087817746606617355.post-71323335463729093742016-05-02T01:11:00.000-07:002016-05-02T01:11:14.018-07:00A Category Example on UIViewIf you have ever used categories, you will probably agree if I tell you they are one of the coolest features of Objective-C. They are great for making code more readable and reusable. Here's an example of a category I recently started using because it can be quite frustrating entering:<br />
<br />
<div style="color: #703daa; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;">[</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">view</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">view</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">origin</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">x</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">10</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">view</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">size</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">width</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">view</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">size</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">height</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)];</span></span></div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: inherit;">just to change the </span><span style="font-family: Courier New, Courier, monospace;">y</span><span style="font-family: inherit;"> value for a view's frame. I wanted to make a category where you can do something like the following:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">view</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #272ad8; font-variant-ligatures: no-common-ligatures;">10</span><span style="font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<h4>
<span style="font-family: inherit;">The resulting category</span></h4>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">Here is the code in the .h file for the </span><span style="font-family: Courier New, Courier, monospace;">UIView+Frame</span><span style="font-family: inherit;"> category:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">@interface</span><span style="font-variant-ligatures: no-common-ligatures;"> UIView (Frame)</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> x;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> y;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> height;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> width;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGSize</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> size;</span></span></div>
<div style="font-size: 18px; line-height: normal; min-height: 21px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"></span><br /></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures;">readonly</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> bottom;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures;">readonly</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> right;</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">@end</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;"><br /></span></span></div>
<div style="line-height: normal;">
<span style="font-family: inherit;">You might never have heard of adding properties using categories, but they're not really properties with instance variables, they're only properties in the sense that they have a setter and a getter. The setters and getters are implemented in the .m file:</span></div>
<div style="line-height: normal;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">@implementation</span><span style="font-variant-ligatures: no-common-ligatures;"> UIView (Frame)</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)x </span>{</span></div>
<div style="color: #703daa; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">origin</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">x</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)y </span>{</span></div>
<div style="color: #703daa; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">origin</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">y</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)height </span>{</span></div>
<div style="color: #703daa; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">size</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">height</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)width </span>{</span></div>
<div style="color: #703daa; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">size</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">width</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGSize</span><span style="font-variant-ligatures: no-common-ligatures;">)size </span>{</span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">frame</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">size</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">)setX:(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)x </span>{</span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="font-variant-ligatures: no-common-ligatures;">(x,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)];</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">)setY:(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)y </span>{</span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">x</span><span style="font-variant-ligatures: no-common-ligatures;">,y,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)];</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">)setHeight:(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)height </span>{</span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">x</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">,height)];</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">)setWidth:(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)width </span>{</span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">x</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;">,width,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)];</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">)setSize:(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGSize</span><span style="font-variant-ligatures: no-common-ligatures;">)size </span>{</span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">setFrame</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color: #3d1d81; font-variant-ligatures: no-common-ligatures;">CGRectMake</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">x</span><span style="font-variant-ligatures: no-common-ligatures;">,</span><span style="color: #bb2ca2; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;">,size.</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">,size.</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)];</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal; min-height: 21px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"></span><br /></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)bottom </span>{</span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">y</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> + </span><span style="font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">height</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal; min-height: 21px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;"></span><br /></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-variant-ligatures: no-common-ligatures;">-(</span><span style="color: #703daa; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">)right </span>{</span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-family: Courier New, Courier, monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">x</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> + </span><span style="font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #4f8187; font-variant-ligatures: no-common-ligatures;">width</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></span></div>
<div style="font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">}</span></span></div>
<div style="font-size: 18px; line-height: normal;">
</div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Courier New, Courier, monospace;">@end</span></span></div>
<div style="color: #bb2ca2; font-size: 18px; line-height: normal;">
<br /></div>
<div style="line-height: normal;">
<span style="font-family: inherit;">Notice that the getter is just the name of the property, while the setter is </span><span style="font-family: Courier New, Courier, monospace;">setName</span><span style="font-family: inherit;"> where </span><span style="font-family: Courier New, Courier, monospace;">name</span><span style="font-family: inherit;"> is the name of the property. Really straightforward. Notice also the two properties </span><span style="font-family: Courier New, Courier, monospace;">bottom</span><span style="font-family: inherit;"> and </span><span style="font-family: Courier New, Courier, monospace;">right</span><span style="font-family: inherit;">. I added them in for convenience, because I often need to know x or y value for the right or bottom of a view. But I never want to change the bottom or the right, so I made that property </span><span style="font-family: Courier New, Courier, monospace;">readonly</span><span style="font-family: inherit;">.</span></div>
<div style="line-height: normal;">
<span style="font-family: inherit;"><br /></span></div>
<div style="line-height: normal;">
<span style="font-family: inherit;">Categories can really make life a lot easier, especially when you use properties in categories. I hope I've encouraged you to try categories a bit more.</span></div>
</div>
<div>
<span style="font-family: inherit;"><br /></span></div>
Tielman van Vuurenhttp://www.blogger.com/profile/01529166828839882483noreply@blogger.com0tag:blogger.com,1999:blog-9087817746606617355.post-2271917751812043622016-04-26T02:17:00.000-07:002016-04-26T02:17:24.644-07:00Music from terminal with afplay on Mac<div style="line-height: normal;">
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: inherit;">So you want to play music on your Mac but you don't want to use iTunes. Open the terminal app (Launchpad => Other => Terminal) cd to the folder of music you want to play, and copy and paste the following:</span></span><br />
<div style="font-size: 12px;">
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></span></div>
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;">find * | grep "mp3$" | while read; do echo "$REPLY"; afplay "$REPLY"; wait; done</span></span><br />
<div style="font-size: 12px;">
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></span></div>
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: inherit;">This will play all mp3s in the folder (and subfolders) it is run in. </span><span style="font-family: Courier New, Courier, monospace;">find</span><span style="font-family: inherit;"> lists all the files in the folder and subfolders, </span><span style="font-family: Courier New, Courier, monospace;">grep</span><span style="font-family: inherit;"> selects those files ending in mp3, the last piece of code plays them one after the other using </span><span style="font-family: Courier New, Courier, monospace;">afplay</span><span style="font-family: inherit;">.</span></span><br />
<br />
<h4>
<span style="font-family: inherit;"><span style="background-color: white;">Shuffling</span></span></h4>
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: inherit;"><br /></span></span>
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: inherit;">If you don't want to play them in order, you'll need something to shuffle the lines of output from </span><span style="font-family: Courier New, Courier, monospace;">grep</span><span style="font-family: inherit;">. There's a stackoverflow <a href="http://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-lines-of-a-text-file-on-the-unix-command-line-or-in-a-shel">question</a></span><span style="font-family: inherit;"> on this, but I decided to uses </span><span style="font-family: Courier New, Courier, monospace;">shuf</span><span style="font-family: inherit;">. To get </span><span style="font-family: Courier New, Courier, monospace;">shuf</span><span style="font-family: inherit;"> using homebrew it's </span><span style="font-family: Courier New, Courier, monospace;">brew install coreutils</span><span style="font-family: inherit;">. On mac by default it's run as </span><span style="font-family: Courier New, Courier, monospace;">gshuf</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span></span></div>
<div style="font-size: 12px; line-height: normal;">
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></span></div>
<div style="font-size: 12px; line-height: normal;">
<span style="background-color: white; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="line-height: normal;">
<span style="background-color: white; font-family: Courier New, Courier, monospace;">find * | grep "mp3$" | gshuf | while read; do echo "$REPLY"; afplay "$REPLY"; wait; done</span><br />
<span style="background-color: white; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="background-color: white; font-family: inherit;">And there you have it. A simple way to play your mp3s using the terminal. Now I just need a way to pause and play again ...</span></div>
Tielman van Vuurenhttp://www.blogger.com/profile/01529166828839882483noreply@blogger.com0tag:blogger.com,1999:blog-9087817746606617355.post-2373481154179445462016-04-23T02:29:00.000-07:002016-04-23T02:41:11.424-07:00Superclass with a Category Factory method<b>tl;dr</b> If you want to make a factory method in Objective-C using a category and still get it to work for subclasses, you can do something like this:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="background-color: white;"><span style="color: purple;">@implementation</span></span> UIButton (ColorButton)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">+(<span style="color: purple;">instancetype</span>)buttonWithColor:(<span style="color: #674ea7;">UIColor</span>*)color {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #674ea7;">UIButton</span>* button = [[<span style="color: purple;">self</span>.class <span style="color: #073763;">alloc</span>] <span style="color: #073763;">initWithFrame</span>:<span style="color: #674ea7;">CGRectZero</span>];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ...</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: purple;">return</span> button;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<h3>
The Problem</h3>
This situation came up recently when I had a category class method that made a button with a certain background colour, but I wanted to customise the button to change background colour when it was highlighted or in the touch down state.<br />
<br />
The category looked like this:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="background-color: white;"><span style="color: purple;">@implementation</span></span> UIButton (ColorButton)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">+(<span style="color: purple;">UIButton</span>)buttonWithColor:(<span style="color: #674ea7;">UIColor</span>*)color {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #674ea7;">UIButton</span>* button = [[<span style="color: purple;">UIButton</span> <span style="color: #073763;">alloc</span>] <span style="color: #073763;">initWithFrame</span>:<span style="color: #674ea7;">CGRectMake</span>(0,0,UIScreen.mainScreen.bounds.size.width,50)];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> button.backgroundColor = color;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: purple;">return</span> button;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: purple;">@end</span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">This method takes a </span><span style="font-family: "courier new" , "courier" , monospace;">UIColor</span><span style="font-family: inherit;"> and returns a </span><span style="font-family: "courier new" , "courier" , monospace;">UIButton</span><span style="font-family: inherit;"> </span><span style="font-family: inherit;">with a size and colour.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In the subclass, I needed to override the </span><span style="font-family: "courier new" , "courier" , monospace;">setHighlighted:</span><span style="font-family: inherit;"> method:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: #bb2ca2; line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: black; font-variant-ligatures: no-common-ligatures;"><span style="background-color: white; color: purple;">@implementation</span> ColorButton</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-variant-ligatures: no-common-ligatures;">- (</span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">void</span><span style="font-variant-ligatures: no-common-ligatures;">) setHighlighted:(</span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">BOOL</span><span style="font-variant-ligatures: no-common-ligatures;">)highlighted {</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> [</span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">super</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #073763; font-variant-ligatures: no-common-ligatures;">setHighlighted</span><span style="font-variant-ligatures: no-common-ligatures;">:highlighted];</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: white; color: purple;">if</span><span style="font-variant-ligatures: no-common-ligatures;"> (highlighted) {</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: black;"> </span><span style="background-color: white; color: purple;">self</span><span style="color: black;">.</span><span style="color: #703daa;">backgroundColor</span><span style="color: black;"> = </span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black;">.</span><span style="color: #4f8187;">colour_selected</span><span style="color: black;">;</span></span></div>
<div style="line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;"> }</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">else</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></span></div>
<div style="line-height: normal;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: black;"> </span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black;">.</span><span style="color: #703daa;">backgroundColor</span><span style="color: black;"> = </span><span style="color: purple; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black;">.</span><span style="color: #4f8187;">colour</span><span style="color: black;">;</span></span></div>
<div style="line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;"> }</span></span></div>
<div style="line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: "courier new" , "courier" , monospace;">}</span></span></div>
<div style="line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><span style="color: purple; font-family: "courier new" , "courier" , monospace;">@end</span></span><br />
<span style="font-variant-ligatures: no-common-ligatures;"><span style="color: purple; font-family: "courier new" , "courier" , monospace;"><br /></span></span>
<span style="font-family: inherit;">This allows me to pick two colours </span><span style="color: #703daa;"><span style="font-family: inherit;"><span style="color: #4f8187; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">colour_selected</span> </span></span><span style="font-family: inherit;">and </span><span style="font-family: inherit;"><span style="color: #703daa;"><span style="color: #4f8187; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">colour</span> </span>to be the tap down and tap up colours respectively. I tried using my new subclass with:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: #674ea7;">ColorButton</span>* button_new = [<span style="color: #674ea7;">ColorButton</span> <span style="color: #073763;">buttonWithColor</span>:[<span style="color: purple;">UIColor</span> whiteColor]];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">button_new.<span style="color: #4f8187;">colour</span> = [<span style="color: purple;">UIColor</span> <span style="color: #073763;">whiteColor</span>];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">button_new.<span style="color: #4f8187;">colour_selected</span> = [<span style="color: purple;">UIColor</span> <span style="color: #073763;">grayColor</span>];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
However when I tried to compile this code and I got the following warning:<br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal; margin-left: 12px; text-indent: -12px;">
incompatible pointer types assigning to 'ColorButton *' from 'UIButton *'</div>
</div>
<h4>
<span style="font-family: inherit;"><br /></span></h4>
<h4>
<span style="font-family: inherit;">The first fix</span></h4>
<span style="font-family: inherit;">Okay. I tried changing </span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">+(</span><span style="color: purple; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">UIButton</span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">)buttonWithColor:(</span><span style="color: #674ea7; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">UIColor</span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">*)color</span><span style="font-family: inherit;"> to</span><br />
<span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">+(</span><span style="color: purple; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">instancetype</span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">)buttonWithColor:(</span><span style="color: #674ea7; font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">UIColor</span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">*)color.</span><span style="font-family: inherit;"> Now the code compiled and ran, but it crashed when it tried to set the </span><span style="font-family: "courier new" , "courier" , monospace;">colour</span><span style="font-family: inherit;"> property on </span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">button_new</span><span style="font-family: inherit;">.</span><br />
<h4>
<span style="font-family: inherit;"><br /></span></h4>
<h4>
<span style="font-family: inherit;">The second fix</span></h4>
<span style="font-family: inherit;">It crashed because </span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">button_new</span><span style="font-family: inherit;"> was actually a </span><span style="font-family: "courier new" , "courier" , monospace;">UIButton</span><span style="font-family: inherit;"> and not a </span><span style="font-family: "courier new" , "courier" , monospace;">ColorButton</span><span style="font-family: inherit;">. To fix this I needed to look at the code in the </span><span style="font-family: "\22 courier new\22 " , "\22 courier\22 " , monospace;">buttonWithColor:</span><span style="font-family: inherit;"> method a little closer.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="background-color: white;"><span style="color: purple;">@implementation</span></span> UIButton (ColorButton)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">+(<span style="color: purple;">instancetype</span>)buttonWithColor:(<span style="color: #674ea7;">UIColor</span>*)color {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #674ea7;">UIButton</span>* button = [[<span style="color: purple;">UIButton</span> <span style="color: #073763;">alloc</span>] <span style="color: #073763;">initWithFrame</span>:<span style="color: #674ea7;">CGRectMake</span>(0,0,UIScreen.mainScreen.bounds.size.width,50)];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> button.backgroundColor = color;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: purple;">return</span> button;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: purple;">@end</span></span><br />
<br />
<span style="font-family: inherit;">Notice the call to </span><span style="font-family: "courier new" , "courier" , monospace;">alloc</span><span style="font-family: inherit;"> is for a </span><span style="font-family: "courier new" , "courier" , monospace;">UIButton</span><span style="font-family: inherit;">. How did I make it work for the current class just like I did with </span><span style="font-family: "courier new" , "courier" , monospace;">instancetype</span><span style="font-family: inherit;">? I replaced the </span><span style="font-family: "courier new" , "courier" , monospace;">UIButton</span><span style="font-family: inherit;"> with </span><span style="font-family: "courier new" , "courier" , monospace;">self.class</span><span style="font-family: inherit;">:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="background-color: white;"><span style="color: purple;">@implementation</span></span> UIButton (ColorButton)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">+(<span style="color: purple;">instancetype</span>)buttonWithColor:(<span style="color: #674ea7;">UIColor</span>*)color {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: #674ea7;">UIButton</span>* button = [[<span style="color: purple;">self.class</span> <span style="color: #073763;">alloc</span>] <span style="color: #073763;">initWithFrame</span>:<span style="color: #674ea7;">CGRectMake</span>(0,0,UIScreen.mainScreen.bounds.size.width,50)];</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> button.backgroundColor = color;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: purple;">return</span> button;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: purple;">@end</span></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="color: purple;"><br /></span></span>
And that's it. The code runs and the button changes colour when it's tapped. But what is <span style="font-family: "courier new" , "courier" , monospace;">self</span><span style="font-family: inherit;"> in this case and what does </span><span style="font-family: "courier new" , "courier" , monospace;">self.class</span><span style="font-family: inherit;"> do? I guess that question could be a topic for another blog post.</span><br />
<span style="font-family: inherit;"><br /></span>Tielman van Vuurenhttp://www.blogger.com/profile/01529166828839882483noreply@blogger.com0