supermarin / ObjectiveSugar

ObjectiveC additions for humans. Ruby style.
MIT License
2.17k stars 190 forks source link

Shorter NSStringWithFormat #73

Closed iluuu1994 closed 10 years ago

iluuu1994 commented 10 years ago

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.

zenangst commented 10 years ago

+1, love it.

3lvis commented 10 years ago

:+1:

supermarin commented 10 years ago

How about -withFormat?

That'd give it a perfect hint, altho the choice is on you. Love the idea.

iluuu1994 commented 10 years ago

@supermarin Agreed.

Great! I'll create a pull-request soon.

neilco commented 10 years ago

I think it should be formatWithArgs since it would work in this way [formatString formatWithArgs:arg1, arg2, ...]

If you wanted to shorten it, I'd be OK with withArgs although it loses some clarity.

supermarin commented 10 years ago

Hm, that makes sense; altho it could get verbose-y again. Here're the examples written down

[@"Hello %@" format:@"world"];
[@"Hello %@" withFormat:@"world"];
[@"Hello %@" withArgs:@"world"];
[@"Hello %@" formatWithArgs:@"world"];

[NSString stringWithFormat:@"Hello %@", @"world"];
NSStringWithFormat(@"Hello", @"world");

@iluuu1994 it's also worth pointing out that the main goal of NSStringWithFormat wasn't shortening, it was more avoiding bracketception and improving readability.

neilco commented 10 years ago

Agreed. See this comparison:

[@"Hello %@" formatWithArgs:@"world"];
NSStringWithFormat(@"Hello %@", @"world");

I think the latter is much clearer to read.

iluuu1994 commented 10 years ago

@neilco Good point.

I like withArgs:, although formatWithArgs: would be clearer. It's your repo, so I guess you guys decide.

iluuu1994 commented 10 years ago

@supermarin

that the main goal of NSStringWithFormat wasn't shortening, it was more avoiding bracketception

Well if that's the case my idea isn't worth a lot. Let me see if I find a more elegant solution. Until then, I'll close the issue.

supermarin commented 10 years ago

fair enough, thanks @iluuu1994!