<?xml version="1.0" encoding="UTF-8"?>
<head><script type='text/javascript'>window.mod_pagespeed_start = Number(new Date());</script></head><rss version="2.0">
    <channel>
        <title>Fabián Cañas' Blog</title>
        <description>A personal blog on philosophy, science, coding, and anything else I find interesting.</description>
        <link>http://www.fabiancanas.com</link>
				
				<item>
				<title>Safe Casting in Objective-C</title>
				<description>&lt;p&gt;I&amp;#8217;m writing a small safe casting for Objective-C. I&amp;#8217;ll be putting it on &lt;a href=&quot;https://github.com/fcanas/SafeCast&quot;&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Story&lt;/h2&gt;

&lt;p&gt;Somewhere on the web, there was some test I took to determine how well I know Objective-C. I think it was elance. One question which infuriated me at the time was something like &amp;#8220;if you have an array &lt;code&gt;NSArray *a&lt;/code&gt;, how do you make it an &lt;code&gt;NSMutableArray&lt;/code&gt;?&amp;#8221; The reason I got angry was that the answer was supposedly&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray *mutableArray = (NSMutableArray *)a;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Except that this is tremendously dangerous, wrong, stupid, crazy&amp;#8230; well. You get the picture. The real answer of course is something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray *mutableArray = [a mutableCopy];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unless you want to actually modify the same instance that &lt;code&gt;a&lt;/code&gt; is &lt;em&gt;if&lt;/em&gt; it&amp;#8217;s a mutable array, in which case you have to do the all-too-familiar dance&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray *mutableArray;
if ([a isKindOfClass:[NSMutableArray class]]) {
    mutableArray = a;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At which point you proceed to mutate &lt;code&gt;mutableArray&lt;/code&gt;, which is also &lt;code&gt;a&lt;/code&gt;, possibly ignoring the fact that &lt;code&gt;mutableArray&lt;/code&gt; may be &lt;code&gt;nil&lt;/code&gt;. Or maybe you return early if it&amp;#8217;s &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Casting is Dangerous&lt;/h2&gt;

&lt;p&gt;If you cast between object that are the same foundational type, no work is done. What I mean is that if you cast from an &lt;code&gt;float&lt;/code&gt; to an &lt;code&gt;int&lt;/code&gt;, since they&amp;#8217;re represented differently in the machine, some work is done to represent that &lt;code&gt;float&lt;/code&gt; as a &lt;code&gt;int&lt;/code&gt; that changes the value you&amp;#8217;re working with.&lt;/p&gt;

&lt;p&gt;In Objective-C, when we&amp;#8217;re dealing with objects, we&amp;#8217;re dealing with pointers to objects. These are really &lt;em&gt;just&lt;/em&gt; pointers. They represent a memory address, and not a real usable value. So if you cast an &lt;code&gt;NSArray&lt;/code&gt; to an &lt;code&gt;NSMutableArray&lt;/code&gt;, nothing happens. Same if you case an object to an &lt;code&gt;int *&lt;/code&gt;. (Which is coincidentally prohibited by &lt;span class=&quot;caps&quot;&gt;ARC&lt;/span&gt; for similar reasons that &lt;span class=&quot;caps&quot;&gt;ARC&lt;/span&gt; prohibits putting objects in structs&amp;#8230; It&amp;#8217;s necessary to make certain restrictions in order to ensure the correctness of the program, which is what &lt;span class=&quot;caps&quot;&gt;ARC&lt;/span&gt; concerns itself with. But anyway, casting an object to an &lt;code&gt;int *&lt;/code&gt; used to be perfectly valid, if slightly insane.)&lt;/p&gt;

&lt;p&gt;Almost every time I see a cast involving an Objective-C object, I get very worried. Objective-C has a lovely type system. We should use it. Our compiler helps us move finding bugs from run time to compile time. Casting is a way to tell the compiler &amp;#8220;don&amp;#8217;t worry, I know what I&amp;#8217;m doing.&amp;#8221; But I&amp;#8217;ve seen too many cases of people not knowing what they&amp;#8217;re doing. They forget that method signatures specify a more general type for a reason. If you are passed an object with type &lt;code&gt;id&amp;lt;MyProtocol&amp;gt;&lt;/code&gt;, you shouldn&amp;#8217;t even assume you&amp;#8217;re working with an &lt;code&gt;NSObject&lt;/code&gt;. And if you&amp;#8217;re passing around raw &lt;code&gt;id&lt;/code&gt;s all the time, you should strongly consider coming up with good protocols. Or maybe you&amp;#8217;re taking a parameter that may be one of two very distinct types you say? Well that&amp;#8217;s certainly odd, but I hope you&amp;#8217;re at least doing the Objective-C casting dance.&lt;/p&gt;

&lt;h2&gt;Getting Rid of the Casting Dance&lt;/h2&gt;

&lt;p&gt;I wrote the casting dance out above. But here it is again with a little more detail.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray *ma;
if ([a isKindOfClass:[NSMutableArray class]]) {
    // You have a mutable array!
    ma = a;
} else {
    // You don&amp;#39;t have a mutable array!
}
// `ma` is `nil` if `a` wasn&amp;#39;t originally an `NSMutableArray`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I want to get rid of that, and many things like it. So this project aims to reduce the above down to this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray *ma = [NSMutableArray cast:a];
// `ma` is `nil` if `a` wasn&amp;#39;t originally an `NSMutableArray`
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Conditional Code&lt;/h3&gt;

&lt;p&gt;Often what we really want to do is run certain code if an object is of a specific type.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if ([a isKindOfClass:[NSMutableArray class]]) {
    NSMutableArray *ma = a;
    [ma addObject:x];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Technically, this could have the same effect&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[[NSMutableArray cast:a] addObject:x];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Practically, this ends up getting more complex. Likely I will want to include ways of running a block with the passed object if the object is the kind that&amp;#8217;s expected. Maybe something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[NSMutableArray cast:a intoBlock:^(NSMutableArray *ma){
    [ma addObject:x];
    [ma addObject:y];
    [ma addObject:z];
}];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or even&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;BOOL success = [NSMutableArray cast:a intoBlock:^(NSMutableArray *ma){
                   [ma addObject:x];
                   [ma addObject:y];
                   [ma addObject:z];
}];
if (!success){
    // Do whatever it is you need to do if `a` is not mutable...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;But what&amp;#8217;s the point?&lt;/h2&gt;

&lt;h3&gt;Conciseness&lt;/h3&gt;

&lt;p&gt;It is more concise, while also being idiomatic.&lt;/p&gt;

&lt;h3&gt;Education&lt;/h3&gt;

&lt;p&gt;Every Objective-C programmer knows how to cast. Sadly, many don&amp;#8217;t understand that it doesn&amp;#8217;t give you any guarantees. It just tells the compiler &amp;#8220;shut up, I know what I&amp;#8217;m doing&amp;#8221;. &lt;a href=&quot;https://stackoverflow.com/questions/18043971/fast-enumeration-does-not-give-correct-object-type-in-objective-c&quot;&gt;Here&amp;#8217;s at least one example more related to fast enumeration&lt;/a&gt;. But surely you&amp;#8217;ve explained this sort of thing to somebody in the past? It&amp;#8217;s an honest enough mistake to make.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s too easy to gloss over the casting dance, correct code that looks very familiar (and &lt;code&gt;if&lt;/code&gt; and a cast), and not realize that there&amp;#8217;s a &lt;em&gt;very&lt;/em&gt; important technical decision being made there. I would rather somebody come across a weird &lt;code&gt;cast:&lt;/code&gt; method in my code base and ask why it&amp;#8217;s there. Then I can go on about the dangers of casting.&lt;/p&gt;

&lt;h3&gt;Clarity&lt;/h3&gt;

&lt;p&gt;The closer that code can read to prose the better the intent of the programmer can be understood. It also means that there&amp;#8217;s fewer places for bugs to creep in. Consider doing something with items in an array.&lt;/p&gt;

&lt;p&gt;We used to do things this way. Novices a day into a &amp;#8220;learn to code&amp;#8221; camp can read that. Loops are boring (and error prone).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSArray *a;
for (int i = 0; i &amp;#38;lt; a.count; i++) {
  NSLog(@&amp;#34;%@&amp;#34;, a[i]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then fast enumeration came along, and off-by-one and out-of-bounds errors became rarer.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSArray *a;
for (id object in a) {
  NSLog(@&amp;#34;%@&amp;#34;, object);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And I even prefer block enumeration sometimes because it puts the semantics of what we&amp;#8217;re doing in the message we&amp;#8217;re sending instead of in understanding the syntactic sugar of fast enumeration.&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
NSArray *a;
[a enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@&amp;#34;%@&amp;#34;, a);
}];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is especially helpful when dealing with other collection types. Some people don&amp;#8217;t know off the top of their heads whether fast enumeration of a dictionary iterates over objects or keys.&lt;/p&gt;

&lt;h2&gt;Digress and Reflect&lt;/h2&gt;

&lt;p&gt;With all the recent chatter about Apple needing to &lt;a href=&quot;http://ashfurrow.com/blog/we-need-to-replace-objective-c&quot;&gt;replace Objective-C&lt;/a&gt;, one common call is to move away from C. That &amp;#8220;we&amp;#8217;re one &lt;code&gt;NULL&lt;/code&gt; pointer dereference away from a crash&amp;#8221;. I don&amp;#8217;t remember the last time code I&amp;#8217;ve worked closely with crashed from a &lt;code&gt;NULL&lt;/code&gt; pointer dereference. It happens when you&amp;#8217;re using C language features, and most code where you can&amp;#8217;t avoid C is code that belongs in C (image manipulation, &lt;span class=&quot;caps&quot;&gt;DSP&lt;/span&gt;). I think it&amp;#8217;s clear that the very best Objective-C developers need to know and love C inside and out. But you can make a lot of pretty solid apps without knowing much about C.&lt;/p&gt;

&lt;p&gt;I think it&amp;#8217;s &lt;em&gt;much&lt;/em&gt; more common for an app to crash with &lt;code&gt;unrecognized selector sent to instance&lt;/code&gt;. And here&amp;#8217;s why I brought up the &amp;#8220;Apple needs to drop Objective-C&amp;#8221; thing. Objective-C is remarkable for its powers of introspection and runtime manipulation. What I&amp;#8217;ve described above is possible. I think it will be easy. So I&amp;#8217;m making a project on GitHub, and starting work on it tonight.&lt;/p&gt;</description>
				<link>http://www.fabiancanas.com/entry/safe-casting-obj-c</link>
				<guid>http://www.fabiancanas.com/entry/safe-casting-obj-c</guid>
				<pubDate>Fri, 14 Feb 2014 02:36:23 MST</pubDate>
				</item>
				
				<item>
				<title>OHMKit</title>
				<description>&lt;p&gt;I&amp;#8217;ve written Objective-C code to turn a service response into a fully hydrated model object too many times. It also seems &lt;a href=&quot;http://www.merowing.info/2013/07/stop-writing-data-parsing-code-in-your-apps/&quot;&gt;I&amp;#8217;m not the only one&lt;/a&gt;. Just a couple of weeks ago, I decided to put together &lt;a href=&quot;https://github.com/fcanas/OHMKit&quot;&gt;OHMKit&lt;/a&gt; so I&amp;#8217;d never have to write boilerplate code for object mapping again.&lt;/p&gt;

&lt;p&gt;Check out the source on &lt;a href=&quot;https://github.com/fcanas/OHMKit&quot;&gt;GitHub&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/fcanas/OHMKit&quot;&gt;OHMKit&lt;/a&gt; is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Mixin&quot;&gt;mixin&lt;/a&gt; to make any Objective-C class easier to hydrate from a dictionary representation, such as you might get from a web service. It makes it easy to keep any direct knowledge of the idiosyncrasies of the service you&amp;#8217;re consuming tucked away in a single place.&lt;/p&gt;

&lt;p&gt;It exists because &lt;a href=&quot;https://github.com/RestKit/RestKit&quot;&gt;RestKit&lt;/a&gt; (which is awesome by the way), is sometimes too big, heavy, and indirect.&lt;/p&gt;

&lt;p&gt;This project doesn&amp;#8217;t know about networks. Use &lt;a href=&quot;https://github.com/AFNetworking/AFNetworking&quot;&gt;AFNetworking&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This project doesn&amp;#8217;t know about routes. Use &lt;a href=&quot;https://github.com/jverkoey/sockit&quot;&gt;SOCKit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This project doesn&amp;#8217;t know about CoreData. It will not manage graphs of entities for you quite like RestKit does. But it &lt;em&gt;is&lt;/em&gt; built on &lt;span class=&quot;caps&quot;&gt;KVO&lt;/span&gt;, and does not care about your model objects&amp;#8217; super class. So you can safely make subclasses of &lt;code&gt;NSManagedObject&lt;/code&gt; mappable.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;h3&gt;Basic Mapping&lt;/h3&gt;

&lt;p&gt;Given a model&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface MYModel : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *favoriteWord;
@property (nonatomic, assign) NSInteger favoriteNumber;
@end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Anywhere in you application, make the model mappable, and assign it a dictionary of mappings from the keys a service will provide to the keys your actual model object uses.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;OHMMappable([MYModel class]);
OHMSetMapping([MYModel class], @{@&amp;#34;favorite_word&amp;#34;  : @&amp;#34;favoriteWord&amp;#34;,
                                 @&amp;#34;favorite_number&amp;#34;: @&amp;#34;favoriteNumber&amp;#34;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And now anywhere in your application, objects of the class &lt;code&gt;MYModel&lt;/code&gt; can be hydrated with a dictionary from a service whose keys will be translated by the mapping dictionary you provided.&lt;/p&gt;

	&lt;p&gt;&lt;pre&gt;&lt;code&gt;MYModel *testModel = [[MYModel alloc] init];&lt;/p&gt;

	&lt;p&gt;[testModel setValuesForKeysWithDictionary:@{&lt;code&gt;&amp;#34;name&amp;#34;           : &lt;/code&gt;&amp;#8220;Fabian&amp;#8221;,
                                            &lt;code&gt;&amp;#34;favorite_word&amp;#34;  : &lt;/code&gt;&amp;#8220;absurd&amp;#8221;,
                                            &lt;code&gt;&amp;#34;favorite_number&amp;#34;: &lt;/code&gt;47];&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;h3&gt;Recursive Mapping&lt;/h3&gt;

&lt;p&gt;You don&amp;#8217;t have to do anything special to get recursive mapping of mappable objects. If an object conforming to &lt;code&gt;&amp;#38;lt;OMMappable&amp;#38;gt;&lt;/code&gt; has a property whose type also conforms to &lt;code&gt;&amp;#38;lt;OMMappable&amp;#38;gt;&lt;/code&gt;, and the value for that key in the hydration dictionary is itself a dictionary, we&amp;#8217;ll instantiate a new model object and hydrate it. (If that didn&amp;#8217;t make sense, just read the next code snippet)&lt;/p&gt;

	&lt;p&gt;&lt;pre&gt;&lt;code&gt;&lt;code&gt;interface MYClass : NSObject
&lt;/code&gt;property (nonatomic, strong) NSString *name;&lt;br /&gt;@end&lt;/p&gt;

	&lt;p&gt;&lt;code&gt;interface MYClass2 : NSObject
&lt;/code&gt;property (nonatomic, strong) NSString *name;&lt;br /&gt;&lt;code&gt;property (nonatomic, strong) NSString *favoriteWord;
&lt;/code&gt;property (nonatomic, assign) NSInteger favoriteNumber;&lt;br /&gt;&lt;code&gt;property (nonatomic, assign) MYClass *favoriteObject;
&lt;/code&gt;end&lt;/p&gt;

	&lt;p&gt;OHMMappable([MYClass class]);&lt;/p&gt;

	&lt;p&gt;OHMMappable([MYClass2 class])&lt;br /&gt;OHMSetMapping([MYClass2 class], &lt;code&gt;{&lt;/code&gt;&amp;#8220;favorite_word&amp;#8221;  : &lt;code&gt;&amp;#34;favoriteWord&amp;#34;, 
                               &lt;/code&gt;&amp;#8220;favorite_number&amp;#8221;: &lt;code&gt;&amp;#34;favoriteNumber&amp;#34;, 
                               &lt;/code&gt;&amp;#8220;favorite_object&amp;#8221; : @&amp;#8220;favoriteObject&amp;#8221;});&lt;/p&gt;

	&lt;p&gt;MYModel *testModel = [[MYClass2 alloc] init];&lt;/p&gt;

	&lt;p&gt;NSDictionary *class2Response = &lt;code&gt;{&lt;/code&gt;&amp;#8220;name&amp;#8221;           : &lt;code&gt;&amp;#34;Fabian&amp;#34;, 
                                 &lt;/code&gt;&amp;#8220;favorite_word&amp;#8221;  : &lt;code&gt;&amp;#34;absurd&amp;#34;, 
                                 &lt;/code&gt;&amp;#8220;favorite_number&amp;#8221;: &lt;code&gt;2, 
                                 &lt;/code&gt;&amp;#8220;favorite_object&amp;#8221;: &lt;code&gt;{&lt;/code&gt;&amp;#8220;name&amp;#8221; : @&amp;#8220;Rock&amp;#8221;}};&lt;/p&gt;

	&lt;p&gt;[testModel setValuesForKeysWithDictionary:class2Response];&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;testModel.favoriteObject&lt;/code&gt; is an instance of &lt;code&gt;MYClass&lt;/code&gt; hydrated with &amp;#8220;Rock&amp;#8221; as its name.&lt;/p&gt;

&lt;p&gt;Internally, the new model object is initialized with &lt;code&gt;[[ alloc] init]&lt;/code&gt;, and then hydrated with &lt;code&gt;[ setValuesForKeysWithDictionary:dictionary]&lt;/code&gt;. If you have a model that needs special consideration for initialization, use an adapter block.&lt;/p&gt;

&lt;h3&gt;Adapter Blocks to handle special properties&lt;/h3&gt;

&lt;p&gt;Users can pass a dictionary of blocks for field requiring special handling. Say a service sends back a dictionary that looks something like this:&lt;/p&gt;

	&lt;p&gt;&lt;pre&gt;&lt;code&gt;{
    &amp;#8220;favorite_color&amp;#8221;: [
        122,
        50,
        80
    ],
    &amp;#8220;least_favorite_color&amp;#8221;: [
        121,
        51,
        81
    ]&lt;/p&gt;

	&lt;p&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;and we expect to map it to a model like this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface MYModel : NSObject
@property (nonatomic, strong) UIColor *favoriteColor;
@property (nonatomic, strong) UIColor *leastFavoriteColor;
@end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can adapt the response with an adapter block:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;OHMMappable([MYModel class]);
OHMSetMapping([MYModel class], @&amp;#34;least_favorite_color&amp;#34; : @&amp;#34;leastFavoriteColor&amp;#34;, @&amp;#34;favorite_color&amp;#34; : @&amp;#34;favoriteColor&amp;#34;)
OHMValueAdapterBlock colorFromNumberArray = ^(NSArray *numberArray) {
    return 
                           green:[numberArray[1] integerValue]/255.0
                            blue:[numberArray[2] integerValue]/255.0
                           alpha:1];
};
OHMSetAdapter([MYModel class], @{@&amp;#34;favoriteColor&amp;#34;: colorFromNumberArray, @&amp;#34;leastFavoriteColor&amp;#34;: colorFromNumberArray});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the key for the adapter is the key on the model object, not on the response. And adapters are added for a property, not a type. If the above example had multiple properties that were colors, you would have to set an adapter block for each property. It would be smart to reuse adapter blocks in your code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;OHMValueAdapterBlock&lt;/code&gt; type is a block that takes an &lt;code&gt;id&lt;/code&gt; and returns an &lt;code&gt;id&lt;/code&gt;. &lt;em&gt;i.e&lt;/em&gt; &lt;code&gt;typedef id(^OHMValueAdapterBlock)(id);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;TODO&lt;/h2&gt;

&lt;h3&gt;Undefined Keys&lt;/h3&gt;

&lt;p&gt;The behavior of undefined keys should be configurable at 3 levels:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Raise, because I should know about everything.&lt;/li&gt;
&lt;li&gt;Drop unrecognized keys. We don&amp;#8217;t need them, but we shouldn&amp;#8217;t crash.&lt;/li&gt;
&lt;li&gt;Add keys to a dictionary so that serialization/deserialization can be symmetric&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Option 2 is currently the only behavior, and I&amp;#8217;m inclined to leave is as the default behavior.&lt;/p&gt;

&lt;h3&gt;NSCoding&lt;/h3&gt;

&lt;p&gt;It might be nice if we built a way to make a class &lt;code&gt;NSCoding&lt;/code&gt; compatible if it&amp;#8217;s not already. I like &lt;a href=&quot;https://github.com/github/Mantle&quot;&gt;Mantle&lt;/a&gt;, but I don&amp;#8217;t want to be told what my super class should be (had you noticed?).&lt;/p&gt;</description>
				<link>http://www.fabiancanas.com/entry/introducing-ohmkit-object-mapping</link>
				<guid>http://www.fabiancanas.com/entry/introducing-ohmkit-object-mapping</guid>
				<pubDate>Sat, 27 Jul 2013 02:14:29 MST</pubDate>
				</item>
				
				<item>
				<title>Per File ARC in CocoaPods</title>
				<description>&lt;p&gt;tl;dr&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Pod::Spec.new do |s|
  s.name = &#39;MyPod&#39;
  ...
  non_arc_files = &#39;Classes/FirstNonARCClass.m&#39;,
    &#39;Classes/SecondNonARCClass.m&#39;,
    &#39;Classes/ThirdNonARCClass.m&#39;
  s.requires_arc = true
  s.source_files = &#39;Classes/*.{h,m}&#39;
  s.exclude_files = non_arc_files
  s.subspec &#39;no-arc&#39; do |sna|
    sna.requires_arc = false
    sna.source_files = non_arc_files
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;ARC&lt;/h2&gt;

&lt;p&gt;Automatic reference counting (ARC) is a good thing. It&#39;s a great compile-time technology that makes it easier, in most cases, to not accidentally introduce trivial memory leaks in modern Objective-C apps.&lt;/p&gt;

&lt;p&gt;But sometimes, you have older code with traditional memory management; it&#39;s not really worth converting to ARC if it&#39;s correct, and possibly finely tuned. And sometimes ARC introduces &lt;a href=&quot;http://www.learn-cocos2d.com/2013/03/confirmed-arc-slow/&quot;&gt;a little unnecessary overhead&lt;/a&gt; that can balloon out of control when handling large numbers of objects. There are also good reasons to put Objective-C objects into structs or other odd places that ARC prohibits or makes difficult (Objective-C++ anyone?).&lt;/p&gt;

&lt;p&gt;That said, ARC is almost always a good idea. And so disabling ARC should be the exception, not the rule.&lt;/p&gt;

&lt;h2&gt;CocoaPods&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://cocoapods.org/&quot;&gt;CocoaPods&lt;/a&gt; is an excellent dependency management system for Objective-C projects. Dependencies are distributed as &#39;Pods&#39; which are described in podpecs. A podspec is a kind of build description, that specifies where the source is to be found, dependencies it has, and how it should be configured and compiled.&lt;/p&gt;

&lt;p&gt;CocoaPods lets you specify ARC on a spec or subspec basis, but doing so on a per-file basis is not planned for the spec DSL. The &lt;a href=&quot;https://github.com/CocoaPods/CocoaPods/issues/589#issuecomment-9350801&quot;&gt;suggested way of doing this&lt;/a&gt; is a little unsatisfactory because it has you listing each file to exclude twice. Luckily we&#39;re working with ruby and we can just exctract the list of files to exclude into a list we can use to exclude from the sources and include in a non-ARC subspec.&lt;/p&gt;
</description>
				<link>http://www.fabiancanas.com/entry/per-file-arc-in-cocoapods</link>
				<guid>http://www.fabiancanas.com/entry/per-file-arc-in-cocoapods</guid>
				<pubDate>Sat, 04 May 2013 03:11:04 MST</pubDate>
				</item>
				
    </channel>
</rss>
<script pagespeed_no_defer="" type="text/javascript">(function(){var e=window,f=Math,g="round",h="performance";e.pagespeed=e.pagespeed||{};var q=e.pagespeed;
q.getResourceTimingData=function(){if(e[h]&&(e[h].getEntries||e[h].webkitGetEntries)){for(var r=0,s=0,k=0,t=0,l=0,u=0,m=0,v=0,n=0,w=0,p=0,c={},d=e[h].getEntries?e[h].getEntries():e[h].webkitGetEntries(),b=0;b<d.length;b++){var a=d[b].duration;0<a&&(r+=a,++k,s=f.max(s,a));a=d[b].connectEnd-d[b].connectStart;0<a&&(u+=a,++m);a=d[b].domainLookupEnd-d[b].domainLookupStart;0<a&&(t+=a,++l);a=d[b].initiatorType;c[a]?++c[a]:c[a]=1;a=d[b].requestStart-d[b].fetchStart;0<a&&(w+=a,++p);a=d[b].responseStart-d[b].requestStart;
0<a&&(v+=a,++n)}return"&afd="+(k?f[g](r/k):0)+"&nfd="+k+"&mfd="+f[g](s)+"&act="+(m?f[g](u/m):0)+"&nct="+m+"&adt="+(l?f[g](t/l):0)+"&ndt="+l+"&abt="+(p?f[g](w/p):0)+"&nbt="+p+"&attfb="+(n?f[g](v/n):0)+"&nttfb="+n+(c.css?"&rit_css="+c.css:"")+(c.link?"&rit_link="+c.link:"")+(c.script?"&rit_script="+c.script:"")+(c.img?"&rit_img="+c.img:"")}return""};q.getResourceTimingData=q.getResourceTimingData;})();
(function(){var c=encodeURIComponent,g=window,h="performance";g.pagespeed=g.pagespeed||{};var l=g.pagespeed,m=function(f,a,d,b){this.c=f;this.a=a;this.b=d;this.d=b};l.beaconUrl="";
var n=function(f){var a=f.c,d=g.mod_pagespeed_start,b=Number(new Date)-d,a=a+(-1==a.indexOf("?")?"?":"&"),a=a+"ets="+("load"==f.a?"load:":"unload:"),a=a+b;if("beforeunload"!=f.a||!g.mod_pagespeed_loaded){a+="&r"+f.a+"=";if(g[h]){var b=g[h].timing,e=b.navigationStart,k=b.requestStart,a=a+(b[f.a+"EventStart"]-e),a=a+("&nav="+(b.fetchStart-e)),a=a+("&dns="+(b.domainLookupEnd-b.domainLookupStart)),a=a+("&connect="+(b.connectEnd-b.connectStart)),a=a+("&req_start="+(k-e)),a=a+("&ttfb="+(b.responseStart-
k)),a=a+("&dwld="+(b.responseEnd-b.responseStart)),a=a+("&dom_c="+(b.domContentLoadedEventStart-e));g[h].navigation&&(a+="&nt="+g[h].navigation.type);e=-1;b.msFirstPaint?e=b.msFirstPaint:g.chrome&&g.chrome.loadTimes&&(e=Math.floor(1E3*g.chrome.loadTimes().firstPaintTime));e=e-k;0<=e&&(a+="&fp="+e)}else a+=b;l.getResourceTimingData&&g.parent==g&&(a+=l.getResourceTimingData());a+=g.parent!=g?"&ifr=1":"&ifr=0";"load"==f.a&&(g.mod_pagespeed_loaded=!0,(b=g.mod_pagespeed_num_resources_prefetched)&&(a+=
"&nrp="+b),(b=g.mod_pagespeed_prefetch_start)&&(a+="&htmlAt="+(d-b)));l.panelLoader&&(d=l.panelLoader.getCsiTimingsString(),""!=d&&(a+="&b_csi="+d));l.criticalCss&&(d=l.criticalCss,a+="&ccis="+d.total_critical_inlined_size+"&cces="+d.total_original_external_size+"&ccos="+d.total_overhead_size+"&ccrl="+d.num_replaced_links+"&ccul="+d.num_unreplaced_links);""!=f.b&&(a+=f.b);document.referrer&&(a+="&ref="+c(document.referrer));a+="&url="+c(f.d);l.beaconUrl=a;(new Image).src=a}};
l.e=function(f,a,d,b){var e=new m(f,a,d,b);g.addEventListener?g.addEventListener(a,function(){n(e)},!1):g.attachEvent("on"+a,function(){n(e)})};l.addInstrumentationInit=l.e;})();

pagespeed.addInstrumentationInit('http://1-ps.googleusercontent.com/beacon?org=120_1_li', 'load', '&id=1409530616224103', 'http://www.fabiancanas.com/new/feed.xml');</script>