supermarin / objectivesugar Goto Github PK
View Code? Open in Web Editor NEWObjectiveC additions for humans. Ruby style.
License: MIT License
ObjectiveC additions for humans. Ruby style.
License: MIT License
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; }
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).
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 );
should be fixed with
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id object = block(key, obj);
if (object)
[array addObject:object];
}];
Input: @"FullName"
Expected & Valid output: fullName
Actual Output: fullname
Fix: wrap the functionality of
-(NSString *)camelCase
to execute only if string contains underscore "_"
Would you consider adding an [NSString isEmpty] as described here: http://stackoverflow.com/a/899320 ?
It seems that when adding the project in a Podfile it does not get updated (getting 1.1.0 version).
Please check.
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); }
How open are you guys for a PR that prefixes all selectors?
should be fixed with
for (id object in self) {
id newObject = block(object);
if (newObject)
[array addObject:newObject];
}
Many a times, during map, I need the index of the object as well. Do you think it makes sense to add a mapWithIndex method?
Since ObjectiveSugar doesn't use method prefixes, what would we like to do here?
I suggest renaming ObjectiveSugar's method to containsCaseInsensitiveString:
.
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.
That seems incorrect and surprising to me. For example, what do you think this should do?
[@"part one - part two":split@" - "]
current behavior: "part","one","part","two"
expected behavior: "part one", "part two"
missed header in NSSet+ObjectiveSugar.m
=)
NSArray unique is useful 👍
When initial value is set to nil
and the array consists of single item, this item is returned and reduce block is never evaluated.
There seems to be an issue at https://github.com/supermarin/ObjectiveSugar/blob/master/Classes/NSArray%2BObjectiveSugar.m#L178
I would rather write it as
accumulator = block(accumulator, object);
Does this make sense?
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.
should be fixed with
for (id object in self) {
id newObject = block(object);
if (newObject)
[array addObject:newObject];
}
Compatibility with Carthage would be nice :)
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.
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.
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?
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?
NSArray+ObjectiveSugar.m
NSDictionary+ObjectiveSugar.m
NSSet+ObjectiveSugar.m
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];
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.