Hacker News new | ask | show | jobs
by mikeash 3697 days ago
Objective-C has no safe collections for non-object values. If you want an array of ints, for example, you either get to use a C array with lots of manual management and potential for error, or you use an NSArray of NSNumbers and pay for a bunch of overhead.

It has very poor support for custom value types. Objective-C structs basically can't contain object pointers, so they're limited to simple things like CGRect. They also can't contain methods, so you end up writing associated code in global functions.

Protocols are extremely limited. They're basically just collections of method declarations. There's no way to add functionality to every class which conforms to a protocol.

Objective-C is often verbose to the point of painful redundancy. Consider:

    NSString *x = [NSString stringWithFormat: @"%d", number];
Versus:

    let x = String(format: "%d", number)
Other examples include having to write the signature of every public method twice (once in the header and once in the implementation) and the need to do an annoying `self = [super init]` dance in every initializer.

There's almost no functional programming stuff available, like map and reduce. This is not strictly a language complaint, but since the standard libraries are pretty tightly woven in, I think it still counts.

Generics support is really limited. What's there was only added to interoperate better with Swift, anyway!

That's a quick overview of some of the ways Swift improves on ObjC. I'm sure there's more.

2 comments

I'd add typed enums to the list of notable bug-avoiding improvements in Swift. I sometimes describe Objective-C as "all of the memory safety of C with the type safety of SmallTalk".
I may steal that quote; I think I like it better than calling Objective-C the "wild west of OOP".
We can even use string interpolation

    let x = "\(number)"