Objective-C Object Initialization

Leave a comment

Here are some guidelines and macros for using designated initializers in objective-c.

Basically you pick your own initializer as the designated one. And the assumption is that it will call it’s parent when initializing.

If you have many convenience initializers, they should all come down to one main initializer. You can make that one up with the NS_DESIGNATED_INITIALIZER macro also.


@interface Foo : NSObject

* Initializes a new user API.
* @discussion This is the designated initializer.
* @param client A client responsible for sending and receiving messaging to Spotify app.
- (instancetype)initWithClient:(Client *)client NS_DESIGNATED_INITIALIZER;

#pragma mark Unavailable Initializers

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;


@implementation SPTAppRemoteUserAPIImplementation

- (instancetype)initWithClient:(Client *)client
   self = [super init];
   if (self) {
       _client  = client;
   return self;

How to add XCode subproject

Leave a comment

When you have an XCode project and you want it to depend on another project (say because you want to depend on the output binary), just drag the project file in from the finder and setup the Target and Binary dependencies so it looks like this:

Screen Shot 2016-04-04 at 3.07.12 PM.png


Drag in the subproject into your main project folder

Screen Shot 2016-04-04 at 3.28.37 PM.png

Screen Shot 2016-04-04 at 3.28.44 PM.png


Then select your project -> Build phases to add the Target Dependencies

Screen Shot 2016-04-04 at 3.28.55 PM.png


Then add the Link Binary with Libraries

Screen Shot 2016-04-04 at 3.29.05 PM.png

Now your project will build with the subproject and always get the latest binary.

Nice pattern for defining NSError constants

Leave a comment

Say you want some type safety around how you define NSErrors. Here is one way to do it.


extern NSString * const MyClassErrorDomain;
typedef NS_ENUM(NSUInteger, MyClassError) {
    MyClassInvalidThing1 = 1,
    MyClassInvalidThing2 = 2,


NSString * const MyClassErrorDomain = @"MyClassErrorDomain";

- (void)tryWakeUpAndReconnect;
    if (self.shouldTryWakeup) {
        [self.delegate failedWithError:[self errorThing1];

#pragma mark Errors

// if a Spotify app could not be found or woken up
- (NSError *)errorThing1
    return [NSError errorWithDomain:MyClassErrorDomain

How to see current auto layout issues

1 Comment

Screen Shot 2016-03-23 at 6.26.56 AM.png

@class vs #import Objective-C

Leave a comment


Three simple rules:

  • Only #import the super class, and adopted protocols, in header files.
  • #import all classes, and protocols, you send messages to in implementation.
  • Forward declarations for everything else.

If you do forward declaration in the implementation files, then you probably do something wrong.

How to use Nullable Objective-c

Leave a comment

The convention we are currently using is to to a blanket nullable on every header file, and then to null out those specific parameters that can be null.

Callbacks are typically nullable – so it’s OK to mark them as such.


- (void)pause:(PlayerCallback __nullable)callback;

// these pairs are the name, but prefer the first one
_Nullable instead of

typedef void (^RemoteCallback)(id _Nullable result, NSError * _Nullable error);
typedef void (^RemoteCallback)(id __nullable result, NSError * __nullable error);

- (void)pause:(nullable SPTAppRemoteCallback)callback;
- (void)pause:(SPTAppRemoteCallback __nullable)callback;

@property (nonatomic, copy, nullable) NSArray *arguments;
@property (nonatomic, strong) NSArray * __nullable arguments;

- (void)fetchImageWithId:(NSString *)imageId callback:(RemoteCallback _Nullable)callback;
- (void)fetchImageWithId:(NSString *)imageId callback:(RemoteCallback __nullable)callback;

+ (NSArray *)appendOptionalArguments:(NSArray * __nullable)arguments



How to make Objective-C base test class

Leave a comment

Sometimes you want a base class for your unit tests. You can make one in objective-c like this.



@interface FooBaseTest : XCTestCase
@property (nonatomic, strong) SomeProperty *someProperty;


@interface FooBaseTest ()

@implementation FooBaseTest

- (void)setUp {
    [super setUp];
    self.someProperty = // whatever


Then you extend it in your test class like this


@interface SubclassTest : FooBaseTest


You can do it this way, or you do it is also via composition.

Older Entries Newer Entries

%d bloggers like this: