[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

I don't really get the SEL syntax

I read Apple's explanation of selectors, yet feel no closer to understanding it.  It seems like it is a delegate function (C# lingo) that can be attached to an object.  But please explain this code in English, not with links to articles.


- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
  return [super methodSignatureForSelector:selector];
}

Thanks.
0
newbieweb
Asked:
newbieweb
  • 2
  • 2
  • 2
4 Solutions
 
fridomCommented:
you can use it by just declaring an
NSMethodSignature and there  is probably a thing for calling it then.

NSMethodSignature *sig = [[self class] methodSignatureForSelector: @selector(size:)];

Then you can "invoke this" e.g with:
NSInvocation *callItThough;

callItThrough =  [NSInvocation invocationWithMethodSignature:signature];

you have to set the target object
let's assume we'd have
NSArray *arr;

[callItThrough  setTarget: arr]

invoke the Invocation:
[callItThough invoke];

get the result
long size;

[calllItThrough getReturnValue: &size];

http://stackoverflow.com/questions/1055666/using-nsmethodsignature-on-iphone-with-obj-c-2-0-properties

It's a very indirect way of constructing calls and/or delegate calls. I'd  probably go for
perfomWith: instead but you may have you reasons to make another indirection....
0
 
fridomCommented:
Sorry, I missed to set the selector properly.
before setTarget:
you've to add:

[callItThrough setSelector: sel];


So if you like you can see it that way. You are fetching some executable code.
You set the proper selector
you set the proper object on which the selector should work
you invoke the prefetched code
you extract the return value if there's one.
0
 
Hamidreza VakilianSenior iOS DeveloperCommented:
If you are looking for how is the usage of methodSignatureForSelector method, the example provided by fridom seems fine.

But to understand what methodSignatureForSelector does:
When we want to execute a method on a particular object we use NSInvocation. Actually NSInvocation asks the target object if it has declared the method and If true, invokes the object to execute the method.
NSInvocation needs to know whether the object has declared the method or not, so it should utilize some search algorithm to achieve this: A method signature is fingerprint of a method which depends on its return value, number of arguments it takes and their types. So the process of invoking a method at runtime will be something like this:

-We initiate a NSInovation by introducing our selector and NSMethodSignature
-We set the target object for the NSInvocation
-Each object has the list of method signatures it supports
-NSInovation calls the target object and looks in its method signature list to see whether the method exists or not
-If a match found; our NSInvocation calls it
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
newbiewebAuthor Commented:
This is really overwhelming.  Is there a "Selectors for Dummy's"?

You lost me here:
NSMethodSignature *sig = [[self class] methodSignatureForSelector: @selector(size:)];

What does @selector(size:) do??

Where is "methodSignatureForSelector:" defined?  By Apple's framework?

How about "self class"?  I don't know that I have seen this before.  Is class a class method?

Confused...
0
 
Hamidreza VakilianSenior iOS DeveloperCommented:
This is really overwhelming.  Is there a "Selectors for Dummy's"?
Selectors are not that complicated as much as you are confused. I don't know any selectors for dummies tutorial or something.

You lost me here:
NSMethodSignature *sig = [[self class] methodSignatureForSelector: @selector(size:)];
methodSignatureForSelector is a method of class NSObject though  [[self class] methodSignatureForSelector: @selector(size:)] will cause compile error since [self class] returns an object of class Class, and it does not have a methodSignatureForSelector method. Instead you should use this syntax:
NSMethodSignature *sig = [self methodSignatureForSelector: @selector(size:)];

What does @selector(size:) do??
As I said later methodSignatureForSelector returns a NSMethodSignature object which is somehow a fingerprint or an identification for a method. Therefore methodSignatureForSelector needs to know which method you want a methodSignatureForSelector for. Tough you will specify your desired method by using a selector. Either these two snippets do the same thing:
NSMethodSignature *sig = [self methodSignatureForSelector: @selector(size:)];

Open in new window

SEL mySelector = @selector(size:);
NSMethodSignature *sig = [self methodSignatureForSelector: mySelector];

Open in new window


Where is "methodSignatureForSelector:" defined?  By Apple's framework?
It is defined in NSObject class.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/methodSignatureForSelector:

How about "self class"?  I don't know that I have seen this before.  Is class a class method?
[self class]: Here class is a static method which returns the Objective-C class objc_class associated with object self. You rarely work with this method when developing iDevice applications but for more information take a look at this link: (This may really help you understand the selectors and messaging in Objective-C)
http://reverse.put.as/wp-content/uploads/2011/06/objective-c-internals.pdf
0
 
newbiewebAuthor Commented:
Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now