Chris Brandow

A Possible NSUserDefaults Alternative.

It is often the case that we need to persist a few bits of info or at least a single object, like a “user”. Core data is too big for this, NSUserDefaults is sometimes not the best practice use case, and does not provide compiler auto-completion. The correct way is to create a (singleton) object for the data, and use NSKeyedArchiver for persistence, which is not super fast, but is straightforward and speed does not matter for small classes like this. It is not hard to use, but there are a few things that you have to do correctly. For a quick refresher, there are lots of examples, but this one is fine

  1. provide a method to encode each property of the object by encodeForKey by giving it an NSString key that is the same name as the property.
  2. Provide a method to decode each property the same way.

This leads to two problems:

  1. Must manually match the @”keyName” to the @property name. The compiler does not help you here.
  2. Must remember to add/modify the @”keyName” every time you add/modify a property name that you want to persist.

The most tedious (and treacherous) way:

[coder encodeObject:name forKey:@"name"]; [coder encodeObject:subgroups forKey:@"jobs"]; [coder encodeObject:tasks forKey:@"location"];

The less tedious way, using an array of keys and valueForKey:

“`

  • (NSArray *)keysForEncoding { return [NSArray arrayWithObjects:@”name”, @”jobs”, @”location”, nil]; }
  • (void)encodeWithCoder:(NSCoder *)aCoder {

    for (NSString *key in self.keysArray) { [aCoder encodeObject:[self valueForKey:key] forKey:key]; } } “`

The worst thing is that both still have boilerplate which is not compiler checked.

So, thinking about this, I was trying to come up with a way to use introspection to eliminate the manual entry of a key array. This led me to a Objective-C runtime method that fit the bill.

“` + (NSArray *)keysFromProperties {

u_int count;

objc_property_t* properties = class_copyPropertyList([self class], &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];

for (int i = 0; i < count ; i++) {
    [propertyArray addObject:@(property_getName(properties[i]))];
}
free(properties);

return [NSArray arrayWithArray:propertyArray];

} “`

So now my encoding does not depend on any array that I must manually fill, it is just a method and looks like this:

“` - (void)encodeWithCoder:(NSCoder *)aCoder {

for (NSString *key in [[self class] keysFromProperties]) {
    [aCoder encodeObject:[self valueForKey:key] forKey:key]; 
}

} “`

So I can simply add object and primitive data properties directly in the @interface, and the class takes care of the rest. I get autocompletion in the compiler, so there are no strings to keep track of.

I have posted the code on Github . I would love feedback.

Swift: Super trivial heads up

In objective-C, when ctrl-dragging things from Interface Builder, in order to make an IBOutlet or IBAction, you can typically rely on the compiler to know which you are intending to make based on where you drag it:

  1. @interface => IBOutlet
  2. @implementation => IBAction

With swift, since there is no distinction between these areas of code, the compiler cannot auto-detect this for you.

So, not a big thing, but just a heads up.

The Soderberghian style, sex, and settings of 'Out Of Sight' »

parislemon:

Noel Murray:

We should acknowledge how great George Clooney is in this movie. Jennifer Lopez is wonderful too—so much so that it’s a shame she didn’t stay the course as a movie star, rather than focusing so much of her energy on pop music—but Clooney has had such a strong post-Out Of Sight career that it’s easy to forget how this film righted his reputation. He’d made a ton of money with the first couple of films he starred in after leaving the TV series ER, but few critics took him seriously as an actor. Around the time Out Of Sight came out, I remember Clooney giving interviews in which he talked about how he was rich enough from Batman & Robin not to have to make any more movies he didn’t believe in. He also said that Soderbergh had cured him of some of his actorly tics, convincing him to stare straight ahead rather than always dipping his chin and bobbing his head. Nathan, you ask about memorable moments, and most of mine involve Clooney’s unexpected maturity and deadpan wit. My favorite example of the latter: When one of Ripley’s goons comes to escort Jack out of his office and says, “There are two ways we can do this,” to which Jack replies, “Yeah, what are they?” As for the former, I think again of that scene at the hotel bar with Karen, when Jack sighs that he’d rather they just be themselves, and bring everything they’ve lived and experienced into the bedroom. “Gary and Celeste, what do they know about anything?” he asks. What changed about Clooney as an actor is that he stopped being a Gary and started being a Jack Foley.

100% agree. This remains my favorite George Clooney role (narrowly ahead of Michael Clayton). And I also view it as the pivotal moment that “righted” his Hollywood ship. He easily could have veered off course as so many others have before him. But it was Out of Sight that put him on the trajectory to become arguably the biggest movie star in the world.

I also believe this is still Steven Soderbergh’s best movie. 

Gonna have to watch this again. I sure did like it the first time around.

Where have all the burglars gone? »

parislemon:

Tallinn on the decrease in crime around the developed world in recent years:

But the sheer scale of the drop—and its broad persistence in the face of the deepest economic depression in a century—make a new crime wave seem unlikely. Policing is still improving; heroin and crack-cocaine consumption continue to fall; and no one is likely to reintroduce lead into petrol. The period of rising crime from the 1950s through to the 1980s looks increasingly like an historical anomaly.

A portion of the theory reminds me a bit of Minority Report’s “pre-crime”. No, not the knowing the future part, but the fact that would-be criminals realize their actions are more likely to be caught in some way, so they are simply thinking twice about doing anything in the first place.

<

p>yeah, but lead is the biggest part

so simple you can see immediately how to build it with 2x6&#8217;s and a 4&#8217;x8&#8217;. But elegant at the same time.

so simple you can see immediately how to build it with 2x6’s and a 4’x8’. But elegant at the same time.

Cable TV Boxes Become 2nd Biggest Energy Drain In Homes »

parislemon:

Ralph Vartabedian on cable boxes:

The seemingly innocuous appliances — all 224 million of them across the nation — together consume as much electricity as produced by four giant nuclear reactors, running around the clock. They have become the biggest single energy user in many homes, apart from air conditioning.

A set-top cable box with a digital recorder can consume as much as 35 watts of power, costing about $8 a month for a typical Southern California consumer. The devices use nearly as much power turned off as they do when they are turned on.

Insane. The title, however is incorrect. The correct title should be: Piece of Shit Cable TV Boxes Become 2nd Bigger Energy Users In Many Homes.