{"id":4826,"date":"2014-03-30T15:39:08","date_gmt":"2014-03-30T15:39:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-do-i-call-methods-in-a-class-that-i-created-dynamically-with-nsclassfromstring-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:39:08","modified_gmt":"2014-03-30T15:39:08","slug":"how-do-i-call-methods-in-a-class-that-i-created-dynamically-with-nsclassfromstring-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-do-i-call-methods-in-a-class-that-i-created-dynamically-with-nsclassfromstring-collection-of-common-programming-errors\/","title":{"rendered":"How do I call methods in a class that I created dynamically with NSClassFromString?-Collection of common programming errors"},"content":{"rendered":"<p>An <strong>easier and fancier solution<\/strong> than NSInvocation \ud83d\ude42<\/p>\n<pre><code>Class mediaClass = NSClassFromString(kMediaClassName);\nif(mediaClass){\n    id mediaObject = class_createInstance(mediaClass,0);\n    objc_msgSend(mediaObject, @selector(doSomethingWith:andWith:alsoWith:), firstP, secondP,thirdP);\n}\n<\/code><\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p><code>class_createInstance(mediaClass,0);<\/code> does exactly the same as <code>[[mediaClass alloc] init];<\/code> if you need to autorelease it, just do the usual <code>[mediaObject autorelease];<\/code><\/p>\n<p><code>objc_msgSend()<\/code> does exactly the same as <code>performSelector:<\/code> method but <code>objc_msgSend()<\/code> allows you to put as many parameters as you want. So, easier than <code>NSInvocation<\/code> right? BTW, their signature are:<\/p>\n<pre><code>id class_createInstance(Class cls, size_t extraBytes)\nid objc_msgSend(id theReceiver, SEL theSelector, ...)\n<\/code><\/pre>\n<p>For more info you can refer the Objective-C Runtime Reference<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An easier and fancier solution than NSInvocation \ud83d\ude42 Class mediaClass = NSClassFromString(kMediaClassName); if(mediaClass){ id mediaObject = class_createInstance(mediaClass,0); objc_msgSend(mediaObject, @selector(doSomethingWith:andWith:alsoWith:), firstP, secondP,thirdP); } Explanation: class_createInstance(mediaClass,0); does exactly the same as [[mediaClass alloc] init]; if you need to autorelease it, just do the usual [mediaObject autorelease]; objc_msgSend() does exactly the same as performSelector: method but objc_msgSend() allows [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4826","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4826"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4826\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}