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.

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.

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!

 

 

 

Older Entries Newer Entries

%d bloggers like this: