@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.

Autolayout Basics 1

Leave a comment

Here is how you add two buttons and space them relative to each other.

Screen Shot 2016-03-07 at 11.53.26 AM.png

Create a new app, add a button to the middle, and then click the icon at the bottom to center button vertically and horizontally.

Screen Shot 2016-03-07 at 11.55.59 AM.png

Screen Shot 2016-03-07 at 12.00.48 PM.png

Button1 should now be centered. Add another button underneath Button1.

Screen Shot 2016-03-07 at 12.03.25 PM.png

And make it hang below Button1 by doing the following.

Screen Shot 2016-03-07 at 12.03.51 PM.png


Screen Shot 2016-03-07 at 12.04.01 PM.png

Screen Shot 2016-03-07 at 12.04.14 PM.png

Screen Shot 2016-03-07 at 12.05.42 PM.png


Screen Shot 2016-03-07 at 12.05.57 PM.png


You should now be done!




Nullability and Objective-C

Leave a comment


The Core: _Nullable and _Nonnull

_Nullable is a pointer that may have a NULL or nil value
_NonNull should not. The compiler will tell you if you break the rules.

– (AAPLListItem * _Nullable)itemWithName:(NSString * _Nonnull)name; @property (copy, readonly) NSArray * _Nonnull allItems;

You can use _Nullable and _NonNull any where you need a pointer. A nicer way to write these within method declarations however is as follows:

_Nullable -> nullable
_Nonnull -> nonnull

– (nullable AAPLListItem *)itemWithName:(nonnull NSString *)name; – (NSInteger)indexOfItem:(nonnull AAPLListItem *)item;

or for properties

@property (copy, nullable) NSString *name; @property (copy, readonly, nonnull) NSArray *allItems;

Audited regions

You can mark certain regions in your code as audited for nullability. Within these regions any simple pointer type will be assumed to be nonnull.

@interface AAPLList : NSObject <NSCoding, NSCopying>
// ...
- (nullable AAPLListItem *)itemWithName:(NSString *)name;
- (NSInteger)indexOfItem:(AAPLListItem *)item;
@property (copy, nullable) NSString *name;
@property (copy, readonly) NSArray *allItems;
// ... @end

Objective-C nil / Nil / NULL / NSNull

Leave a comment

Here are some notes from Mattt excellent write up on nil / Nil and Null.
Read the full post here.

Read more here http://nshipster.com/nil/

C represents nothing as 0 for primitives, NULL, for pointers.

Objective-C nothing is nil. nil is an object pointer to nothing.Although semantically distinct from NULL, they are technically equivalent to one another.

On the framework level, Foundation defines NSNull, which defines a class method +null, which defines the singleton NSNull object. NSNull is different from nil or NULL, in that it is an actual object, rather than a zero value.

Foundation/NSObjcCRuntime.h also defines Nil as a class pointer to nothing. This lesser-known title-case cousin of nil doesn’t show up much very often, but it’s at least worth nothing.

Newly alloc’d NSObjects start life off with their contents set to 0. This means that all points that object has to other points bigs as nil.

Most notable behavior of nil, though, is that is can have messages sent to it. This would crash other programs in C++. Which greatly simplifies expressions, as it obviates the need to check for nil before doing anything.

Synthesizing properties in protocols

Leave a comment

When you define properties in a protocol, and register yourself for it later, you need to remember to synthesize it after in the .m file.

So define the protocol here.


@protocol Transport <NSObject>

@property (nonatomic, weak) id<TransportDelegate> delegate;
@property (nonatomic, assign, readonly) TransportState transportState;


Implement it here.


@interface StreamTransport : NSObject<SPTAppRemoteTransport, NSStreamDelegate>

And this synthesize it in the .m file here.


@implementation StreamTransport

@synthesize delegate = _delegate;
@synthesize transportState = _transportState;

You have to do it like this because this is your first change to get your handles on the actual property. So you need to synthesize it manually because you weren’t able to define it like a regularly property previously.

Older Entries Newer Entries


Get every new post delivered to your Inbox.

Join 348 other followers

%d bloggers like this: