Coder Social home page Coder Social logo

objectivesugar's Issues

NSArray, NSSet first

methods aren't safe

NSArray

- (id)first {
    return [self objectAtIndex:0];
}

should be

- (id)first {
    if (self.count > 0)
        return [self objectAtIndex:0];
    return nil;
}

NSSet

- (id)first {
    return self.allObjects[0];
}

should be

- (id)first {
    NSArray *allObjects = self.allObjects;
    if (allObjects.count > 0)
        return [allObjects objectAtIndex:0];
    return nil;
}

Returning NSMutableArray as NSArray

Hey guys, I see multiple places where the method signature says the return type is NSArray but the actual object being returned is NSMutableArray. This is a potential problem because the returned object will still respond to NSMutableArray messages. It is Apple's recommendation and standard practice to make a non-mutable copy of the array before returning. So:

NSMutableArray *myArray = ...
// fill the array here
return myArray;

should become

NSMutableArray *myArray = ...
// fill the array here
return [myArray copy];

You can also use [NSArray arrayWithArray:myArray] instead of [myArray copy](I think "copy" in this case just makes a call to arrayWithArray anyway, so using arrayWithArray cuts out a directive or two).

RFE: add "all" / "every" method to collections

I miss this simple addition:

 - (BOOL)all:(BOOL (^)(id object))condition;

i.e:

NSArray *list = @['a', 'b', 'c'];
BOOL allMatch = [list all:^(id object) { return id != nil }];
assert( allMatch );

Problem with NSDictionary map

should be fixed with

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    id object = block(key, obj);
    if (object)
        [array addObject:object];
}];

Pod not updated

It seems that when adding the project in a Podfile it does not get updated (getting 1.1.0 version).
Please check.

Macros proposal

Hi,

thanks for starting this library! I was planning to do it myself, but never found the time to. I have experimented with some macros to bring the part of ruby that I love the most to Objective-C. Following you can find the unfinished result. What do you think? Is this something that you would be interested in adding to your ObjectiveSugar?

//#define RACAble(...) metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__))(_RACAbleObject(self, __VA_ARGS__))(_RACAbleObject(__VA_ARGS__))

///-----------------------------------------------------------------------------
// @name map
///-----------------------------------------------------------------------------

#define $to(selector) ^id (id x) { return [x selector]; }
[authors map:$to(name)] 
[authors map:$to(nameWithAge:date)]

///-----------------------------------------------------------------------------
// @name filter & select
///-----------------------------------------------------------------------------

#define $by(selector) ^BOOL (id x) { return [x selector]; }
[views filter:$by(hidden)]

#define $if(expression) ^BOOL (id x) { return (expression); }
[views filter:$if([x frame].origin.x > 10)]


///-----------------------------------------------------------------------------
// @name each
///-----------------------------------------------------------------------------

#define $do(expression) ^(id x) { {expression;}; }
[lions each:$do(attack)]

#define $doWithKey(expression) ^(id* key, id x) { (expression); }

Problem with NSSet map

should be fixed with

for (id object in self) {
    id newObject = block(object);
    if (newObject)
        [array addObject:newObject];
}

ld: library not found for -llibPods-test

Every time I pull new updates from this project, I have to modify the test project to get it to build with the error shown in the title of this issue. I'd like to suggest you add the Pods project to the workspace and make it a dependency to the SampleProjectTests target.

Shorter NSStringWithFormat

As you all know, [NSString stringWithFormat:@"...", ...] is a lot of syntax for something so common. This is why ObjectiveSugar implemented the NSStringWithFormat function.
It is indeed shorter, but well... not a lot.

How about a format: method on NSString?
[@"..." format:...] would be much more appealing in my opinion.

I will implement this method and update the tests and doc if anyone finds this useful.

problem with NSArray map

should be fixed with

for (id object in self) {
    id newObject = block(object);
    if (newObject)
        [array addObject:newObject];
}

NSNumber date methods not accounting for DST, leap year and other date/calendar oddities

The current methods can give a fast approximation, but they can cause some inaccurate/misleading results when you factor in things like daylight savings time and leap year. On a day where the clocks were set back for daylight savings time, one would probably expect @(1).day.ago executed at 2pm to return 2pm the previous day, but since 24 hours ago was 3pm the previous day, that's the time you'll get back with the current implementation.

It looks like the SexyDates project handles that correctly. [NSCalendar currentCalendar] is too slow to call repeatedly without cacheing on iOS 6 and lower, but evidently is now cached in iOS 7. If you're concerned about performance on iOS 6, you can cache the NSCalendar in a thread dictionary.

I'd be happy to work on this if you let me know your thoughts on it before I get started.

Naming consistency

Why are there +Accessors, +Rubyfy and now +Sweetner categories? Why isn't it NSString+Rubyfy.m? Or even better, why aren't all of them +ObjectiveSugar?

Wonderful project, but it doesn't seem very consistent now.

NSSet should return NSSet

I've been looking for a replacement for BlocksKit, and I was wondering why NSSet selectors return an NSArray?
And would you be open for a PR to change it?

documentation up to date?

So, how up-to-date is the documentation in the README? Some of the examples don't seem to work (though I may just be an idiot):

//this gives the error "no visibile @interface for 'nsarray' declares the selector 'detect'"

NSArray *mixedData = @[ @1, @"Objective Sugar!", @"Github", @4, @"5"];
[mixedData detect:^BOOL(id object) {
    return ([object class] == [NSString class]);
}];

//this gives the error 'Expected method to read dictionary element not found on object of type 'NSArray *'

NSArray *indices = @[@1, @2, @3, @4, @5];
indices[@"1..3"];

Other examples seem to work, however. Am I missing something?

Use fast enumeration

NSArray+ObjectiveSugar.m

  • (void)each:(void (^)(id object))block {
    for (id object in self)
    block(object);
    }

NSDictionary+ObjectiveSugar.m

  • (void)eachKey:(void (^)(id k))block {
    for (id key in self)
    block(key);
    }

NSSet+ObjectiveSugar.m

  • (void)each:(void (^)(id object))block {
    for (id object in self)
    block(object);
    }

Add one function : map NSArray and skip someone

Sometimes not only need to map out an array, but also need to conditional mapping. Reference to NSArray's block traversal method - (void) enumerateObjectsUsingBlock: (void (NS_NOESCAPE ^) (ObjectType obj, NSUInteger idx, BOOL * stop)) block.

Add a skip option for the mapping method:

- (NSArray *) mm_mapWithskip:(id (^)(id obj, BOOL *skip))callback{
    
    NSMutableArray * _self = [NSMutableArray arrayWithCapacity:self.count];
    
    for( id obj in self ){
        
        BOOL skip = NO;
        
        id mapObj = callback(obj, &skip);
        
        if( !skip ){
            [_self addObject:mapObj];
        }
    }
    return [_self copy];
}

Refer to this answer

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.