Objective c: All selectors of instance during runtime [duplicate] - iphone

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
List selectors for obj-c object
Does anybody know how to get all selectors that a instance respond to during runtime in objective C?

As answered here, #import < objc/runtime.h > and use class_copyMethodList().

In general, this is not possible. "The selectors an instance responds to" may be an infinite set. For example, it is possible to implement a class that is sent Roman numerals as messages and returns the corresponding integer value. If you want to know the precise set of instance methods implemented by a class at a given time (which is a different question), you can just use the Objective-C runtime functions to get a class's instance method list and walk up the class tree to find the ones it inherits from superclasses. Again, though, these are two totally different things. A class might have a method for a message that it chooses not to respond to and it might respond to messages for which it does not have a directly corresponding method.

dapptrace (Dtrace) is your friend.
on the man page (man dapptrace):
dapptrace prints details on user and
library function calls
dapptrace is written for the Dtrace scripting language (D). This means you can adjust dapptrace or pull ideas from it's script file to do many things. For instance:
wait for myFunctionWhichCreatesSpecialObject to be called. Store the object address that it returns (the special object). Print out any selectors invoked on that object.
You can also invoke dtrace directly to write simple single-line spells. I'll let you go search for those.

During runtime you would use
the class method "+ (BOOL)instancesRespondToSelector:(SEL)aSelector"
provided you know the selectors you want to check on.

Related

variable number of arguements RPC

I would like to know how to pass a variable number of arguments and types to an RPC function in UNITY?
I saw some similar questions asked in here but no straight answers on how to do it.
Thanks
RPC function is deprecated since unity 5.X cause a new network system was released. If you have an already made game and can't migrate from old network API you should take a look on Legacy Network RPC's documentation, there is good covering information about how to pass params into RPCs.
Basicaly what you need is an array of objects like:
networkView.RPC("MyMethod",RPCMode.All, arg1[], arg2, arg...);
where args must match the end point method signature MyMethod(arg1[], arg2, arg...).
If you need declare methods with variable types and number of arguments you'll have a lot of ways to proceed (from using specific objects as arguments through convert or serialize an object as byte arrays or something that you would deserializes after receive ). You can improve your research results on this approaches by searching directly on "C#" instead of "Unity" or "RPC" targets...

preventing init and forcing initWithOption

I know you can have #private member variables but is there someway to prevent code from calling the default init: method of your class and forcing code that creates and uses your objects to only use another initializer such as initWithOptions:
I have had a flick through some Objective-C books and didn't immediately see anything.
I have seen the stack overflow topic suggesting to throw exception, or assert/abort/whatever in how to prevent usage of other init methods other than my custom method in objective-c
and the use of the depreciated keyword
How do I flag a function as being deprecated in an iPhone Objective C header file?
Both of these seem somewhat less than elegant solutions, there is really no language orientated elegant way to say doesNotImplmentSelector in Objective-C...?
I come from a C++ background and just kind of expected something like the ability to hide the default constructor...
R.
I think the standard thing to do is to have an implementation of init which calls initWithOptions: with a default set of options.

Should I class contain another object or subclass another class? Has or Is? [duplicate]

This question already has answers here:
Prefer composition over inheritance?
(35 answers)
Closed 9 years ago.
I am making a program for studying chess openings, traps and maybe other related things. It has a class MoveSequence, which basically is an ordered list of objects from a class ChessPosition. I also have a class ChessOpening which has a sequence of moves and a name, an ECO-code (chess opening classification system) and probably some more stuff.
Should I implement ChessOpening as a subclass of MoveSequence, or should it just contain a MoveSequence object? The same question would apply for a class ChessTrap.
Don't think it matter so much what I choose in this simple problem. But I want to learn this stuff, so I was wondering if there is some principles, or rules of thumb, one should consider when making decision like this.
My 0.02$ worth:
If a possible subclass does not share basically all of the properties and methods of its superclass, I start to wonder.
Overriding methods to change behavior, or adding new properties and methods is normal. But when you hit that awkwardness when some of the superclass properties/methods are not applicable to the subclass, it gets creepy. (What do you do then, just ignore the invalid/inappropriate parts? Hope no one ever calls them? Stub them out and raise an error if they are used?)
If xxx is not really a yyy, maybe xxx and yyy are both zzz's. (Or if they just share common behaviors, maybe create a module to include, which is a standard Ruby/Rails practice.)
A real Computer Scientist may have a more concise and definitive answer...

Multiple parts of methods in Objective C

