Protocol delegate example iOS

Leave a comment

WeatherService.h

#import <Foundation/Foundation.h>
#import "Weather.h"

// I am going to call these methods on you...
@protocol WeatherServiceDelegate <NSObject>
-(void)didFetchWeather:(Weather *)weather;
@end

// this is where you register yourself with me
@interface WeatherService : NSObject
-(void)fetchWeather;
@property (nonatomic, weak) id <WeatherServiceDelegate> delegate;
@end

WeatherService.m

#import "WeatherService.h"

@implementation WeatherService

-(void)fetchWeather
{
    // fake weather for now
    Weather *weather = [[Weather alloc] init];
    weather.currentTemperature = -20;

    if ([self.delegate respondsToSelector:@selector(didFetchWeather:)]) {
        [self.delegate didFetchWeather:weather];
    }

}
@end

ViewController.m

#import "WeatherService.h"
#import "ViewController.h"

@interface ViewController () <WeatherServiceDelegate>
@property (weak, nonatomic) IBOutlet UILabel *currentTempLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WeatherService *tempWeatherService = [[WeatherService alloc] init];

    // register ourselves as a delegate for the weather service
    tempWeatherService.delegate = self;

    // call the service - which is in turn calling us...
    [tempWeatherService fetchWeather];
}

-(void)didFetchWeather:(Weather *)weather {
    self.currentTempLabel.text = [NSString stringWithFormat:@"%d", weather.currentTemperature];
}

@end

How to define inner class objective-c

Leave a comment

If you have a test class, and you just want to define another class within it (say for test purposes), define an interface and an implementation near the bottom of the test file. Like this. Note the order here matters. Define the test class first in the file, before you use it in the test (objective-c!).


#import <XCTest/XCTest.h>
#import "SPTAppRemoteServerConnector.h"

// test class to act as the delegate

@interface FakeConnector : NSObject

@end

@interface FakeConnector () <SPTAppRemoteServerConnectorDelegate>

@end


@implementation FakeConnector
- (void)didConnectWithServer
{

}
@end



@interface SPTAppRemoteServerConnectorTests : XCTestCase

@end

@implementation SPTAppRemoteServerConnectorTests

- (void)setUp
{
    [super setUp];
}

- (void)testDelegateGetsFiredWhenServerConnects
{
    FakeConnector *fake = [FakeConnector new];
    fake = nil;

    SPTAppRemoteServerConnector *connector = [SPTAppRemoteServerConnector new];
    connector = nil;
}


@end



How to view crash logs MacOS

Leave a comment

If you build a build error in Xcode

Screen Shot 2016-02-01 at 8.47.58 AM

You can view these logs via a tool built into Apple OS.
Cd to the above directories and open the log files
> cd ~/Library/Logs/DiagnosticReports
> open ruby_xxx.crash

Screen Shot 2016-02-01 at 8.48.18 AM

You can then see from above code that you fail in ruby string.c land. Ruby version error.

This program is called the Console application and you can find it in your applications folder.

Screen Shot 2016-02-01 at 8.49.45 AM

How to fork and clone a repos

Leave a comment

When contributing to an GHE project the first thing to do is to fork the main repository and then clone it to your machine.

This is normally known as a triangular workflow.

