I’ve been writing code in Objective-C for more than 12 years now, yet it still surprises me. One example of something that caught me off guard recen

Preventing Surprisingly Large Objective-C Type Encodings

submited by
Style Pass
2021-05-21 10:30:12

I’ve been writing code in Objective-C for more than 12 years now, yet it still surprises me. One example of something that caught me off guard recently is the combination of ObjC, C++ objects, and type encodings. Did you know these encodings can become thousands of characters long? This is obviously wasteful and bad for binary size and performance.

If you’re familiar with Objective-C, you likely know about selectors, but they only contain the name and number of parameters. The type each parameter has is stored in the method type encoding, which is encoded as a simple string. With C++ types, those strings can easily become lengthy and add to your binary size. The ObjC runtime includes NSMethodSignature, which helps to parse this string, but you don’t have to understand how this encoding works in detail to follow along.

Here’s an example from our codebase of the encoding for a simple map with an enum and a string, std::unordered_map<PSPDFAnnotationPlaceholderState, NSString *>:

Leave a Comment