GithubHelp home page GithubHelp logo

danielsmevik / concisekit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from petejkim/concisekit

1.0 2.0 0.0 296 KB

A set of Objective-C additions and macros that lets you to write code more quickly.

License: MIT License

concisekit's Introduction

ConciseKit 0.0.1

A set of Objective-C additions and macros that lets you to write code more quickly.

$ class

Method Swizzling

    [$ swizzleMethod:@selector(foo) with:@selector(bar) in:[Foo class]]
    [$ swizzleMethod:@selector(foo) in:[Foo class] with:@selector(bar) in:[Bar class]]

    [$ swizzleClassMethod:@selector(foo) with:@selector(bar) in:[Foo class]]
    [$ swizzleClassMethod:@selector(foo) in:[Foo class] with:@selector(bar) in:[Bar class]]

Path

    [$ homePath]     => path to user's home directory
    [$ desktopPath]  => path to user's desktop directory
    [$ documentPath] => path to user's document directory
    [$ appPath]      => path to app directory
    [$ resourcePath] => path to app's resources directory

waitUntil

Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds, checking is done every 0.1 seconds.

    [$ waitUntil:^{ return (BOOL)(someConditionIsMet == YES) }]
    [$ waitUntil:^{ return (BOOL)(someConditionIsMet == YES) } timeOut:10.0]
    [$ waitUntil:^{ return (BOOL)(someConditionIsMet == YES) } timeOut:10.0 interval:0.1]

Singleton

Creating Singletons

    @interface Foo
    - (id)initSingleton; // <= add these to the interface
    + (Foo *)sharedFoo;  // <= where Foo is the class name
    @end

    @implementation Foo
    $singleton(Foo);     // => makes Foo a singleton class

    - (id)initSingleton {
      foo = 1;           // do initialization in -initSingleton method
      bar = 2;
      return self;
    }
    @end

Using Singletons

    $shared(Foo)         // => returns the shared instance
    /* or */
    [Foo sharedFoo]

Macros

General shorthands

    $new(Foo)       => [[[Foo alloc] init] autorelease]
    $eql(foo, bar)  => [foo isEqual:bar]
    $safe(obj)      => (obj == [NSNull null] ? nil : obj)

NSArray shorthands

    $arr(foo, bar)   =>  [NSArray arrayWithObjects:foo, bar, nil]
    $marr(foo, bar)  =>  [NSMutableArray ...]

NSSet shorthands

    $set(foo, bar)   =>  [NSSet setWithObjects:foo, bar, nil]
    $mset(foo, bar)  =>  [NSMutableSet ...]

NSDictionary shorthands

    $dict(v1, k1, v2, k2)  => [NSDictionary dictionaryWithObjectsAndKeys:v1, k1, v2, k2, nil]
    $mdict(v1, k1, v2, k2) => [NSMutableDictionary ...]

NSString shorthands

    $str(@"foo: %@", bar)   => [NSString stringWithFormat:@"foo: %@", bar]
    $mstr(@"foo: %@", bar)  => [NSMutableString ...]

NSNumber shorthands

    $bool(YES)    => [NSNumber numberWithBool:YES]
    $int(123)     => [NSNumber numberWithInt:123]
    $float(123.4) => [NSNumber numberWithFloat:123.4]

    $char(), $double(), $integer(), $long(), $longlong(), $short(),
    $uchar(), $uint(), $uinteger(), $ulong(), $ulonglong(), $ushort()

NSValue shorthands

    $nonretained(), $pointer(), $point(), $range(), $rect(), $size()

Additions

NSArray

    [array $first] => [array objectAtIndex:0]
    [array $last]  => [array lastObject]
    [array $at:1]  => [array objectAtIndex:1]

    [array $each:^(id obj) {
      NSLog(@"%@", obj);
    }]

    [array $eachWithIndex:^(id obj, NSUInteger i) {
      NSLog(@"%d %@", i, obj);
    }]

    [array $eachWithStop:^(id obj, BOOL *stop) {
      NSLog(@"%@", obj);
      if($eql(obj, @"foo")) {
        *stop = YES;
      }
    }]

    [array $eachWithIndexAndStop:^(id obj, NSUInteger i, BOOL *stop) {
      NSLog(@"%d %@", i, obj);
      if(i == 1) {
        *stop = YES;
      }
    }]

    [array $map:^(id obj) {
      return $integer([obj integerValue] * 2);
    }]

    [array $mapWithIndex:^(id obj, NSUInteger i) {
      return $integer([obj integerValue] * 2 + i);
    }]

NSMutableArray

    [array $push:foo] => [array addObject:foo] (+ returns self)

NSDictionary

    [dict $for:@"foo"] => [dict objectForKey:@"foo"]

NSMutableDictionary

    [dict $obj:@"bar" for:@"foo"] => [dict setObject:@"bar" forKey:@"foo"] (+ returns self)

NSString

    [string $append:@"foo"]  => [string stringByAppendString:@"foo"]
    [string $prepend:@"foo"] => [NSString stringWithFormat:@"%@%@", @"foo", string]
    [string $split:@","]     => [string componentsSeparatedByString:@","]

NSMutableString

    [string $append_:@"foo"]     => [string appendString:@"foo"]           (+ returns self)
    [string $prepend_:@"foo"]    => [string insertString:@"foo" atIndex:0] (+ returns self)
    [string $insert:@"foo" at:1] => [string insertString:@"foo" atIndex:1] (+ returns self)
    [string $set:@"foo"]         => [string setString:@"foo"]              (+ returns self)

License

Copyright (c) 2010 Peter Jihoon Kim. This code is licensed under the MIT License.

concisekit's People

Contributors

petejkim avatar nolanw avatar

Stargazers

Kds@Devs avatar

Watchers

James Cloos avatar Kds@Devs avatar

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.