In Search of the Longest Method

Objective-C programmers are famed for their discursive nature. A good Objective-C developer will never use three or four words when a couple of thousand would easily do. We treat header files as carefully crafted essays, surprising and delighting the casual reader with innovative word play and thesaurus-stretching prose. After all, the argument goes, we have excellent code completion in Xcode, making even the longest method only a few keystrokes away - why not take advantage and let our imaginations take flight?

In light of this, the question arises every now and then: what is the longest method in all of Cocoa? Which method name stands head and shoulders above all the others, looking down upon its brothers with an air of smug superiority?

Despite several blog posts, StackOverflow threads and indeed Quora questions I’ve found on the topic, in a full 3 minutes of Googling I couldn’t find any answers which I found to be satisfying and backed by research. Which is surprising to me, as it’s not only an entertaining diversion, but also a great way to exercise the remarkable power and dynamicism of the Objective-C runtime.

For the purposes of this investigation, we’re looking at method names in frameworks in the iOS 7 SDK1. It’s possible that methods in Mac OS X may have longer names - there’s less need to worry about draining battery life and using up system resources with all those extra characters, of course, and Mac programmers are older and have worse eyesight so they do need things spelled out a bit more2.

To find the longest method, we need a list of all the available methods. In order to do that, we need a list of all the available objects. Usefully, Objective-C and the objc/runtime.h header provide a number of tools for inspecting the runtime, including objc_getClassList()3:

1
2
3
4
NSInteger numberOfClasses = objc_getClassList(NULL, 0); // Get the count of classes available
Class *classes = NULL; // Allocate an array in which to store the class list
classes = (__unsafe_unretained Class *)malloc(numberOfClasses * sizeof(Class));
numberOfClasses = objc_getClassList(classes, numberOfClasses); // Get the list of classes

Now we have a list of all the classes, getting a list of all the methods in a class is a similar process, using the class_copyMethodList() function:

1
2
3
4
5
6
7
8
NSUInteger numberOfMethods = 0;
Method *instanceMethods = class_copyMethodList(class, &numberOfMethods);

for (NSInteger i=0; i < numberOfMethods; i++) {
  Method method = instanceMethods[i];
    SEL selector = method_getName(method);
  NSString *methodName = NSStringFromSelector(selector);
}

So with just this, we have enough to find the longest instance method in the iOS 7 SDK. Loop through all the classes and their methods, keep track of which one is the longest and voila, a method whose signature is an impressive 258 characters long:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-[PLBBBulletin _initWithLikesCount:
                       commentDate:
                  firstCommentGUID:
                   toAssetWithUUID:
                     photosBatchID:
                   mainAssetIsMine:
                  mainAssetIsVideo:
                  inAlbumWithTitle:
                         albumUUID:
                        assetUUIDs:
             placeholderAssetUUIDs:
             lowResThumbAssetUUIDs:
                       senderNames:
                  forMultipleAsset:
            allMultipleAssetIsMine:
                       isMixedType:]

PLBBBulletin is the responsible class, buried inside the PhotoLibraryServices.framework and not at all public. Considering the references to comments, assets, likes, sender names etc, and adding the fact that this method first appeared in iOS 6.0, I’d hazard a guess that this method is buried deep in the bowels of the PhotoStream feature, which may have been codenamed ‘Bulletin Board’ or similar at some point during its development.

But wait, the search isn’t over yet. Notice how I mentioned above that this is the longest instance method. Fear not, I haven’t forgotten about class methods. Classes are themselves objects, but the class meta-objects aren’t returned by objc_getClassList(), and class methods aren’t returned by class_copyMethodList(). This means, to include class methods in our list, we also need to get the meta-class and add its list of methods to the pile. This is done using the objc_getMetaClass() function:

1
2
Class metaClass = objc_getMetaClass([NSStringFromClass(class) cStringUsingEncoding:NSASCIIStringEncoding]);
Method *classMethods = class_copyMethodList(metaClass, &numberOfMethods);

After adding the class methods in, this corker of a method appears:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+[CUIShapeEffectStack shapeEffectSingleBlurFrom:
                               withInteriorFill:
                                         offset:
                                       blurSize:
                                   innerGlowRed:
                                 innerGlowGreen:
                                  innerGlowBlue:
                               innerGlowOpacity:
                                 innerShadowRed:
                               innerShadowGreen:
                                innerShadowBlue:
                             innerShadowOpacity:
                                   outerGlowRed:
                                 outerGlowGreen:
                                  outerGlowBlue:
                               outerGlowOpacity:
                                 outerShadowRed:
                               outerShadowGreen:
                                outerShadowBlue:
                             outerShadowOpacity:
                            hasInsideShadowBlur:
                           hasOutsideShadowBlur:]

Weighing in at an eye-watering 352 characters and taking an astonishing 22 arguments, it’s clear we have a winner. This method, introduced in iOS 7 as part of the new, private, CoreUI.framework, is undocumented in the public sphere, and it’s outside the scope of this post to figure out exactly what it is achieving - but going by the method name, the framework, and the names of the arguments, it doesn’t seem to much of a stretch to suggest that this method right here could be the very one responsible for iOS 7’s impressive ‘frosted glass’ effect, the gaussian blur seen in toolbars, navigation bars, Siri, and in the Notification and Control Centers.

You never know, perhaps the real reason that Apple’s implementation of a ‘live blur’ effect hasn’t been exposed to third party developers yet is that it’s going to take an infinite number of monkeys an infinite amount of time to write the documentation just for this one method.


  1. iOS 7 has itself introduced a number of remarkably long method names, and some of the beta APIs even included some quite astonishing references to my mother.

  2. Just kidding, of course.

  3. You do have to make sure to link every framework in the app running this code, or else you may miss objects and methods which only appear in obscure corners of the iOS SDK frameworks.