{"id":4886,"date":"2014-03-30T16:26:02","date_gmt":"2014-03-30T16:26:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/arguments-in-selector-collection-of-common-programming-errors\/"},"modified":"2014-03-30T16:26:02","modified_gmt":"2014-03-30T16:26:02","slug":"arguments-in-selector-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/arguments-in-selector-collection-of-common-programming-errors\/","title":{"rendered":"Arguments in @selector-Collection of common programming errors"},"content":{"rendered":"<p>You could use the <code>NSTimer<\/code> method:<\/p>\n<pre><code>+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds\n                                 invocation:(NSInvocation *)invocation\n                                    repeats:(BOOL)repeats;\n<\/code><\/pre>\n<p>Instead, since an <code>NSInvocation<\/code> object will allow you to pass arguments; an <code>NSInvocation<\/code> object is, as the docs define it:<\/p>\n<blockquote>\n<p>an Objective-C message rendered static, that is, it is an action turned into an object.<\/p>\n<\/blockquote>\n<p>Whilst creating an <code>NSTimer<\/code> object using a selector requires the format of the method being:<\/p>\n<pre><code>- (void)timerFireMethod:(NSTimer*)theTimer\n<\/code><\/pre>\n<p>An <code>NSInvocation<\/code> allows you to set the target, the selector, and the arguments that you pass in:<\/p>\n<pre><code>SEL selector = @selector(myMethod:setValue2:);\n\nNSMethodSignature *signature = [MyObject instanceMethodSignatureForSelector:selector];\nNSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];\n[invocation setSelector:selector];\n\nNSString *str1 = @\"someString\";\nNSString *str2 = @\"someOtherString\";\n\n\/\/The invocation object must retain its arguments\n[str1 retain];\n[str2 retain];\n\n\/\/Set the arguments\n[invocation setTarget:targetInstance];\n[invocation setArgument:&amp;str1 atIndex:2];\n[invocation setArgument:&amp;str2 atIndex:3];\n\n[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:invocation repeats:YES];\n<\/code><\/pre>\n<p>Where <code>MyObject<\/code> is the class that <code>myMethod:setValue2:<\/code> is declared and implemented on &#8211; <code>instanceMethodSignatureForSelector:<\/code> is a convenience function declared on <code>NSObject<\/code> which returns an <code>NSMethodSignature<\/code> object for you, to be passed to <code>NSInvocation<\/code>.<\/p>\n<p>Also, to note, with <code>setArgument:atIndex:<\/code>, the indices for arguments to be passed to the method set as the selector start at index 2. From the docs:<\/p>\n<blockquote>\n<p>Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; you should set these values directly with the setTarget: and setSelector: methods. Use indices 2 and greater for the arguments normally passed in a message.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>You could use the NSTimer method: + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats; Instead, since an NSInvocation object will allow you to pass arguments; an NSInvocation object is, as the docs define it: an Objective-C message rendered static, that is, it is an action turned into an object. Whilst creating an NSTimer object using a selector [&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-4886","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4886","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=4886"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4886\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}