OpinionatedC 0.3.0

OpinionatedC 0.3.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2016年1月

Leo Schweizer 维护。



有时候,Objective-C 的语法确实很冗长。生命太短,没有必要频繁使用 enumerateObjectsUsingBlock,也没有时间去创建子数组使用 filteredArrayUsingPredicate 吧?

OpinionatedC 正是为了解决这个问题而诞生的。它提供了一系列像 Smalltalk 风格的便捷扩展方法,使得编写简洁、易读的 Objective-C 代码成为一种乐趣。

使用方法

将 OpinionatedC 集成到项目中 最简单的方法是通过 CocoaPods

pod 'OpinionatedC'

将伞头文件导入到您想要体验 OpinionatedC 美味的任何地方

#import <OpinionatedC/OpinionatedC.h>

关于方法前缀的说明

毫无疑问,对于分类中的扩展方法,最佳实践是在选择器中包含供应商前缀。目前,OpinionatedC 并未遵循这一最佳实践,因为写(和读)诸如 oc_each: 这样的代码而不是简单的 each: 并不能真正有助于提高可读性。

然而,我现在正在寻找一种允许用户可选、可配置的方法前缀的方法。如果您对这个话题有任何想法,请随时在 #1 话题中添加讨论。

特性

完整的特性列表可在官方 OpinionatedC 网站找到。

以下是 OpinionatedC 可以实现的一些功能的小样

[@[@"hello", @"world!"] average:^NSNumber*(id each) { 
    return @([each length]);
}]
// => @5.5

NSSet *set = [NSSet setWithObjects:@"foo", @"bar", @"hello", @"world!", nil];
[set groupedBy:^id(id each) {
    return @([each length]);
}]
// => @{
//        @3 : a NSSet(@"foo", @"bar"),
//        @5 : a NSSet(@"hello"),
//        @6 : a NSSet(@"world!")
//    }

[@[@"foo", @"bar"] each:^(NSString *each) {
    NSLog(@"%@", each);
}]
// => foo
// => bar

[@"abc" eachWithIndex:^(NSString *each, NSUInteger idx) {
    NSLog(@"%@ - %@", each, @(idx));
}]
// => a - 0
// => b - 1
// => c - 2

[@[@"a", @"b", @"c"] isEmpty]
// => NO

[@[@1, @2, @3] map:^id(NSNumber *each) {
    return @([each integerValue] * 2);
}]
// => @[@2, @4, @6]

[@[@1, @2, @3] inject:@0 into:^id(NSNumber *running, NSNumber *each) {
    return @([running integerValue] + [each integerValue]);
}]
// => @6

[@[@1, @2, @3, @4] allSatisfy:^BOOL(NSNumber *each) {
    return [each integerValue] % 2 == 0;
}]
// => NO

[@"abcdef" first:3]
// => @"abc"

[@{ @1 : @"foo", @2 : @"bar"} detect:^BOOL(OCAssociation *each) {
    return [each.key isEqualToNumber:@2];
}]
// => an OCAssociation(@2, @"bar")

[@{ @1 : @"foo", @2 : @"bar"} select:^BOOL(OCAssociation *each) {
    return [each.key isEqualToNumber:@2];
}]
// => @{ @2 : @"bar" }

[@{ @1 : @"foo", @2 : @"bar"} reject:^BOOL(OCAssociation *each) {
    return [each.key isEqualToNumber:@2];
}]
// => @{ @1 : @"foo" }

[@100 atRandom]
// => 77

[@3 timesRepeat:^{ 
   NSLog(@"hooray!"); 
}]
// => hooray
// => hooray
// => hooray

[[@1 to:@10] select:^id(NSNumber *each) {
    return [each integerValue] % 2 == 0;
}]
// => @[@2, @4, @6, @8, @10]

贡献

如果您有任何问题、评论、想法或改进建议,请不要犹豫,打开一个问题。如果您正在创建一个 pull request,这里有一些注意事项

  • 如果您不确定您的想法是否受欢迎,首先打开一个问题
  • 使用制表符缩进,而不是空格
  • 编写单元测试,追求 100% 的覆盖率

贡献者

许可证

The MIT License (MIT)

Copyright (c) 2015 Leo Schweizer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.