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.

Header.h

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

@implementation SPTAppRemoteUserAPIImplementation

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

How to add XCode subproject

1 Comment

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

https://www.cocoanetics.com/2011/12/sub-projects-in-xcode/
https://www.cocoanetics.com/2012/01/helping-xcode-find-library-headers/

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.

MyClass.h

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

MyClass.m

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
                               code:MyClassInvalidThing1
                           userInfo:nil];
}

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

http://stackoverflow.com/questions/322597/class-vs-import

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.

NS_ASSUME_NONNULL_BEGIN

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

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

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


NS_ASSUME_NONNULL_END

http://stackoverflow.com/questions/32452889/difference-between-nullable-nullable-and-nullable-in-objective-c
https://developer.apple.com/swift/blog/?id=25

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.

FooBaseTest.h

#import 

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

FooBaseTest.m

@interface FooBaseTest ()
@end

@implementation FooBaseTest

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

@end

Then you extend it in your test class like this

SomeTest.m

@interface SubclassTest : FooBaseTest

@end

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

Older Entries Newer Entries

%d bloggers like this: