Get It Down

taking notes cause i'll forget

Keeping Count With OSAtomic

OSAtomic is a many splendored thing, with many low level functions to simplify manipulating your data in a thread safe manner.

OSAtomic

One of the things it does best is make integer math really easy to do without the weight of a general purpose lock.

Here’s a dead simple counter that just bumps an integer on each access:

#import <Foundation/Foundation.h>
#import <libkern/OSAtomic.h>

static int32_t _count = 0;
static inline int32_t MonotonicCounter(void)
{
    return OSAtomicIncrement32(&_count);
}

OSAtomic is worth spending some time with, there’s lots of goodies.

Accessible Buttons

Apple provides an astonishingly good framework for making iOS apps equally useful to sighted and unsighted users.

Some preliminary reading if you’re interested:

I’d like to offer a short take on one of the most common mistakes that iOS developers make when they’re not thinking about accessibility, Buttons.

UIButton instances can be a configured a lot of ways:

  • With text
  • With text and an image
  • With just an image
  • With custom drawing
  • Etc

I’ve created a sample project that you can grab to see how buttons can be configured to make sure that iOS’s Voice Over framework can see what that buttons content and purpose is.

Grab the code, give it a look, and remember these takeaways:

  • Set the buttons title any time you can
  • If you can’t set the buttons title, set its accessibilityLabel
  • A picture of text, is not the same as text if you’re not sighted

Objective-C Object Mapping

I think basically any Objective-C developer who has spent some time on the platform has written a half dozen different ways to turn a dictionary into a model object:

SomeModelObject *model = [[SomeModelObject alloc] init];
model.someProperty = [someDictionary objectForKey:@"someProperty"];
//etc

or a little better

@implementation SomeModelObject
- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self)
    {
        self.someProperty = [dictionary objectForKey:@"someProperty"];
    }
    return self;
}

and then there’s transformations, like turning a string into a date, etc.

While I was at Bottle Rocket, we decided to formalize this process and write something that would make it easy and consistent to turn dictionaries into model objects and back into dictionaries.

When I left Bottle Rocket, I had gotten so used to having this tool at my disposal I decided, with BR blessing, to build my own.

ESObjectMap is my 2.0 take on this problem:

  • Easy configuration from a dictionary for native model objects and core data objects
  • Mismatched field name mapping
  • Built in basic data transforms for common data types and an easy path to build custom transforms
  • Mapping of native model objects back into dictionaries

Couple of non-feature side notes:

  • Written for ARC
  • While the samples are of rendering to and from JSON, this is really just about dictionaries to objects and back
  • Uses my property introspection logic, so you have lots of meta data to work with when performing mappings and transforms.

Check out the sample project, let me know what you think. I’m pretty happy with where it is now, but there’s always room for improvement.

Objective-C Declared Property Introspection

Here’s a fun little NSObject category that allows you to pop out an object’s declared properties into a dictionary keyed by the property names.

NSObject+PropertyDictionary

The values in the dictionary are ESDeclaredPropertyAttributes objects that should give you most of the data you’d want for most kinds of properties most of the time. (A comprehensive object that gives you all the possible data stored into a declared properties would be a complicated kitchen sink and only marginally more usable)

typedef enum {
    AssignStorage, // assign is the default declared property setter type
    StrongStorage,
    CopyStorage,
    ReadOnlyStorage
} PropertyStorageMethod;

// Not going to try to support
// enum, struct, union, int *, void *, long, short, signed, unsigned, etc
typedef enum
{
    IDType,
    ObjectType,
    CharType,
    DoubleType,
    FloatType,
    IntType,
    UnsupportedType
} PropertyStorageType;

@interface ESDeclaredPropertyAttributes : NSObject

@property (strong, nonatomic) NSString *classString;
@property (strong, nonatomic) NSString *name;
@property (nonatomic) PropertyStorageType storageType;
@property (nonatomic) SEL getter;
@property (nonatomic) SEL setter;
@property (nonatomic) PropertyStorageMethod storageMethod;
@property (nonatomic) BOOL readOnly;
@property (nonatomic) BOOL nonatomic;
@property (nonatomic) BOOL dynamic;

@end