runtime.h 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /*
  2. * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _OBJC_RUNTIME_H
  24. #define _OBJC_RUNTIME_H
  25. #include <objc/objc.h>
  26. #include <stdarg.h>
  27. #include <stdint.h>
  28. #include <stddef.h>
  29. #include <Availability.h>
  30. #include <TargetConditionals.h>
  31. #if TARGET_OS_MAC
  32. #include <sys/types.h>
  33. #endif
  34. /* Types */
  35. #if !OBJC_TYPES_DEFINED
  36. /// An opaque type that represents a method in a class definition.
  37. typedef struct objc_method *Method;
  38. /// An opaque type that represents an instance variable.
  39. typedef struct objc_ivar *Ivar;
  40. /// An opaque type that represents a category.
  41. typedef struct objc_category *Category;
  42. /// An opaque type that represents an Objective-C declared property.
  43. typedef struct objc_property *objc_property_t;
  44. struct objc_class {
  45. Class _Nonnull isa OBJC_ISA_AVAILABILITY;
  46. #if !__OBJC2__
  47. Class _Nullable super_class OBJC2_UNAVAILABLE;
  48. const char * _Nonnull name OBJC2_UNAVAILABLE;
  49. long version OBJC2_UNAVAILABLE;
  50. long info OBJC2_UNAVAILABLE;
  51. long instance_size OBJC2_UNAVAILABLE;
  52. struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;
  53. struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;
  54. struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;
  55. struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
  56. #endif
  57. } OBJC2_UNAVAILABLE;
  58. /* Use `Class` instead of `struct objc_class *` */
  59. #endif
  60. #ifdef __OBJC__
  61. @class Protocol;
  62. #else
  63. typedef struct objc_object Protocol;
  64. #endif
  65. /// Defines a method
  66. struct objc_method_description {
  67. SEL _Nullable name; /**< The name of the method */
  68. char * _Nullable types; /**< The types of the method arguments */
  69. };
  70. /// Defines a property attribute
  71. typedef struct {
  72. const char * _Nonnull name; /**< The name of the attribute */
  73. const char * _Nonnull value; /**< The value of the attribute (usually empty) */
  74. } objc_property_attribute_t;
  75. /* Functions */
  76. /* Working with Instances */
  77. /**
  78. * Returns a copy of a given object.
  79. *
  80. * @param obj An Objective-C object.
  81. * @param size The size of the object \e obj.
  82. *
  83. * @return A copy of \e obj.
  84. */
  85. OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size)
  86. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  87. OBJC_ARC_UNAVAILABLE;
  88. /**
  89. * Frees the memory occupied by a given object.
  90. *
  91. * @param obj An Objective-C object.
  92. *
  93. * @return nil
  94. */
  95. OBJC_EXPORT id _Nullable
  96. object_dispose(id _Nullable obj)
  97. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  98. OBJC_ARC_UNAVAILABLE;
  99. /**
  100. * Returns the class of an object.
  101. *
  102. * @param obj The object you want to inspect.
  103. *
  104. * @return The class object of which \e object is an instance,
  105. * or \c Nil if \e object is \c nil.
  106. */
  107. OBJC_EXPORT Class _Nullable
  108. object_getClass(id _Nullable obj)
  109. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  110. /**
  111. * Sets the class of an object.
  112. *
  113. * @param obj The object to modify.
  114. * @param cls A class object.
  115. *
  116. * @return The previous value of \e object's class, or \c Nil if \e object is \c nil.
  117. */
  118. OBJC_EXPORT Class _Nullable
  119. object_setClass(id _Nullable obj, Class _Nonnull cls)
  120. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  121. /**
  122. * Returns whether an object is a class object.
  123. *
  124. * @param obj An Objective-C object.
  125. *
  126. * @return true if the object is a class or metaclass, false otherwise.
  127. */
  128. OBJC_EXPORT BOOL
  129. object_isClass(id _Nullable obj)
  130. OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0);
  131. /**
  132. * Reads the value of an instance variable in an object.
  133. *
  134. * @param obj The object containing the instance variable whose value you want to read.
  135. * @param ivar The Ivar describing the instance variable whose value you want to read.
  136. *
  137. * @return The value of the instance variable specified by \e ivar, or \c nil if \e object is \c nil.
  138. *
  139. * @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar
  140. * for the instance variable is already known.
  141. */
  142. OBJC_EXPORT id _Nullable
  143. object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
  144. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  145. /**
  146. * Sets the value of an instance variable in an object.
  147. *
  148. * @param obj The object containing the instance variable whose value you want to set.
  149. * @param ivar The Ivar describing the instance variable whose value you want to set.
  150. * @param value The new value for the instance variable.
  151. *
  152. * @note Instance variables with known memory management (such as ARC strong and weak)
  153. * use that memory management. Instance variables with unknown memory management
  154. * are assigned as if they were unsafe_unretained.
  155. * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
  156. * for the instance variable is already known.
  157. */
  158. OBJC_EXPORT void
  159. object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
  160. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  161. /**
  162. * Sets the value of an instance variable in an object.
  163. *
  164. * @param obj The object containing the instance variable whose value you want to set.
  165. * @param ivar The Ivar describing the instance variable whose value you want to set.
  166. * @param value The new value for the instance variable.
  167. *
  168. * @note Instance variables with known memory management (such as ARC strong and weak)
  169. * use that memory management. Instance variables with unknown memory management
  170. * are assigned as if they were strong.
  171. * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
  172. * for the instance variable is already known.
  173. */
  174. OBJC_EXPORT void
  175. object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
  176. id _Nullable value)
  177. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  178. /**
  179. * Changes the value of an instance variable of a class instance.
  180. *
  181. * @param obj A pointer to an instance of a class. Pass the object containing
  182. * the instance variable whose value you wish to modify.
  183. * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
  184. * @param value The new value for the instance variable.
  185. *
  186. * @return A pointer to the \c Ivar data structure that defines the type and
  187. * name of the instance variable specified by \e name.
  188. *
  189. * @note Instance variables with known memory management (such as ARC strong and weak)
  190. * use that memory management. Instance variables with unknown memory management
  191. * are assigned as if they were unsafe_unretained.
  192. */
  193. OBJC_EXPORT Ivar _Nullable
  194. object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,
  195. void * _Nullable value)
  196. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  197. OBJC_ARC_UNAVAILABLE;
  198. /**
  199. * Changes the value of an instance variable of a class instance.
  200. *
  201. * @param obj A pointer to an instance of a class. Pass the object containing
  202. * the instance variable whose value you wish to modify.
  203. * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
  204. * @param value The new value for the instance variable.
  205. *
  206. * @return A pointer to the \c Ivar data structure that defines the type and
  207. * name of the instance variable specified by \e name.
  208. *
  209. * @note Instance variables with known memory management (such as ARC strong and weak)
  210. * use that memory management. Instance variables with unknown memory management
  211. * are assigned as if they were strong.
  212. */
  213. OBJC_EXPORT Ivar _Nullable
  214. object_setInstanceVariableWithStrongDefault(id _Nullable obj,
  215. const char * _Nonnull name,
  216. void * _Nullable value)
  217. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
  218. OBJC_ARC_UNAVAILABLE;
  219. /**
  220. * Obtains the value of an instance variable of a class instance.
  221. *
  222. * @param obj A pointer to an instance of a class. Pass the object containing
  223. * the instance variable whose value you wish to obtain.
  224. * @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
  225. * @param outValue On return, contains a pointer to the value of the instance variable.
  226. *
  227. * @return A pointer to the \c Ivar data structure that defines the type and name of
  228. * the instance variable specified by \e name.
  229. */
  230. OBJC_EXPORT Ivar _Nullable
  231. object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
  232. void * _Nullable * _Nullable outValue)
  233. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  234. OBJC_ARC_UNAVAILABLE;
  235. /* Obtaining Class Definitions */
  236. /**
  237. * Returns the class definition of a specified class.
  238. *
  239. * @param name The name of the class to look up.
  240. *
  241. * @return The Class object for the named class, or \c nil
  242. * if the class is not registered with the Objective-C runtime.
  243. *
  244. * @note \c objc_getClass is different from \c objc_lookUpClass in that if the class
  245. * is not registered, \c objc_getClass calls the class handler callback and then checks
  246. * a second time to see whether the class is registered. \c objc_lookUpClass does
  247. * not call the class handler callback.
  248. *
  249. * @warning Earlier implementations of this function (prior to OS X v10.0)
  250. * terminate the program if the class does not exist.
  251. */
  252. OBJC_EXPORT Class _Nullable
  253. objc_getClass(const char * _Nonnull name)
  254. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  255. /**
  256. * Returns the metaclass definition of a specified class.
  257. *
  258. * @param name The name of the class to look up.
  259. *
  260. * @return The \c Class object for the metaclass of the named class, or \c nil if the class
  261. * is not registered with the Objective-C runtime.
  262. *
  263. * @note If the definition for the named class is not registered, this function calls the class handler
  264. * callback and then checks a second time to see if the class is registered. However, every class
  265. * definition must have a valid metaclass definition, and so the metaclass definition is always returned,
  266. * whether it’s valid or not.
  267. */
  268. OBJC_EXPORT Class _Nullable
  269. objc_getMetaClass(const char * _Nonnull name)
  270. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  271. /**
  272. * Returns the class definition of a specified class.
  273. *
  274. * @param name The name of the class to look up.
  275. *
  276. * @return The Class object for the named class, or \c nil if the class
  277. * is not registered with the Objective-C runtime.
  278. *
  279. * @note \c objc_getClass is different from this function in that if the class is not
  280. * registered, \c objc_getClass calls the class handler callback and then checks a second
  281. * time to see whether the class is registered. This function does not call the class handler callback.
  282. */
  283. OBJC_EXPORT Class _Nullable
  284. objc_lookUpClass(const char * _Nonnull name)
  285. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  286. /**
  287. * Returns the class definition of a specified class.
  288. *
  289. * @param name The name of the class to look up.
  290. *
  291. * @return The Class object for the named class.
  292. *
  293. * @note This function is the same as \c objc_getClass, but kills the process if the class is not found.
  294. * @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
  295. */
  296. OBJC_EXPORT Class _Nonnull
  297. objc_getRequiredClass(const char * _Nonnull name)
  298. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  299. /**
  300. * Obtains the list of registered class definitions.
  301. *
  302. * @param buffer An array of \c Class values. On output, each \c Class value points to
  303. * one class definition, up to either \e bufferCount or the total number of registered classes,
  304. * whichever is less. You can pass \c NULL to obtain the total number of registered class
  305. * definitions without actually retrieving any class definitions.
  306. * @param bufferCount An integer value. Pass the number of pointers for which you have allocated space
  307. * in \e buffer. On return, this function fills in only this number of elements. If this number is less
  308. * than the number of registered classes, this function returns an arbitrary subset of the registered classes.
  309. *
  310. * @return An integer value indicating the total number of registered classes.
  311. *
  312. * @note The Objective-C runtime library automatically registers all the classes defined in your source code.
  313. * You can create class definitions at runtime and register them with the \c objc_addClass function.
  314. *
  315. * @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject,
  316. * so you cannot safely call any methods on such classes without detecting that the method is implemented first.
  317. */
  318. OBJC_EXPORT int
  319. objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount)
  320. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  321. /**
  322. * Creates and returns a list of pointers to all registered class definitions.
  323. *
  324. * @param outCount An integer pointer used to store the number of classes returned by
  325. * this function in the list. It can be \c nil.
  326. *
  327. * @return A nil terminated array of classes. It must be freed with \c free().
  328. *
  329. * @see objc_getClassList
  330. */
  331. OBJC_EXPORT Class _Nonnull * _Nullable
  332. objc_copyClassList(unsigned int * _Nullable outCount)
  333. OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0);
  334. /* Working with Classes */
  335. /**
  336. * Returns the name of a class.
  337. *
  338. * @param cls A class object.
  339. *
  340. * @return The name of the class, or the empty string if \e cls is \c Nil.
  341. */
  342. OBJC_EXPORT const char * _Nonnull
  343. class_getName(Class _Nullable cls)
  344. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  345. /**
  346. * Returns a Boolean value that indicates whether a class object is a metaclass.
  347. *
  348. * @param cls A class object.
  349. *
  350. * @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class,
  351. * \c NO if \e cls is \c Nil.
  352. */
  353. OBJC_EXPORT BOOL
  354. class_isMetaClass(Class _Nullable cls)
  355. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  356. /**
  357. * Returns the superclass of a class.
  358. *
  359. * @param cls A class object.
  360. *
  361. * @return The superclass of the class, or \c Nil if
  362. * \e cls is a root class, or \c Nil if \e cls is \c Nil.
  363. *
  364. * @note You should usually use \c NSObject's \c superclass method instead of this function.
  365. */
  366. OBJC_EXPORT Class _Nullable
  367. class_getSuperclass(Class _Nullable cls)
  368. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  369. /**
  370. * Sets the superclass of a given class.
  371. *
  372. * @param cls The class whose superclass you want to set.
  373. * @param newSuper The new superclass for cls.
  374. *
  375. * @return The old superclass for cls.
  376. *
  377. * @warning You should not use this function.
  378. */
  379. OBJC_EXPORT Class _Nonnull
  380. class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper)
  381. __OSX_DEPRECATED(10.5, 10.5, "not recommended")
  382. __IOS_DEPRECATED(2.0, 2.0, "not recommended")
  383. __TVOS_DEPRECATED(9.0, 9.0, "not recommended")
  384. __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
  385. __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended");
  386. /**
  387. * Returns the version number of a class definition.
  388. *
  389. * @param cls A pointer to a \c Class data structure. Pass
  390. * the class definition for which you wish to obtain the version.
  391. *
  392. * @return An integer indicating the version number of the class definition.
  393. *
  394. * @see class_setVersion
  395. */
  396. OBJC_EXPORT int
  397. class_getVersion(Class _Nullable cls)
  398. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  399. /**
  400. * Sets the version number of a class definition.
  401. *
  402. * @param cls A pointer to an Class data structure.
  403. * Pass the class definition for which you wish to set the version.
  404. * @param version An integer. Pass the new version number of the class definition.
  405. *
  406. * @note You can use the version number of the class definition to provide versioning of the
  407. * interface that your class represents to other classes. This is especially useful for object
  408. * serialization (that is, archiving of the object in a flattened form), where it is important to
  409. * recognize changes to the layout of the instance variables in different class-definition versions.
  410. * @note Classes derived from the Foundation framework \c NSObject class can set the class-definition
  411. * version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function.
  412. */
  413. OBJC_EXPORT void
  414. class_setVersion(Class _Nullable cls, int version)
  415. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  416. /**
  417. * Returns the size of instances of a class.
  418. *
  419. * @param cls A class object.
  420. *
  421. * @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil.
  422. */
  423. OBJC_EXPORT size_t
  424. class_getInstanceSize(Class _Nullable cls)
  425. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  426. /**
  427. * Returns the \c Ivar for a specified instance variable of a given class.
  428. *
  429. * @param cls The class whose instance variable you wish to obtain.
  430. * @param name The name of the instance variable definition to obtain.
  431. *
  432. * @return A pointer to an \c Ivar data structure containing information about
  433. * the instance variable specified by \e name.
  434. */
  435. OBJC_EXPORT Ivar _Nullable
  436. class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
  437. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  438. /**
  439. * Returns the Ivar for a specified class variable of a given class.
  440. *
  441. * @param cls The class definition whose class variable you wish to obtain.
  442. * @param name The name of the class variable definition to obtain.
  443. *
  444. * @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name.
  445. */
  446. OBJC_EXPORT Ivar _Nullable
  447. class_getClassVariable(Class _Nullable cls, const char * _Nonnull name)
  448. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  449. /**
  450. * Describes the instance variables declared by a class.
  451. *
  452. * @param cls The class to inspect.
  453. * @param outCount On return, contains the length of the returned array.
  454. * If outCount is NULL, the length is not returned.
  455. *
  456. * @return An array of pointers of type Ivar describing the instance variables declared by the class.
  457. * Any instance variables declared by superclasses are not included. The array contains *outCount
  458. * pointers followed by a NULL terminator. You must free the array with free().
  459. *
  460. * If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0.
  461. */
  462. OBJC_EXPORT Ivar _Nonnull * _Nullable
  463. class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
  464. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  465. /**
  466. * Returns a specified instance method for a given class.
  467. *
  468. * @param cls The class you want to inspect.
  469. * @param name The selector of the method you want to retrieve.
  470. *
  471. * @return The method that corresponds to the implementation of the selector specified by
  472. * \e name for the class specified by \e cls, or \c NULL if the specified class or its
  473. * superclasses do not contain an instance method with the specified selector.
  474. *
  475. * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
  476. */
  477. OBJC_EXPORT Method _Nullable
  478. class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
  479. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  480. /**
  481. * Returns a pointer to the data structure describing a given class method for a given class.
  482. *
  483. * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
  484. * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
  485. *
  486. * @return A pointer to the \c Method data structure that corresponds to the implementation of the
  487. * selector specified by aSelector for the class specified by aClass, or NULL if the specified
  488. * class or its superclasses do not contain an instance method with the specified selector.
  489. *
  490. * @note Note that this function searches superclasses for implementations,
  491. * whereas \c class_copyMethodList does not.
  492. */
  493. OBJC_EXPORT Method _Nullable
  494. class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
  495. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  496. /**
  497. * Returns the function pointer that would be called if a
  498. * particular message were sent to an instance of a class.
  499. *
  500. * @param cls The class you want to inspect.
  501. * @param name A selector.
  502. *
  503. * @return The function pointer that would be called if \c [object name] were called
  504. * with an instance of the class, or \c NULL if \e cls is \c Nil.
  505. *
  506. * @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
  507. * @note The function pointer returned may be a function internal to the runtime instead of
  508. * an actual method implementation. For example, if instances of the class do not respond to
  509. * the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
  510. */
  511. OBJC_EXPORT IMP _Nullable
  512. class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)
  513. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  514. /**
  515. * Returns the function pointer that would be called if a particular
  516. * message were sent to an instance of a class.
  517. *
  518. * @param cls The class you want to inspect.
  519. * @param name A selector.
  520. *
  521. * @return The function pointer that would be called if \c [object name] were called
  522. * with an instance of the class, or \c NULL if \e cls is \c Nil.
  523. */
  524. OBJC_EXPORT IMP _Nullable
  525. class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name)
  526. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  527. OBJC_ARM64_UNAVAILABLE;
  528. /**
  529. * Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
  530. *
  531. * @param cls The class you want to inspect.
  532. * @param sel A selector.
  533. *
  534. * @return \c YES if instances of the class respond to the selector, otherwise \c NO.
  535. *
  536. * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector:
  537. * methods instead of this function.
  538. */
  539. OBJC_EXPORT BOOL
  540. class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel)
  541. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  542. /**
  543. * Describes the instance methods implemented by a class.
  544. *
  545. * @param cls The class you want to inspect.
  546. * @param outCount On return, contains the length of the returned array.
  547. * If outCount is NULL, the length is not returned.
  548. *
  549. * @return An array of pointers of type Method describing the instance methods
  550. * implemented by the class—any instance methods implemented by superclasses are not included.
  551. * The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
  552. *
  553. * If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
  554. *
  555. * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
  556. * @note To get the implementations of methods that may be implemented by superclasses,
  557. * use \c class_getInstanceMethod or \c class_getClassMethod.
  558. */
  559. OBJC_EXPORT Method _Nonnull * _Nullable
  560. class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
  561. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  562. /**
  563. * Returns a Boolean value that indicates whether a class conforms to a given protocol.
  564. *
  565. * @param cls The class you want to inspect.
  566. * @param protocol A protocol.
  567. *
  568. * @return YES if cls conforms to protocol, otherwise NO.
  569. *
  570. * @note You should usually use NSObject's conformsToProtocol: method instead of this function.
  571. */
  572. OBJC_EXPORT BOOL
  573. class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol)
  574. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  575. /**
  576. * Describes the protocols adopted by a class.
  577. *
  578. * @param cls The class you want to inspect.
  579. * @param outCount On return, contains the length of the returned array.
  580. * If outCount is NULL, the length is not returned.
  581. *
  582. * @return An array of pointers of type Protocol* describing the protocols adopted
  583. * by the class. Any protocols adopted by superclasses or other protocols are not included.
  584. * The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
  585. *
  586. * If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.
  587. */
  588. OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
  589. class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
  590. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  591. /**
  592. * Returns a property with a given name of a given class.
  593. *
  594. * @param cls The class you want to inspect.
  595. * @param name The name of the property you want to inspect.
  596. *
  597. * @return A pointer of type \c objc_property_t describing the property, or
  598. * \c NULL if the class does not declare a property with that name,
  599. * or \c NULL if \e cls is \c Nil.
  600. */
  601. OBJC_EXPORT objc_property_t _Nullable
  602. class_getProperty(Class _Nullable cls, const char * _Nonnull name)
  603. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  604. /**
  605. * Describes the properties declared by a class.
  606. *
  607. * @param cls The class you want to inspect.
  608. * @param outCount On return, contains the length of the returned array.
  609. * If \e outCount is \c NULL, the length is not returned.
  610. *
  611. * @return An array of pointers of type \c objc_property_t describing the properties
  612. * declared by the class. Any properties declared by superclasses are not included.
  613. * The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
  614. *
  615. * If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
  616. */
  617. OBJC_EXPORT objc_property_t _Nonnull * _Nullable
  618. class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
  619. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  620. /**
  621. * Returns a description of the \c Ivar layout for a given class.
  622. *
  623. * @param cls The class to inspect.
  624. *
  625. * @return A description of the \c Ivar layout for \e cls.
  626. */
  627. OBJC_EXPORT const uint8_t * _Nullable
  628. class_getIvarLayout(Class _Nullable cls)
  629. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  630. /**
  631. * Returns a description of the layout of weak Ivars for a given class.
  632. *
  633. * @param cls The class to inspect.
  634. *
  635. * @return A description of the layout of the weak \c Ivars for \e cls.
  636. */
  637. OBJC_EXPORT const uint8_t * _Nullable
  638. class_getWeakIvarLayout(Class _Nullable cls)
  639. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  640. /**
  641. * Adds a new method to a class with a given name and implementation.
  642. *
  643. * @param cls The class to which to add a method.
  644. * @param name A selector that specifies the name of the method being added.
  645. * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
  646. * @param types An array of characters that describe the types of the arguments to the method.
  647. *
  648. * @return YES if the method was added successfully, otherwise NO
  649. * (for example, the class already contains a method implementation with that name).
  650. *
  651. * @note class_addMethod will add an override of a superclass's implementation,
  652. * but will not replace an existing implementation in this class.
  653. * To change an existing implementation, use method_setImplementation.
  654. */
  655. OBJC_EXPORT BOOL
  656. class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
  657. const char * _Nullable types)
  658. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  659. /**
  660. * Replaces the implementation of a method for a given class.
  661. *
  662. * @param cls The class you want to modify.
  663. * @param name A selector that identifies the method whose implementation you want to replace.
  664. * @param imp The new implementation for the method identified by name for the class identified by cls.
  665. * @param types An array of characters that describe the types of the arguments to the method.
  666. * Since the function must take at least two arguments—self and _cmd, the second and third characters
  667. * must be “@:” (the first character is the return type).
  668. *
  669. * @return The previous implementation of the method identified by \e name for the class identified by \e cls.
  670. *
  671. * @note This function behaves in two different ways:
  672. * - If the method identified by \e name does not yet exist, it is added as if \c class_addMethod were called.
  673. * The type encoding specified by \e types is used as given.
  674. * - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called.
  675. * The type encoding specified by \e types is ignored.
  676. */
  677. OBJC_EXPORT IMP _Nullable
  678. class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
  679. const char * _Nullable types)
  680. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  681. /**
  682. * Adds a new instance variable to a class.
  683. *
  684. * @return YES if the instance variable was added successfully, otherwise NO
  685. * (for example, the class already contains an instance variable with that name).
  686. *
  687. * @note This function may only be called after objc_allocateClassPair and before objc_registerClassPair.
  688. * Adding an instance variable to an existing class is not supported.
  689. * @note The class must not be a metaclass. Adding an instance variable to a metaclass is not supported.
  690. * @note The instance variable's minimum alignment in bytes is 1<<align. The minimum alignment of an instance
  691. * variable depends on the ivar's type and the machine architecture.
  692. * For variables of any pointer type, pass log2(sizeof(pointer_type)).
  693. */
  694. OBJC_EXPORT BOOL
  695. class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size,
  696. uint8_t alignment, const char * _Nullable types)
  697. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  698. /**
  699. * Adds a protocol to a class.
  700. *
  701. * @param cls The class to modify.
  702. * @param protocol The protocol to add to \e cls.
  703. *
  704. * @return \c YES if the method was added successfully, otherwise \c NO
  705. * (for example, the class already conforms to that protocol).
  706. */
  707. OBJC_EXPORT BOOL
  708. class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol)
  709. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  710. /**
  711. * Adds a property to a class.
  712. *
  713. * @param cls The class to modify.
  714. * @param name The name of the property.
  715. * @param attributes An array of property attributes.
  716. * @param attributeCount The number of attributes in \e attributes.
  717. *
  718. * @return \c YES if the property was added successfully, otherwise \c NO
  719. * (for example, the class already has that property).
  720. */
  721. OBJC_EXPORT BOOL
  722. class_addProperty(Class _Nullable cls, const char * _Nonnull name,
  723. const objc_property_attribute_t * _Nullable attributes,
  724. unsigned int attributeCount)
  725. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  726. /**
  727. * Replace a property of a class.
  728. *
  729. * @param cls The class to modify.
  730. * @param name The name of the property.
  731. * @param attributes An array of property attributes.
  732. * @param attributeCount The number of attributes in \e attributes.
  733. */
  734. OBJC_EXPORT void
  735. class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
  736. const objc_property_attribute_t * _Nullable attributes,
  737. unsigned int attributeCount)
  738. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  739. /**
  740. * Sets the Ivar layout for a given class.
  741. *
  742. * @param cls The class to modify.
  743. * @param layout The layout of the \c Ivars for \e cls.
  744. */
  745. OBJC_EXPORT void
  746. class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
  747. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  748. /**
  749. * Sets the layout for weak Ivars for a given class.
  750. *
  751. * @param cls The class to modify.
  752. * @param layout The layout of the weak Ivars for \e cls.
  753. */
  754. OBJC_EXPORT void
  755. class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
  756. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  757. /**
  758. * Used by CoreFoundation's toll-free bridging.
  759. * Return the id of the named class.
  760. *
  761. * @return The id of the named class, or an uninitialized class
  762. * structure that will be used for the class when and if it does
  763. * get loaded.
  764. *
  765. * @warning Do not call this function yourself.
  766. */
  767. OBJC_EXPORT Class _Nonnull
  768. objc_getFutureClass(const char * _Nonnull name)
  769. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  770. OBJC_ARC_UNAVAILABLE;
  771. /* Instantiating Classes */
  772. /**
  773. * Creates an instance of a class, allocating memory for the class in the
  774. * default malloc memory zone.
  775. *
  776. * @param cls The class that you wish to allocate an instance of.
  777. * @param extraBytes An integer indicating the number of extra bytes to allocate.
  778. * The additional bytes can be used to store additional instance variables beyond
  779. * those defined in the class definition.
  780. *
  781. * @return An instance of the class \e cls.
  782. */
  783. OBJC_EXPORT id _Nullable
  784. class_createInstance(Class _Nullable cls, size_t extraBytes)
  785. OBJC_RETURNS_RETAINED
  786. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  787. /**
  788. * Creates an instance of a class at the specific location provided.
  789. *
  790. * @param cls The class that you wish to allocate an instance of.
  791. * @param bytes The location at which to allocate an instance of \e cls.
  792. * Must point to at least \c class_getInstanceSize(cls) bytes of well-aligned,
  793. * zero-filled memory.
  794. *
  795. * @return \e bytes on success, \c nil otherwise. (For example, \e cls or \e bytes
  796. * might be \c nil)
  797. *
  798. * @see class_createInstance
  799. */
  800. OBJC_EXPORT id _Nullable
  801. objc_constructInstance(Class _Nullable cls, void * _Nullable bytes)
  802. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  803. OBJC_ARC_UNAVAILABLE;
  804. /**
  805. * Destroys an instance of a class without freeing memory and removes any
  806. * associated references this instance might have had.
  807. *
  808. * @param obj The class instance to destroy.
  809. *
  810. * @return \e obj. Does nothing if \e obj is nil.
  811. *
  812. * @note CF and other clients do call this under GC.
  813. */
  814. OBJC_EXPORT void * _Nullable objc_destructInstance(id _Nullable obj)
  815. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  816. OBJC_ARC_UNAVAILABLE;
  817. /* Adding Classes */
  818. /**
  819. * Creates a new class and metaclass.
  820. *
  821. * @param superclass The class to use as the new class's superclass, or \c Nil to create a new root class.
  822. * @param name The string to use as the new class's name. The string will be copied.
  823. * @param extraBytes The number of bytes to allocate for indexed ivars at the end of
  824. * the class and metaclass objects. This should usually be \c 0.
  825. *
  826. * @return The new class, or Nil if the class could not be created (for example, the desired name is already in use).
  827. *
  828. * @note You can get a pointer to the new metaclass by calling \c object_getClass(newClass).
  829. * @note To create a new class, start by calling \c objc_allocateClassPair.
  830. * Then set the class's attributes with functions like \c class_addMethod and \c class_addIvar.
  831. * When you are done building the class, call \c objc_registerClassPair. The new class is now ready for use.
  832. * @note Instance methods and instance variables should be added to the class itself.
  833. * Class methods should be added to the metaclass.
  834. */
  835. OBJC_EXPORT Class _Nullable
  836. objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name,
  837. size_t extraBytes)
  838. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  839. /**
  840. * Registers a class that was allocated using \c objc_allocateClassPair.
  841. *
  842. * @param cls The class you want to register.
  843. */
  844. OBJC_EXPORT void
  845. objc_registerClassPair(Class _Nonnull cls)
  846. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  847. /**
  848. * Used by Foundation's Key-Value Observing.
  849. *
  850. * @warning Do not call this function yourself.
  851. */
  852. OBJC_EXPORT Class _Nonnull
  853. objc_duplicateClass(Class _Nonnull original, const char * _Nonnull name,
  854. size_t extraBytes)
  855. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  856. /**
  857. * Destroy a class and its associated metaclass.
  858. *
  859. * @param cls The class to be destroyed. It must have been allocated with
  860. * \c objc_allocateClassPair
  861. *
  862. * @warning Do not call if instances of this class or a subclass exist.
  863. */
  864. OBJC_EXPORT void
  865. objc_disposeClassPair(Class _Nonnull cls)
  866. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  867. /* Working with Methods */
  868. /**
  869. * Returns the name of a method.
  870. *
  871. * @param m The method to inspect.
  872. *
  873. * @return A pointer of type SEL.
  874. *
  875. * @note To get the method name as a C string, call \c sel_getName(method_getName(method)).
  876. */
  877. OBJC_EXPORT SEL _Nonnull
  878. method_getName(Method _Nonnull m)
  879. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  880. /**
  881. * Returns the implementation of a method.
  882. *
  883. * @param m The method to inspect.
  884. *
  885. * @return A function pointer of type IMP.
  886. */
  887. OBJC_EXPORT IMP _Nonnull
  888. method_getImplementation(Method _Nonnull m)
  889. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  890. /**
  891. * Returns a string describing a method's parameter and return types.
  892. *
  893. * @param m The method to inspect.
  894. *
  895. * @return A C string. The string may be \c NULL.
  896. */
  897. OBJC_EXPORT const char * _Nullable
  898. method_getTypeEncoding(Method _Nonnull m)
  899. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  900. /**
  901. * Returns the number of arguments accepted by a method.
  902. *
  903. * @param m A pointer to a \c Method data structure. Pass the method in question.
  904. *
  905. * @return An integer containing the number of arguments accepted by the given method.
  906. */
  907. OBJC_EXPORT unsigned int
  908. method_getNumberOfArguments(Method _Nonnull m)
  909. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  910. /**
  911. * Returns a string describing a method's return type.
  912. *
  913. * @param m The method to inspect.
  914. *
  915. * @return A C string describing the return type. You must free the string with \c free().
  916. */
  917. OBJC_EXPORT char * _Nonnull
  918. method_copyReturnType(Method _Nonnull m)
  919. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  920. /**
  921. * Returns a string describing a single parameter type of a method.
  922. *
  923. * @param m The method to inspect.
  924. * @param index The index of the parameter to inspect.
  925. *
  926. * @return A C string describing the type of the parameter at index \e index, or \c NULL
  927. * if method has no parameter index \e index. You must free the string with \c free().
  928. */
  929. OBJC_EXPORT char * _Nullable
  930. method_copyArgumentType(Method _Nonnull m, unsigned int index)
  931. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  932. /**
  933. * Returns by reference a string describing a method's return type.
  934. *
  935. * @param m The method you want to inquire about.
  936. * @param dst The reference string to store the description.
  937. * @param dst_len The maximum number of characters that can be stored in \e dst.
  938. *
  939. * @note The method's return type string is copied to \e dst.
  940. * \e dst is filled as if \c strncpy(dst, parameter_type, dst_len) were called.
  941. */
  942. OBJC_EXPORT void
  943. method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len)
  944. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  945. /**
  946. * Returns by reference a string describing a single parameter type of a method.
  947. *
  948. * @param m The method you want to inquire about.
  949. * @param index The index of the parameter you want to inquire about.
  950. * @param dst The reference string to store the description.
  951. * @param dst_len The maximum number of characters that can be stored in \e dst.
  952. *
  953. * @note The parameter type string is copied to \e dst. \e dst is filled as if \c strncpy(dst, parameter_type, dst_len)
  954. * were called. If the method contains no parameter with that index, \e dst is filled as
  955. * if \c strncpy(dst, "", dst_len) were called.
  956. */
  957. OBJC_EXPORT void
  958. method_getArgumentType(Method _Nonnull m, unsigned int index,
  959. char * _Nullable dst, size_t dst_len)
  960. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  961. OBJC_EXPORT struct objc_method_description * _Nonnull
  962. method_getDescription(Method _Nonnull m)
  963. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  964. /**
  965. * Sets the implementation of a method.
  966. *
  967. * @param m The method for which to set an implementation.
  968. * @param imp The implemention to set to this method.
  969. *
  970. * @return The previous implementation of the method.
  971. */
  972. OBJC_EXPORT IMP _Nonnull
  973. method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
  974. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  975. /**
  976. * Exchanges the implementations of two methods.
  977. *
  978. * @param m1 Method to exchange with second method.
  979. * @param m2 Method to exchange with first method.
  980. *
  981. * @note This is an atomic version of the following:
  982. * \code
  983. * IMP imp1 = method_getImplementation(m1);
  984. * IMP imp2 = method_getImplementation(m2);
  985. * method_setImplementation(m1, imp2);
  986. * method_setImplementation(m2, imp1);
  987. * \endcode
  988. */
  989. OBJC_EXPORT void
  990. method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
  991. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  992. /* Working with Instance Variables */
  993. /**
  994. * Returns the name of an instance variable.
  995. *
  996. * @param v The instance variable you want to enquire about.
  997. *
  998. * @return A C string containing the instance variable's name.
  999. */
  1000. OBJC_EXPORT const char * _Nullable
  1001. ivar_getName(Ivar _Nonnull v)
  1002. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1003. /**
  1004. * Returns the type string of an instance variable.
  1005. *
  1006. * @param v The instance variable you want to enquire about.
  1007. *
  1008. * @return A C string containing the instance variable's type encoding.
  1009. *
  1010. * @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
  1011. */
  1012. OBJC_EXPORT const char * _Nullable
  1013. ivar_getTypeEncoding(Ivar _Nonnull v)
  1014. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1015. /**
  1016. * Returns the offset of an instance variable.
  1017. *
  1018. * @param v The instance variable you want to enquire about.
  1019. *
  1020. * @return The offset of \e v.
  1021. *
  1022. * @note For instance variables of type \c id or other object types, call \c object_getIvar
  1023. * and \c object_setIvar instead of using this offset to access the instance variable data directly.
  1024. */
  1025. OBJC_EXPORT ptrdiff_t
  1026. ivar_getOffset(Ivar _Nonnull v)
  1027. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1028. /* Working with Properties */
  1029. /**
  1030. * Returns the name of a property.
  1031. *
  1032. * @param property The property you want to inquire about.
  1033. *
  1034. * @return A C string containing the property's name.
  1035. */
  1036. OBJC_EXPORT const char * _Nonnull
  1037. property_getName(objc_property_t _Nonnull property)
  1038. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1039. /**
  1040. * Returns the attribute string of a property.
  1041. *
  1042. * @param property A property.
  1043. *
  1044. * @return A C string containing the property's attributes.
  1045. *
  1046. * @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide.
  1047. */
  1048. OBJC_EXPORT const char * _Nullable
  1049. property_getAttributes(objc_property_t _Nonnull property)
  1050. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1051. /**
  1052. * Returns an array of property attributes for a property.
  1053. *
  1054. * @param property The property whose attributes you want copied.
  1055. * @param outCount The number of attributes returned in the array.
  1056. *
  1057. * @return An array of property attributes; must be free'd() by the caller.
  1058. */
  1059. OBJC_EXPORT objc_property_attribute_t * _Nullable
  1060. property_copyAttributeList(objc_property_t _Nonnull property,
  1061. unsigned int * _Nullable outCount)
  1062. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1063. /**
  1064. * Returns the value of a property attribute given the attribute name.
  1065. *
  1066. * @param property The property whose attribute value you are interested in.
  1067. * @param attributeName C string representing the attribute name.
  1068. *
  1069. * @return The value string of the attribute \e attributeName if it exists in
  1070. * \e property, \c nil otherwise.
  1071. */
  1072. OBJC_EXPORT char * _Nullable
  1073. property_copyAttributeValue(objc_property_t _Nonnull property,
  1074. const char * _Nonnull attributeName)
  1075. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1076. /* Working with Protocols */
  1077. /**
  1078. * Returns a specified protocol.
  1079. *
  1080. * @param name The name of a protocol.
  1081. *
  1082. * @return The protocol named \e name, or \c NULL if no protocol named \e name could be found.
  1083. *
  1084. * @note This function acquires the runtime lock.
  1085. */
  1086. OBJC_EXPORT Protocol * _Nullable
  1087. objc_getProtocol(const char * _Nonnull name)
  1088. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1089. /**
  1090. * Returns an array of all the protocols known to the runtime.
  1091. *
  1092. * @param outCount Upon return, contains the number of protocols in the returned array.
  1093. *
  1094. * @return A C array of all the protocols known to the runtime. The array contains \c *outCount
  1095. * pointers followed by a \c NULL terminator. You must free the list with \c free().
  1096. *
  1097. * @note This function acquires the runtime lock.
  1098. */
  1099. OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
  1100. objc_copyProtocolList(unsigned int * _Nullable outCount)
  1101. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1102. /**
  1103. * Returns a Boolean value that indicates whether one protocol conforms to another protocol.
  1104. *
  1105. * @param proto A protocol.
  1106. * @param other A protocol.
  1107. *
  1108. * @return \c YES if \e proto conforms to \e other, otherwise \c NO.
  1109. *
  1110. * @note One protocol can incorporate other protocols using the same syntax
  1111. * that classes use to adopt a protocol:
  1112. * \code
  1113. * @protocol ProtocolName < protocol list >
  1114. * \endcode
  1115. * All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
  1116. */
  1117. OBJC_EXPORT BOOL
  1118. protocol_conformsToProtocol(Protocol * _Nullable proto,
  1119. Protocol * _Nullable other)
  1120. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1121. /**
  1122. * Returns a Boolean value that indicates whether two protocols are equal.
  1123. *
  1124. * @param proto A protocol.
  1125. * @param other A protocol.
  1126. *
  1127. * @return \c YES if \e proto is the same as \e other, otherwise \c NO.
  1128. */
  1129. OBJC_EXPORT BOOL
  1130. protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
  1131. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1132. /**
  1133. * Returns the name of a protocol.
  1134. *
  1135. * @param proto A protocol.
  1136. *
  1137. * @return The name of the protocol \e p as a C string.
  1138. */
  1139. OBJC_EXPORT const char * _Nonnull
  1140. protocol_getName(Protocol * _Nonnull proto)
  1141. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1142. /**
  1143. * Returns a method description structure for a specified method of a given protocol.
  1144. *
  1145. * @param proto A protocol.
  1146. * @param aSel A selector.
  1147. * @param isRequiredMethod A Boolean value that indicates whether aSel is a required method.
  1148. * @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method.
  1149. *
  1150. * @return An \c objc_method_description structure that describes the method specified by \e aSel,
  1151. * \e isRequiredMethod, and \e isInstanceMethod for the protocol \e p.
  1152. * If the protocol does not contain the specified method, returns an \c objc_method_description structure
  1153. * with the value \c {NULL, \c NULL}.
  1154. *
  1155. * @note This function recursively searches any protocols that this protocol conforms to.
  1156. */
  1157. OBJC_EXPORT struct objc_method_description
  1158. protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
  1159. BOOL isRequiredMethod, BOOL isInstanceMethod)
  1160. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1161. /**
  1162. * Returns an array of method descriptions of methods meeting a given specification for a given protocol.
  1163. *
  1164. * @param proto A protocol.
  1165. * @param isRequiredMethod A Boolean value that indicates whether returned methods should
  1166. * be required methods (pass YES to specify required methods).
  1167. * @param isInstanceMethod A Boolean value that indicates whether returned methods should
  1168. * be instance methods (pass YES to specify instance methods).
  1169. * @param outCount Upon return, contains the number of method description structures in the returned array.
  1170. *
  1171. * @return A C array of \c objc_method_description structures containing the names and types of \e p's methods
  1172. * specified by \e isRequiredMethod and \e isInstanceMethod. The array contains \c *outCount pointers followed
  1173. * by a \c NULL terminator. You must free the list with \c free().
  1174. * If the protocol declares no methods that meet the specification, \c NULL is returned and \c *outCount is 0.
  1175. *
  1176. * @note Methods in other protocols adopted by this protocol are not included.
  1177. */
  1178. OBJC_EXPORT struct objc_method_description * _Nullable
  1179. protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
  1180. BOOL isRequiredMethod,
  1181. BOOL isInstanceMethod,
  1182. unsigned int * _Nullable outCount)
  1183. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1184. /**
  1185. * Returns the specified property of a given protocol.
  1186. *
  1187. * @param proto A protocol.
  1188. * @param name The name of a property.
  1189. * @param isRequiredProperty \c YES searches for a required property, \c NO searches for an optional property.
  1190. * @param isInstanceProperty \c YES searches for an instance property, \c NO searches for a class property.
  1191. *
  1192. * @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto,
  1193. * or \c NULL if none of \e proto's properties meets the specification.
  1194. */
  1195. OBJC_EXPORT objc_property_t _Nullable
  1196. protocol_getProperty(Protocol * _Nonnull proto,
  1197. const char * _Nonnull name,
  1198. BOOL isRequiredProperty, BOOL isInstanceProperty)
  1199. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1200. /**
  1201. * Returns an array of the required instance properties declared by a protocol.
  1202. *
  1203. * @note Identical to
  1204. * \code
  1205. * protocol_copyPropertyList2(proto, outCount, YES, YES);
  1206. * \endcode
  1207. */
  1208. OBJC_EXPORT objc_property_t _Nonnull * _Nullable
  1209. protocol_copyPropertyList(Protocol * _Nonnull proto,
  1210. unsigned int * _Nullable outCount)
  1211. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1212. /**
  1213. * Returns an array of properties declared by a protocol.
  1214. *
  1215. * @param proto A protocol.
  1216. * @param outCount Upon return, contains the number of elements in the returned array.
  1217. * @param isRequiredProperty \c YES returns required properties, \c NO returns optional properties.
  1218. * @param isInstanceProperty \c YES returns instance properties, \c NO returns class properties.
  1219. *
  1220. * @return A C array of pointers of type \c objc_property_t describing the properties declared by \e proto.
  1221. * Any properties declared by other protocols adopted by this protocol are not included. The array contains
  1222. * \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
  1223. * If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0.
  1224. */
  1225. OBJC_EXPORT objc_property_t _Nonnull * _Nullable
  1226. protocol_copyPropertyList2(Protocol * _Nonnull proto,
  1227. unsigned int * _Nullable outCount,
  1228. BOOL isRequiredProperty, BOOL isInstanceProperty)
  1229. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  1230. /**
  1231. * Returns an array of the protocols adopted by a protocol.
  1232. *
  1233. * @param proto A protocol.
  1234. * @param outCount Upon return, contains the number of elements in the returned array.
  1235. *
  1236. * @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers
  1237. * followed by a \c NULL terminator. You must free the array with \c free().
  1238. * If the protocol declares no properties, \c NULL is returned and \c *outCount is \c 0.
  1239. */
  1240. OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
  1241. protocol_copyProtocolList(Protocol * _Nonnull proto,
  1242. unsigned int * _Nullable outCount)
  1243. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1244. /**
  1245. * Creates a new protocol instance that cannot be used until registered with
  1246. * \c objc_registerProtocol()
  1247. *
  1248. * @param name The name of the protocol to create.
  1249. *
  1250. * @return The Protocol instance on success, \c nil if a protocol
  1251. * with the same name already exists.
  1252. * @note There is no dispose method for this.
  1253. */
  1254. OBJC_EXPORT Protocol * _Nullable
  1255. objc_allocateProtocol(const char * _Nonnull name)
  1256. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1257. /**
  1258. * Registers a newly constructed protocol with the runtime. The protocol
  1259. * will be ready for use and is immutable after this.
  1260. *
  1261. * @param proto The protocol you want to register.
  1262. */
  1263. OBJC_EXPORT void
  1264. objc_registerProtocol(Protocol * _Nonnull proto)
  1265. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1266. /**
  1267. * Adds a method to a protocol. The protocol must be under construction.
  1268. *
  1269. * @param proto The protocol to add a method to.
  1270. * @param name The name of the method to add.
  1271. * @param types A C string that represents the method signature.
  1272. * @param isRequiredMethod YES if the method is not an optional method.
  1273. * @param isInstanceMethod YES if the method is an instance method.
  1274. */
  1275. OBJC_EXPORT void
  1276. protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
  1277. const char * _Nullable types,
  1278. BOOL isRequiredMethod, BOOL isInstanceMethod)
  1279. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1280. /**
  1281. * Adds an incorporated protocol to another protocol. The protocol being
  1282. * added to must still be under construction, while the additional protocol
  1283. * must be already constructed.
  1284. *
  1285. * @param proto The protocol you want to add to, it must be under construction.
  1286. * @param addition The protocol you want to incorporate into \e proto, it must be registered.
  1287. */
  1288. OBJC_EXPORT void
  1289. protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition)
  1290. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1291. /**
  1292. * Adds a property to a protocol. The protocol must be under construction.
  1293. *
  1294. * @param proto The protocol to add a property to.
  1295. * @param name The name of the property.
  1296. * @param attributes An array of property attributes.
  1297. * @param attributeCount The number of attributes in \e attributes.
  1298. * @param isRequiredProperty YES if the property (accessor methods) is not optional.
  1299. * @param isInstanceProperty YES if the property (accessor methods) are instance methods.
  1300. * This is the only case allowed fo a property, as a result, setting this to NO will
  1301. * not add the property to the protocol at all.
  1302. */
  1303. OBJC_EXPORT void
  1304. protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
  1305. const objc_property_attribute_t * _Nullable attributes,
  1306. unsigned int attributeCount,
  1307. BOOL isRequiredProperty, BOOL isInstanceProperty)
  1308. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1309. /* Working with Libraries */
  1310. /**
  1311. * Returns the names of all the loaded Objective-C frameworks and dynamic
  1312. * libraries.
  1313. *
  1314. * @param outCount The number of names returned.
  1315. *
  1316. * @return An array of C strings of names. Must be free()'d by caller.
  1317. */
  1318. OBJC_EXPORT const char * _Nonnull * _Nonnull
  1319. objc_copyImageNames(unsigned int * _Nullable outCount)
  1320. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1321. /**
  1322. * Returns the dynamic library name a class originated from.
  1323. *
  1324. * @param cls The class you are inquiring about.
  1325. *
  1326. * @return The name of the library containing this class.
  1327. */
  1328. OBJC_EXPORT const char * _Nullable
  1329. class_getImageName(Class _Nullable cls)
  1330. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1331. /**
  1332. * Returns the names of all the classes within a library.
  1333. *
  1334. * @param image The library or framework you are inquiring about.
  1335. * @param outCount The number of class names returned.
  1336. *
  1337. * @return An array of C strings representing the class names.
  1338. */
  1339. OBJC_EXPORT const char * _Nonnull * _Nullable
  1340. objc_copyClassNamesForImage(const char * _Nonnull image,
  1341. unsigned int * _Nullable outCount)
  1342. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1343. /* Working with Selectors */
  1344. /**
  1345. * Returns the name of the method specified by a given selector.
  1346. *
  1347. * @param sel A pointer of type \c SEL. Pass the selector whose name you wish to determine.
  1348. *
  1349. * @return A C string indicating the name of the selector.
  1350. */
  1351. OBJC_EXPORT const char * _Nonnull
  1352. sel_getName(SEL _Nonnull sel)
  1353. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  1354. /**
  1355. * Registers a method with the Objective-C runtime system, maps the method
  1356. * name to a selector, and returns the selector value.
  1357. *
  1358. * @param str A pointer to a C string. Pass the name of the method you wish to register.
  1359. *
  1360. * @return A pointer of type SEL specifying the selector for the named method.
  1361. *
  1362. * @note You must register a method name with the Objective-C runtime system to obtain the
  1363. * method’s selector before you can add the method to a class definition. If the method name
  1364. * has already been registered, this function simply returns the selector.
  1365. */
  1366. OBJC_EXPORT SEL _Nonnull
  1367. sel_registerName(const char * _Nonnull str)
  1368. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  1369. /**
  1370. * Returns a Boolean value that indicates whether two selectors are equal.
  1371. *
  1372. * @param lhs The selector to compare with rhs.
  1373. * @param rhs The selector to compare with lhs.
  1374. *
  1375. * @return \c YES if \e lhs and \e rhs are equal, otherwise \c NO.
  1376. *
  1377. * @note sel_isEqual is equivalent to ==.
  1378. */
  1379. OBJC_EXPORT BOOL
  1380. sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs)
  1381. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1382. /* Objective-C Language Features */
  1383. /**
  1384. * This function is inserted by the compiler when a mutation
  1385. * is detected during a foreach iteration. It gets called
  1386. * when a mutation occurs, and the enumerationMutationHandler
  1387. * is enacted if it is set up. A fatal error occurs if a handler is not set up.
  1388. *
  1389. * @param obj The object being mutated.
  1390. *
  1391. */
  1392. OBJC_EXPORT void
  1393. objc_enumerationMutation(id _Nonnull obj)
  1394. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1395. /**
  1396. * Sets the current mutation handler.
  1397. *
  1398. * @param handler Function pointer to the new mutation handler.
  1399. */
  1400. OBJC_EXPORT void
  1401. objc_setEnumerationMutationHandler(void (*_Nullable handler)(id _Nonnull ))
  1402. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1403. /**
  1404. * Set the function to be called by objc_msgForward.
  1405. *
  1406. * @param fwd Function to be jumped to by objc_msgForward.
  1407. * @param fwd_stret Function to be jumped to by objc_msgForward_stret.
  1408. *
  1409. * @see message.h::_objc_msgForward
  1410. */
  1411. OBJC_EXPORT void
  1412. objc_setForwardHandler(void * _Nonnull fwd, void * _Nonnull fwd_stret)
  1413. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  1414. /**
  1415. * Creates a pointer to a function that will call the block
  1416. * when the method is called.
  1417. *
  1418. * @param block The block that implements this method. Its signature should
  1419. * be: method_return_type ^(id self, method_args...).
  1420. * The selector is not available as a parameter to this block.
  1421. * The block is copied with \c Block_copy().
  1422. *
  1423. * @return The IMP that calls this block. Must be disposed of with
  1424. * \c imp_removeBlock.
  1425. */
  1426. OBJC_EXPORT IMP _Nonnull
  1427. imp_implementationWithBlock(id _Nonnull block)
  1428. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1429. /**
  1430. * Return the block associated with an IMP that was created using
  1431. * \c imp_implementationWithBlock.
  1432. *
  1433. * @param anImp The IMP that calls this block.
  1434. *
  1435. * @return The block called by \e anImp.
  1436. */
  1437. OBJC_EXPORT id _Nullable
  1438. imp_getBlock(IMP _Nonnull anImp)
  1439. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1440. /**
  1441. * Disassociates a block from an IMP that was created using
  1442. * \c imp_implementationWithBlock and releases the copy of the
  1443. * block that was created.
  1444. *
  1445. * @param anImp An IMP that was created using \c imp_implementationWithBlock.
  1446. *
  1447. * @return YES if the block was released successfully, NO otherwise.
  1448. * (For example, the block might not have been used to create an IMP previously).
  1449. */
  1450. OBJC_EXPORT BOOL
  1451. imp_removeBlock(IMP _Nonnull anImp)
  1452. OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
  1453. /**
  1454. * This loads the object referenced by a weak pointer and returns it, after
  1455. * retaining and autoreleasing the object to ensure that it stays alive
  1456. * long enough for the caller to use it. This function would be used
  1457. * anywhere a __weak variable is used in an expression.
  1458. *
  1459. * @param location The weak pointer address
  1460. *
  1461. * @return The object pointed to by \e location, or \c nil if \e *location is \c nil.
  1462. */
  1463. OBJC_EXPORT id _Nullable
  1464. objc_loadWeak(id _Nullable * _Nonnull location)
  1465. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  1466. /**
  1467. * This function stores a new value into a __weak variable. It would
  1468. * be used anywhere a __weak variable is the target of an assignment.
  1469. *
  1470. * @param location The address of the weak pointer itself
  1471. * @param obj The new object this weak ptr should now point to
  1472. *
  1473. * @return The value stored into \e location, i.e. \e obj
  1474. */
  1475. OBJC_EXPORT id _Nullable
  1476. objc_storeWeak(id _Nullable * _Nonnull location, id _Nullable obj)
  1477. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  1478. /* Associative References */
  1479. /**
  1480. * Policies related to associative references.
  1481. * These are options to objc_setAssociatedObject()
  1482. */
  1483. typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
  1484. OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
  1485. OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
  1486. * The association is not made atomically. */
  1487. OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
  1488. * The association is not made atomically. */
  1489. OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
  1490. * The association is made atomically. */
  1491. OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
  1492. * The association is made atomically. */
  1493. };
  1494. /**
  1495. * Sets an associated value for a given object using a given key and association policy.
  1496. *
  1497. * @param object The source object for the association.
  1498. * @param key The key for the association.
  1499. * @param value The value to associate with the key key for object. Pass nil to clear an existing association.
  1500. * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
  1501. *
  1502. * @see objc_setAssociatedObject
  1503. * @see objc_removeAssociatedObjects
  1504. */
  1505. OBJC_EXPORT void
  1506. objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
  1507. id _Nullable value, objc_AssociationPolicy policy)
  1508. OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
  1509. /**
  1510. * Returns the value associated with a given object for a given key.
  1511. *
  1512. * @param object The source object for the association.
  1513. * @param key The key for the association.
  1514. *
  1515. * @return The value associated with the key \e key for \e object.
  1516. *
  1517. * @see objc_setAssociatedObject
  1518. */
  1519. OBJC_EXPORT id _Nullable
  1520. objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
  1521. OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
  1522. /**
  1523. * Removes all associations for a given object.
  1524. *
  1525. * @param object An object that maintains associated objects.
  1526. *
  1527. * @note The main purpose of this function is to make it easy to return an object
  1528. * to a "pristine state”. You should not use this function for general removal of
  1529. * associations from objects, since it also removes associations that other clients
  1530. * may have added to the object. Typically you should use \c objc_setAssociatedObject
  1531. * with a nil value to clear an association.
  1532. *
  1533. * @see objc_setAssociatedObject
  1534. * @see objc_getAssociatedObject
  1535. */
  1536. OBJC_EXPORT void
  1537. objc_removeAssociatedObjects(id _Nonnull object)
  1538. OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
  1539. /* Hooks for Swift */
  1540. /**
  1541. * Function type for a hook that intercepts class_getImageName().
  1542. *
  1543. * @param cls The class whose image name is being looked up.
  1544. * @param outImageName On return, the result of the image name lookup.
  1545. * @return YES if an image name for this class was found, NO otherwise.
  1546. *
  1547. * @see class_getImageName
  1548. * @see objc_setHook_getImageName
  1549. */
  1550. typedef BOOL (*objc_hook_getImageName)(Class _Nonnull cls, const char * _Nullable * _Nonnull outImageName);
  1551. /**
  1552. * Install a hook for class_getImageName().
  1553. *
  1554. * @param newValue The hook function to install.
  1555. * @param outOldValue The address of a function pointer variable. On return,
  1556. * the old hook function is stored in the variable.
  1557. *
  1558. * @note The store to *outOldValue is thread-safe: the variable will be
  1559. * updated before class_getImageName() calls your new hook to read it,
  1560. * even if your new hook is called from another thread before this
  1561. * setter completes.
  1562. * @note The first hook in the chain is the native implementation of
  1563. * class_getImageName(). Your hook should call the previous hook for
  1564. * classes that you do not recognize.
  1565. *
  1566. * @see class_getImageName
  1567. * @see objc_hook_getImageName
  1568. */
  1569. OBJC_EXPORT void objc_setHook_getImageName(objc_hook_getImageName _Nonnull newValue,
  1570. objc_hook_getImageName _Nullable * _Nonnull outOldValue)
  1571. OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0);
  1572. #define _C_ID '@'
  1573. #define _C_CLASS '#'
  1574. #define _C_SEL ':'
  1575. #define _C_CHR 'c'
  1576. #define _C_UCHR 'C'
  1577. #define _C_SHT 's'
  1578. #define _C_USHT 'S'
  1579. #define _C_INT 'i'
  1580. #define _C_UINT 'I'
  1581. #define _C_LNG 'l'
  1582. #define _C_ULNG 'L'
  1583. #define _C_LNG_LNG 'q'
  1584. #define _C_ULNG_LNG 'Q'
  1585. #define _C_FLT 'f'
  1586. #define _C_DBL 'd'
  1587. #define _C_BFLD 'b'
  1588. #define _C_BOOL 'B'
  1589. #define _C_VOID 'v'
  1590. #define _C_UNDEF '?'
  1591. #define _C_PTR '^'
  1592. #define _C_CHARPTR '*'
  1593. #define _C_ATOM '%'
  1594. #define _C_ARY_B '['
  1595. #define _C_ARY_E ']'
  1596. #define _C_UNION_B '('
  1597. #define _C_UNION_E ')'
  1598. #define _C_STRUCT_B '{'
  1599. #define _C_STRUCT_E '}'
  1600. #define _C_VECTOR '!'
  1601. #define _C_CONST 'r'
  1602. /* Obsolete types */
  1603. #if !__OBJC2__
  1604. #define CLS_GETINFO(cls,infomask) ((cls)->info & (infomask))
  1605. #define CLS_SETINFO(cls,infomask) ((cls)->info |= (infomask))
  1606. // class is not a metaclass
  1607. #define CLS_CLASS 0x1
  1608. // class is a metaclass
  1609. #define CLS_META 0x2
  1610. // class's +initialize method has completed
  1611. #define CLS_INITIALIZED 0x4
  1612. // class is posing
  1613. #define CLS_POSING 0x8
  1614. // unused
  1615. #define CLS_MAPPED 0x10
  1616. // class and subclasses need cache flush during image loading
  1617. #define CLS_FLUSH_CACHE 0x20
  1618. // method cache should grow when full
  1619. #define CLS_GROW_CACHE 0x40
  1620. // unused
  1621. #define CLS_NEED_BIND 0x80
  1622. // methodLists is array of method lists
  1623. #define CLS_METHOD_ARRAY 0x100
  1624. // the JavaBridge constructs classes with these markers
  1625. #define CLS_JAVA_HYBRID 0x200
  1626. #define CLS_JAVA_CLASS 0x400
  1627. // thread-safe +initialize
  1628. #define CLS_INITIALIZING 0x800
  1629. // bundle unloading
  1630. #define CLS_FROM_BUNDLE 0x1000
  1631. // C++ ivar support
  1632. #define CLS_HAS_CXX_STRUCTORS 0x2000
  1633. // Lazy method list arrays
  1634. #define CLS_NO_METHOD_ARRAY 0x4000
  1635. // +load implementation
  1636. #define CLS_HAS_LOAD_METHOD 0x8000
  1637. // objc_allocateClassPair API
  1638. #define CLS_CONSTRUCTING 0x10000
  1639. // class compiled with bigger class structure
  1640. #define CLS_EXT 0x20000
  1641. struct objc_method_description_list {
  1642. int count;
  1643. struct objc_method_description list[1];
  1644. };
  1645. struct objc_protocol_list {
  1646. struct objc_protocol_list * _Nullable next;
  1647. long count;
  1648. __unsafe_unretained Protocol * _Nullable list[1];
  1649. };
  1650. struct objc_category {
  1651. char * _Nonnull category_name OBJC2_UNAVAILABLE;
  1652. char * _Nonnull class_name OBJC2_UNAVAILABLE;
  1653. struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE;
  1654. struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;
  1655. struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
  1656. } OBJC2_UNAVAILABLE;
  1657. struct objc_ivar {
  1658. char * _Nullable ivar_name OBJC2_UNAVAILABLE;
  1659. char * _Nullable ivar_type OBJC2_UNAVAILABLE;
  1660. int ivar_offset OBJC2_UNAVAILABLE;
  1661. #ifdef __LP64__
  1662. int space OBJC2_UNAVAILABLE;
  1663. #endif
  1664. } OBJC2_UNAVAILABLE;
  1665. struct objc_ivar_list {
  1666. int ivar_count OBJC2_UNAVAILABLE;
  1667. #ifdef __LP64__
  1668. int space OBJC2_UNAVAILABLE;
  1669. #endif
  1670. /* variable length structure */
  1671. struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;
  1672. } OBJC2_UNAVAILABLE;
  1673. struct objc_method {
  1674. SEL _Nonnull method_name OBJC2_UNAVAILABLE;
  1675. char * _Nullable method_types OBJC2_UNAVAILABLE;
  1676. IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
  1677. } OBJC2_UNAVAILABLE;
  1678. struct objc_method_list {
  1679. struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE;
  1680. int method_count OBJC2_UNAVAILABLE;
  1681. #ifdef __LP64__
  1682. int space OBJC2_UNAVAILABLE;
  1683. #endif
  1684. /* variable length structure */
  1685. struct objc_method method_list[1] OBJC2_UNAVAILABLE;
  1686. } OBJC2_UNAVAILABLE;
  1687. typedef struct objc_symtab *Symtab OBJC2_UNAVAILABLE;
  1688. struct objc_symtab {
  1689. unsigned long sel_ref_cnt OBJC2_UNAVAILABLE;
  1690. SEL _Nonnull * _Nullable refs OBJC2_UNAVAILABLE;
  1691. unsigned short cls_def_cnt OBJC2_UNAVAILABLE;
  1692. unsigned short cat_def_cnt OBJC2_UNAVAILABLE;
  1693. void * _Nullable defs[1] /* variable size */ OBJC2_UNAVAILABLE;
  1694. } OBJC2_UNAVAILABLE;
  1695. typedef struct objc_cache *Cache OBJC2_UNAVAILABLE;
  1696. #define CACHE_BUCKET_NAME(B) ((B)->method_name)
  1697. #define CACHE_BUCKET_IMP(B) ((B)->method_imp)
  1698. #define CACHE_BUCKET_VALID(B) (B)
  1699. #ifndef __LP64__
  1700. #define CACHE_HASH(sel, mask) (((uintptr_t)(sel)>>2) & (mask))
  1701. #else
  1702. #define CACHE_HASH(sel, mask) (((unsigned int)((uintptr_t)(sel)>>3)) & (mask))
  1703. #endif
  1704. struct objc_cache {
  1705. unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE;
  1706. unsigned int occupied OBJC2_UNAVAILABLE;
  1707. Method _Nullable buckets[1] OBJC2_UNAVAILABLE;
  1708. };
  1709. typedef struct objc_module *Module OBJC2_UNAVAILABLE;
  1710. struct objc_module {
  1711. unsigned long version OBJC2_UNAVAILABLE;
  1712. unsigned long size OBJC2_UNAVAILABLE;
  1713. const char * _Nullable name OBJC2_UNAVAILABLE;
  1714. Symtab _Nullable symtab OBJC2_UNAVAILABLE;
  1715. } OBJC2_UNAVAILABLE;
  1716. #else
  1717. struct objc_method_list;
  1718. #endif
  1719. /* Obsolete functions */
  1720. OBJC_EXPORT IMP _Nullable
  1721. class_lookupMethod(Class _Nullable cls, SEL _Nonnull sel)
  1722. __OSX_DEPRECATED(10.0, 10.5, "use class_getMethodImplementation instead")
  1723. __IOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead")
  1724. __TVOS_DEPRECATED(9.0, 9.0, "use class_getMethodImplementation instead")
  1725. __WATCHOS_DEPRECATED(1.0, 1.0, "use class_getMethodImplementation instead")
  1726. __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead");
  1727. OBJC_EXPORT BOOL
  1728. class_respondsToMethod(Class _Nullable cls, SEL _Nonnull sel)
  1729. __OSX_DEPRECATED(10.0, 10.5, "use class_respondsToSelector instead")
  1730. __IOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead")
  1731. __TVOS_DEPRECATED(9.0, 9.0, "use class_respondsToSelector instead")
  1732. __WATCHOS_DEPRECATED(1.0, 1.0, "use class_respondsToSelector instead")
  1733. __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead");
  1734. OBJC_EXPORT void
  1735. _objc_flush_caches(Class _Nullable cls)
  1736. __OSX_DEPRECATED(10.0, 10.5, "not recommended")
  1737. __IOS_DEPRECATED(2.0, 2.0, "not recommended")
  1738. __TVOS_DEPRECATED(9.0, 9.0, "not recommended")
  1739. __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
  1740. __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended");
  1741. OBJC_EXPORT id _Nullable
  1742. object_copyFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z)
  1743. __OSX_DEPRECATED(10.0, 10.5, "use object_copy instead")
  1744. __IOS_UNAVAILABLE __TVOS_UNAVAILABLE
  1745. __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
  1746. OBJC_ARC_UNAVAILABLE;
  1747. OBJC_EXPORT id _Nullable
  1748. object_realloc(id _Nullable anObject, size_t nBytes)
  1749. OBJC2_UNAVAILABLE;
  1750. OBJC_EXPORT id _Nullable
  1751. object_reallocFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z)
  1752. OBJC2_UNAVAILABLE;
  1753. #define OBSOLETE_OBJC_GETCLASSES 1
  1754. OBJC_EXPORT void * _Nonnull
  1755. objc_getClasses(void)
  1756. OBJC2_UNAVAILABLE;
  1757. OBJC_EXPORT void
  1758. objc_addClass(Class _Nonnull myClass)
  1759. OBJC2_UNAVAILABLE;
  1760. OBJC_EXPORT void
  1761. objc_setClassHandler(int (* _Nullable )(const char * _Nonnull))
  1762. OBJC2_UNAVAILABLE;
  1763. OBJC_EXPORT void
  1764. objc_setMultithreaded(BOOL flag)
  1765. OBJC2_UNAVAILABLE;
  1766. OBJC_EXPORT id _Nullable
  1767. class_createInstanceFromZone(Class _Nullable, size_t idxIvars,
  1768. void * _Nullable z)
  1769. __OSX_DEPRECATED(10.0, 10.5, "use class_createInstance instead")
  1770. __IOS_UNAVAILABLE __TVOS_UNAVAILABLE
  1771. __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
  1772. OBJC_ARC_UNAVAILABLE;
  1773. OBJC_EXPORT void
  1774. class_addMethods(Class _Nullable, struct objc_method_list * _Nonnull)
  1775. OBJC2_UNAVAILABLE;
  1776. OBJC_EXPORT void
  1777. class_removeMethods(Class _Nullable, struct objc_method_list * _Nonnull)
  1778. OBJC2_UNAVAILABLE;
  1779. OBJC_EXPORT void
  1780. _objc_resolve_categories_for_class(Class _Nonnull cls)
  1781. OBJC2_UNAVAILABLE;
  1782. OBJC_EXPORT Class _Nonnull
  1783. class_poseAs(Class _Nonnull imposter, Class _Nonnull original)
  1784. OBJC2_UNAVAILABLE;
  1785. OBJC_EXPORT unsigned int
  1786. method_getSizeOfArguments(Method _Nonnull m)
  1787. OBJC2_UNAVAILABLE;
  1788. OBJC_EXPORT unsigned
  1789. method_getArgumentInfo(struct objc_method * _Nonnull m, int arg,
  1790. const char * _Nullable * _Nonnull type,
  1791. int * _Nonnull offset)
  1792. UNAVAILABLE_ATTRIBUTE // This function was accidentally deleted in 10.9.
  1793. OBJC2_UNAVAILABLE;
  1794. OBJC_EXPORT Class _Nullable
  1795. objc_getOrigClass(const char * _Nonnull name)
  1796. OBJC2_UNAVAILABLE;
  1797. #define OBJC_NEXT_METHOD_LIST 1
  1798. OBJC_EXPORT struct objc_method_list * _Nullable
  1799. class_nextMethodList(Class _Nullable, void * _Nullable * _Nullable)
  1800. OBJC2_UNAVAILABLE;
  1801. // usage for nextMethodList
  1802. //
  1803. // void *iterator = 0;
  1804. // struct objc_method_list *mlist;
  1805. // while ( mlist = class_nextMethodList( cls, &iterator ) )
  1806. // ;
  1807. OBJC_EXPORT id _Nullable
  1808. (* _Nonnull _alloc)(Class _Nullable, size_t)
  1809. OBJC2_UNAVAILABLE;
  1810. OBJC_EXPORT id _Nullable
  1811. (* _Nonnull _copy)(id _Nullable, size_t)
  1812. OBJC2_UNAVAILABLE;
  1813. OBJC_EXPORT id _Nullable
  1814. (* _Nonnull _realloc)(id _Nullable, size_t)
  1815. OBJC2_UNAVAILABLE;
  1816. OBJC_EXPORT id _Nullable
  1817. (* _Nonnull _dealloc)(id _Nullable)
  1818. OBJC2_UNAVAILABLE;
  1819. OBJC_EXPORT id _Nullable
  1820. (* _Nonnull _zoneAlloc)(Class _Nullable, size_t, void * _Nullable)
  1821. OBJC2_UNAVAILABLE;
  1822. OBJC_EXPORT id _Nullable
  1823. (* _Nonnull _zoneRealloc)(id _Nullable, size_t, void * _Nullable)
  1824. OBJC2_UNAVAILABLE;
  1825. OBJC_EXPORT id _Nullable
  1826. (* _Nonnull _zoneCopy)(id _Nullable, size_t, void * _Nullable)
  1827. OBJC2_UNAVAILABLE;
  1828. OBJC_EXPORT void
  1829. (* _Nonnull _error)(id _Nullable, const char * _Nonnull, va_list)
  1830. OBJC2_UNAVAILABLE;
  1831. #endif