I am learning Objective C and noticed this funky quirk while reading up on methods.
Like Java and C++, Obj.C can take in multiple parameters, which is fine, however it states that objective C methods can have multiple names which does not seem to register to well with me.
For instance:
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
In the above example, there are two parameters, bombLocation (return type CGPoint) and damaged (return type BOOL) and alongside the method name seems to be split as shipsatpoint:withDamage
I don't understand what's up with this...
What does it signify when it states that a method can have multiple names?
Is this applicable only for methods that require multiple parameters? Alternately, say I want to name my method with a single name but provide it with multiple parameters, is that possible or I must provide it with multiple names each of which correspond to a parameter? If yes, then why?
Thanks for jumping in with my confusion!!! :)
The reason is to make it easier to understand.
With your example, the method would be something like this in C++:
int shipsAtPointWithDamage (CGPoint bomb, BOOL damage) //I don't really know C++
OK, so the first parameter is the ship's point, and the damage is the second. It's easy enough to figure out, but that's the thing, you have to FIGURE it out, you have to look at the method to try and figure out what each thing is.
In Objective-C you have
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
Each parameter is clearly defined, the first is the ship's point, the second is damage. It reads like a sentence, whereas with C++ (and almost every other language) it doesn't.
If you want a method to have multiple parameters in Obj-C you have to write it this way:
-(returnType)paraOne:(type*)name paraTwo:(type*)name
It's something that just takes getting used to, every language is different. Once you get used to the way Objective-C does things, you'll think it's absolutely fantastic.
EDIT: and as filipe pointed out, because the method as multiple parameters it doesn't mean it has multiple names, in the example I gave above, the method name would be paraOne:paraTwo, NOT paraOne:
Objective-C uses a system of message passing based on selectors. This is not quite the same thing as method calling. When you see code like this:
[world shipsAtPoint:point withDamage:YES];
That is converted into the following C call (in the most common case):
objc_msgSend(world, #selector(shipsAtPoint:withDamage:), point, YES);
The #selector() construct returns a unique identifier. The exact format of that identifier is an internal implementation detail.
objc_msgSend includes quite a lot of logic in it's few dozen bytes of assembler. But in simplest case, it looks up the class for world, walks through a table of selectors until it finds the one that matches shipsAtPoint:withDamage: and then grabs the function pointer at that slot. It then jumps to that function pointer, leaving the rest of the parameters alone (in registers or on the stack as appropriate for the processor). The function at that location is your method, and it knows the order and types of its parameters based on your declaration.
What's important in all this for you is that the selector is shipsAtPoint:withDamage:. This is generally the one-and-only name of the method. There are not "multiple names" as you suggest. (Usually.... the Objective-C runtime is very powerful and it's possible to point multiple selectors to the same implementation.)
As Joe points out, a selector can be in the form foo::. This would represent a method that took two parameters and would be called like [world foo:point :YES]. You should never do this. It's incredibly confusing to read. But it's legal.
Here is the best explanation i've ever seen. It includes comparisons with C++/C as well as lots of other good info.
I think you are confused. A method cannot have multiple names, but the argument may be named differently in the header then they are in the implementation.
The name of that method is shipsAtPoint:withDamage:. This is also known as a selector.
This method returns an instance of NSArray, and accepts a CGPoint as the first argument, and a BOOL as the second argument.
The names of the arguments may differ, however. This is totally valid:
// .h file
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
// .m file
-(NSArray *)shipsAtPoint:(CGPoint)loc withDamage:(BOOL)dmg {
// ...
}
Lastly, ObjC is mainly some nice syntax sugar. You should know that any method invocation really just boils down to some C that looks more or less like this:
objc_msgSend(receiverObj, #selector(shipsAtPoint:withDamage:), point, damage);
So at the end of the day, you have a receiver, a selector, and your arguments. But the ObjC syntax is much nicer than that.
It is possible provide a method without labeled parameters but it is obviously discouraged.
-(void)badmethod:(id)obj1:(id)obj2:(id)obj3
{
}
//...
//Usage
[self badmethod:nil :nil :nil];
SEL sel = #selector(badmethod:::);

Confusion about MKOverlayView

I've been working with layers for MapKit on the iPhone, and one library that I came across was this one: https://github.com/mtigas/iOS-MapLayerDemo/. It's very helpful, and seems to work fine. However, I'm trying to go through and understand a bit how it works, but I'm having some trouble.
On this page, for example: https://github.com/mtigas/iOS-MapLayerDemo/blob/master/MapLayerDemo/Classes/CustomOverlayView.m,
at the top, there are 4 custom functions defined. I assume these functions are adding on to the normal features of MKOverlayView? The thing is, I can't find where any of these new functions are actually called from, and thus I'm having some trouble understanding how this page works. It doesn't seem to be from any of the other files within the project.
I appreciate any help, thanks.
After some extended discussion with you in comments:
The override-able functions of MKOverlayView, such as canDrawMapRect cannot easily be traced back to their calling code because that code is obfuscated somewhere in the MapKit.framework.
Instead, the typical approach is to re-read their documentation until you get a mental picture of what the framework is using the function for. (There is such a thing as decompiling binaries, although that is generally frowned upon and I do not recommend it.)
canDrawMapRect documentation: http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKOverlayView_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009715-CH1-SW10
After reading their documentation, I inferred this: Somewhere in the MapKit.framework, canDrawMapRect is being called prior to actually drawing the view. If you didn't override that function in your subclass, it calls the super-class's default implementation, which always returns YES and then calls drawMapRect: (Which MUST be overridden if you are subclassing MKOverlayView, or else nothing will draw!)
The class you linked above potentially returns NO. In that particular case, it appears the code in MapKit.framework skips calling drawMapRect: and nothing is displayed (or refreshed).
So, long story short: for this case, you have to play code-detective and hope the documentation is written clearly enough to figure it out without being able to see all of the code.
Edit: Just to further clarify - It appears MKOverlayView must be subclassed to actually generate something visible.
My original answer before getting to your underlying question --
Short answer: Those are private functions for use within that class.
Long answer: Functions declared in an empty-name category at the top
of implementation files are visible only to the class the category is
extending. Thus, those functions can only be called within that
class's implementation. (C++ equivalent would just be declaring the
functions private)
3 of those 4 functions are called within that same .m file. Without
digging around, I'm guessing they wrote the first function and then
later decided to not use it.