GithubHelp home page GithubHelp logo

nsobject-tap's Introduction

NSObject-Tap License MIT

Version Platform

tap: method for Objective-C borrowed from Ruby. It also supports tapp for print debugging use case from the RubyGem.

Setup

Let's use CocoaPods.

# Podfile
pod 'NSObject-Tap'
$ pod install

or copy two files NSObject+Tap.{h,m} to your project.

Then, import the header file.

#import "NSObject+Tap.h"

Examples

Initilization:

NSArray *array = [[NSMutableArray array] tap:^(NSMutableArray *x) {
    [x addObject:@1];
    [x addObject:@2];
    [x addObject:@3];
}];
Person *person = [[[Person alloc] init] tap:^(Person *p) {
    p.name = @"Your Name";
    p.age = 20;
    p.address = @"Kyoto, Japan";
}];

Shorter codes, reduce local variables:

// NSUserDefaults

// before 1
[[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"1"];
[[NSUserDefaults standardUserDefaults] setObject:@2 forKey:@"2"];
[[NSUserDefaults standardUserDefaults] setObject:@3 forKey:@"3"];

// before 2
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@1 forKey:@"1"];
[defaults setObject:@2 forKey:@"2"];
[defaults setObject:@3 forKey:@"3"];

// after
[[NSUserDefaults standardUserDefaults] tap:^(NSUserDefaults *x) {
    [x setObject:@1 forKey:@"1"];
    [x setObject:@2 forKey:@"2"];
    [x setObject:@3 forKey:@"3"];
}];


// NSNotificationCenter
[[NSNotificationCenter defaultCenter] tap:^(id x) {
    [x addObserver:self
          selector:@selector(willEnterForeground:)
              name:UIApplicationWillEnterForegroundNotification
            object:nil];
    [x addObserver:self
          selector:@selector(didEnterBackground:)
              name:UIApplicationDidEnterBackgroundNotification
            object:nil];
}]

Use tapp:

// Outputs "Hello, world!"
NSString *helloWorld = [@"Hello, world!" tapp];

// Outputs like "<NSObject: 0x7ffbf0402f10>"
id object = [[[NSObject alloc] init] tapp];

// Between method chains
NSArray *filtered = [[[@[ @1, @2, @3, @4, @5 ] tapp] filteredArrayUsingPredicate:evenPredicate] tapp];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.