NSObject-Tap 1.0.1

NSObject-Tap 1.0.1

测试已测试
语言语言 Obj-CObjective-C
许可证 MIT
发布上次发布2014年12月

Syo Ikeda维护。



  • Syo Ikeda

tap:方法是从Ruby借用的。它还支持来自RubyGemtapp,用于打印调试用例。

设置

让我们使用CocoaPods

# Podfile
pod 'NSObject-Tap'
$ pod install

或者复制两个文件NSObject+Tap.{h,m}到您的项目中。

然后,导入头文件。

#import "NSObject+Tap.h"

示例

初始化

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";
}];

更简洁的代码,减少局部变量

// 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];
}]

使用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];