Search code examples
ctypespyobjcbridging-header

Loading NSString class on iOS fails, while NSSet and other works?


Using ctypes, I am getting a handle for objc_getClass method, and then I am trying to locate NSString but it FAILS!! At the same time, other classes such as NSSet/NSMutableArray and so on just work great!

import ctypes
from ctypes import cdll

c = cdll.LoadLibrary(None)
objc_getClass = c.objc_getClass

>>> c.objc_getClass(b'NSSet')
-110110920
>>> c.objc_getClass(b'NSString')
0

Solution

  • That's probably because the Foundation framework is not loaded.

    Try adding this before trying to resolve classes:

    cdll.LoadLibrary('/System/Library/Frameworks/Foundation.framework/Foundation')