Screen Shot 2016-02-01 at 6.48.37 AM

 

  1. Navigate to the GHE page for the project that you want to contribute to (for instance and fork it.
    • Click on the “fork button” in the top right corner of the repo’s page.
    • Click on your account in the modal which appears.
    • You’ll then be taken to your fork.
  2. In the right column of the page you’ll find the git clone URL. Make sure to select the SSH URL.
  3. Clone your fork.
    • git clone git@xxx:$USER/yyy.git &amp;&amp; cd xxx
      

  4. Add the main repository as your upstream remote
    • git remote add upstream git@xxx:yyy.git
      

  5. Disable pushes to upstream, to minimise the risk for mistakes:
    • git remote set-url --push upstream no_push
      

  6. Make sure git pushes to your fork by default.
    • git config remote.pushdefault origin
      

  7. Make sure git only pushes the current branch.
    • git config --global push.default simple
      

  8. Tell git and GHE who you are.
    • git config --global user.name &quot;Your Awesome Name&quot;
      

    • git config --global user.email &quot;${USER}@xxx.com&quot;
      

  9. Optionally (although highly recommended); set your master branch’s remote to upstream. This way you don’t need to manage to master branches, your fork’s is quite useless anyways. Making your life over 9000 times easier.
    • git fetch upstream
      

    • git branch --set-upstream-to=upstream/master master
      

 

Suggested workflow

This is our suggested workflow for day-to-day work, after having setup your fork (origin) of the main repository (upstream).

  1. First, make sure to base your changes on latest master:
    • git checkout master &amp;&amp; git pull upstream master
      

  2. Make a topic branch for your change
    • git checkout -b my-fix
      

    • Note: you must to do this or bad things will happen.
  3. Do the changes and commit them. Keep the commits small and atomic; for instance, don’t do code style changes in the same commit as something that fixes a bug, and don’t do refactors and behavior changes in the same commit.
  4. Push the changes to your fork:
    git push -u origin my-fix
    

  5. Open a pull request (PR) from your topic branch to master in the main repo. Either via;
    • hub pull-request -b xxx:master
      

      or

    • the GitHub web interface.
  6. Mention the relevant people or teams in the body of the pull request using the@username syntax (also works for teams, i.e. @the_org/team_name).
  7. It is OK to merge a PR once it has been reviewed and the reviewer has written a comment that clearly states that it is approved, for instance by writing “+2” or “Approved”.
  8. If merging a PR to upstream conflicts, merge master to your branch and update the PR:
    • git fetch upstream master && git merge upstream/master
      

  9. The person who created the PR is typically the one who is expected to merge it (and fix conflicts if necessary). Although an approved PR can be merged by anyone if needed.
  10. Once your topic branch has been merged into master go ahead and remove it in your own fork.
    • Either via the GHE pull request interface by clicking on “Delete Branch” or,
    • via the CLI: git branch -d my-fix && git push origin :my-fix

 

Autolayout basics

Leave a comment

To align one button to another in storyboard do this. Drag out your second button and center it by control dragging out and selecting ‘Center Horizontal’.

Screen Shot 2015-11-25 at 12.04.18 PM

 

Then, to make your second button trail the first, control drag second to first and select ‘Vertical Spacing’.

Screen Shot 2015-11-25 at 12.03.29 PM

You can then double click on the constraint to set whatever spacing you like.

Screen Shot 2015-11-25 at 12.13.43 PM

To test your layout select ‘Update Frames’ from the triangle located near the bottom of the storyboard frame.

 

Screen Shot 2015-11-25 at 12.04.46 PM

And to see all your constraints,  select this well hidden slider, Hide Document Outline, near the bottom left of the storyboard screen.

Screen Shot 2015-11-25 at 12.15.03 PM

And you should see a frame slide out showing you all the constraints for your current project.

Screen Shot 2015-11-25 at 12.16.20 PM

 

 

How to define protocol Objective-C

Leave a comment

Define your protocol

@protocol RemotePlayer <NSObject>
- (void)play:(void (^)(NSError *error))callback;
- (void)pause:(void (^)(NSError *error))callback;
@end

Define your implementation

@interface RemotePlayerImplementation : NSObject <RemotePlayer>

Use your protocol

id<RemotePlayer> player = [RemotePlayerImplementation new];

How to make generic iOS library

Leave a comment

To add files to your generic iOS library make sure the header file you want to include is in the target header section. Like this.

 

Note – if you click on the headers section and you don’t see the class you want to add, try dragging it from the left project area into the header section. Thank you Johan.

 

 

Screen Shot 2015-11-24 at 7.25.35 AM

Screen Shot 2015-11-24 at 7.25.55 AM

Older Entries

Follow

Get every new post delivered to your Inbox.

Join 340 other followers

%d bloggers like this: