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


Adding a subproject makes it easier to make changes to your subproject, and then see those changes instantly reflected in the parent project. To add a subproject, you subproject must first be a static library (unless it’s an app extension).

But once you’ve got a static library subproject, here’s how you add it.

Drag the subproject in via finder

Skärmavbild 2016-12-23 kl. 9.30.53 fm.png

Then select the Main project -> MainApp Target -> Build Phases -> Target Dependency


Skärmavbild 2016-12-23 kl. 9.34.47 fm.png

Then add the Link Binary with Libraries

Skärmavbild 2016-12-23 kl. 9.36.28 fm.png

If you have setup your staticlibrary like I explained here you now need to tell the main project where it can find the public header.

Under Build Settings search for User Header Search Paths and add $(CONFIGURATION_BUILD_DIR) (non-recursive)

Skärmavbild 2016-12-23 kl. 7.59.50 fm.png

With that done, you should now be able to import the static library header and use it in your main app.

Skärmavbild 2016-12-23 kl. 8.02.29 fm.png

Links that help


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

1 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: