{"id":4594,"date":"2014-03-30T13:47:55","date_gmt":"2014-03-30T13:47:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/for-objective-c-pointer-to-method-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:47:55","modified_gmt":"2014-03-30T13:47:55","slug":"for-objective-c-pointer-to-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/for-objective-c-pointer-to-method-collection-of-common-programming-errors\/","title":{"rendered":"For Objective-C &hellip; Pointer to method-Collection of common programming errors"},"content":{"rendered":"<p>Yes! In Objective-C, function pointers are called selectors. If you have a method defined like this:<\/p>\n<pre><code>- (void)myFunctionWithObject:(NSObject*)obj otherObject:(NSNumber*)obj2\n{\n}\n<\/code><\/pre>\n<p>The selector is declared like this:<\/p>\n<pre><code>@selector(myFunctionWithObject:otherObject:)\n<\/code><\/pre>\n<p>To perform a selector on an object, you can use:<\/p>\n<pre><code>[object performSelector:@selector(myFunction)];\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>[object performSelector:@selector(myFunctionTakingParameter:) withObject: o];\n<\/code><\/pre>\n<p>The selector data type is particularly useful for threads and timers, where you can dispatch a thread and provide it a selector to the message you&#8217;d like it to invoke. If you need to create an array of selectors (or a dispatch table), or if you need to invoke selectors with multiple parameters, you can use the NSInvocation class. It provides a wrapper for a selector and allows you to specify actual arguments.<\/p>\n<p>You should keep in mind that Objective-C is already based on a fully dynamic method dispatch table. It sounds like maintaining function pointers using selectors will work fine for you if you just need a reference to a function, though.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes! In Objective-C, function pointers are called selectors. If you have a method defined like this: &#8211; (void)myFunctionWithObject:(NSObject*)obj otherObject:(NSNumber*)obj2 { } The selector is declared like this: @selector(myFunctionWithObject:otherObject:) To perform a selector on an object, you can use: [object performSelector:@selector(myFunction)]; or [object performSelector:@selector(myFunctionTakingParameter:) withObject: o]; The selector data type is particularly useful for threads and [&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-4594","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4594","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=4594"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4594\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}