Forum Archive

Listing all methods in an ObjCClass

polymerchm

Somewhere, one of the Objc_util gurus posted a script to display all methods of a ObjectiveC class in a TextView. I have, of course, lost it.

JonB

objc_browser in my objc_hacks repo shows a tableview of all known classes. tapping a class shows all instance methods of that class(and copys the name to the clipboard). tapping a method copies the method name to the clipboard, and also installs a logger to all calls to that method in order to get into on arguments.... I don't recommend doing that.

also, i think i have a print_objc .py file which lets you print all methods of a class, showing which are inherited vs specific to the class.

polymerchm

They're not real happy with Pythonista 3, or the beta. I think the underlying implementation of .startswith() is re based and not real happy with bytestrings.

JonB

objc_browser should work in both interpreters. i usually run it in 3.x. it requires swizzlelog, and swizzle, though those could be removed if you just want to browse.

print_objc.print_methods works with the 2.7 interpreter.... though the latest beta seems to have broken the shebang controlling interpreter... long press to run in 2.7, and of course switch console interpreter in the corner. probably i should just make the browser highlight cells based on whether it is a class method, inherited instance method, or specialized instance method.

wolf71

@JonB
1. Can add function list. list all objc c function?
2. Can browser objc const's value ?

JonB

Not sure what you mean by all objc functions... if you mean the c dll symbols, i am not sure if that is possible, plus there are so damn many of them i am not aure how useful it would be.

Likewise with consts...

dgelessus

What JonB said. The Objective-C runtime only knows about Objective-C classes and objects. Functions that don't belong to a class are normal C functions, and there is no good way to list all C functions declared in a library. You can only use a function if you know its name and set its restype and argtypes.

The NSString constants used in various frameworks are similar, there is no way to list all of them, but you can get them by name using ctypes.c_void_p.in_dll.

Integer constants (defined with enum or #define in C) are replaced with normal integers by the compiler. There is no way to get an enum or #define constant by its name using ctypes.

wolf71

Thanks @dgelessus @JonB

JonB

@dgelessus In retrosepct, while it is not usually possible to list symbols in a dll, here we actually have a static library. So the symbol table must be present....

sure enough,

import sys
f=open('sys.executable','rb')
while True:
   s=f.read(10000).decode('latin1').replace('\x00','')
   if s.find('objc_msgSend') >0:
      print(f.tell())
      break

stops an a chunk of symbol table.
Looks like a regex looking for @_ and ending with \x90 should capture all symbols... looks like tonights project...

JonB
import sys,re, mmap

with open(sys.executable,'rb') as f:
    m=mmap.mmap(f.fileno(),0, access=mmap.ACCESS_READ)
    symbols=re.findall(b'@_(\w*)',m)

obviously not a complete list, but this gives me about 1500 symbols.