I came across this code in a project, where it is using NSInvocation. I want to know what it is supposed to do, and why would we ever need that. Simple explanations would be appreciated. I am posting the code.  // Public interface @interface CCDelegateSplitter : NSObject  - (void) addDelegate: (id) delegate; - (void) addDelegates: (NSArray*) delegates;  @end  // Private interface @interface CCDelegateSplitter () @property(strong) NSMutableSet *delegates; @end  @implementation CCDelegateSplitter  - (id) init {     self = [super init];     _delegates = [NSMutableSet set];     return self; }  - (void) addDelegate: (id) delegate {     [_delegates addObject:delegate]; }  - (void) addDelegates: (NSArray*) delegates {     [_delegates addObjectsFromArray:delegates]; }  - (void) forwardInvocation: (NSInvocation*) invocation {     for (id delegate in _delegates) {         [invocation invokeWithTarget:delegate];     } }  - (NSMethodSignature*) methodSignatureForSelector: (SEL) selector {     NSMethodSignature *our = [super methodSignatureForSelector:selector];     NSMethodSignature *delegated = [(NSObject *)[_delegates anyObject] methodSignatureForSelector:selector];     return our ? our : delegated; }  - (BOOL) respondsToSelector: (SEL) selector {     return [[_delegates anyObject] respondsToSelector:selector]; }  @end
120612
How exactly does NSInvocation work? Is there a good introduction?  I’m specifically having issues understanding how the following code (from Cocoa Programming for Mac OS X, 3rd Edition) works, but then also be able to apply the concepts independently of the tutorial sample. The code:  - (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index {     NSLog(@"adding %@ to %@", p, employees);     // Add inverse of this operation to undo stack     NSUndoManager *undo = [self undoManager];     [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];     if (![undo isUndoing])         [undo setActionName:@"Insert Person"];      // Finally, add person to the array     [employees insertObject:p atIndex:index]; }  - (void)removeObjectFromEmployeesAtIndex:(int)index {     Person *p = [employees objectAtIndex:index];     NSLog(@"removing %@ from %@", p, employees);     // Add inverse of this operation to undo stack     NSUndoManager *undo = [self undoManager];     [[undo prepareWithInvocationTarget:self] insertObject:p                                        inEmployeesAtIndex:index];     if (![undo isUndoing])         [undo setActionName:@"Delete Person"];      // Finally, remove person from array     [employees removeObjectAtIndex:index]; }   I get what it’s trying to do. (BTW, employees is an NSArray of a custom Person class.)  Being a .NET guy, I try to associate unfamiliar Obj-C and Cocoa concepts to roughly analogous .NET concepts. Is this similar to .NET’s delegate concept, but untyped?   This isn’t 100% clear from the book, so I’m looking for something supplemental from real Cocoa/Obj-C experts, again with the goal that I understand the fundamental concept beneath the simple(-ish) example. I'm really looking to be able to independently apply the knowledge -- up until chapter 9, I was having no difficulty doing that. But now ...    Thanks in advance!
13476
Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates:   Predicate Action Func
54034
I've been studying opengl es and an example I saw was using a "Pool" class to keep track of touch and keyboard events.  Could someone please explain how and why a pool class is needed. From what I was reading it had something to do with garbage collection and limiting the number of input classes that are made.   This all seems a little abstract to me, so if someone could please explain what is going on, I'd appreciate it, I'll pasted some code here:      public Pool(PoolObjectFactory &lt; T &gt; factory, int maxSize) {                     this.factory = factory;                  this.maxSize = maxSize;                  this.freeObjects = new ArrayList &lt; T &gt; (maxSize);          }                 public T newObject() {                 T object = null ;                 if (freeObjects.isEmpty())                         object = factory.createObject();                 else                          object = freeObjects.remove(freeObjects.size() - 1);                     return object;          }       public void free(T object) {                  if (freeObjects.size() &lt; maxSize)                          freeObjects.add(object);          }      PoolObjectFactory &lt;TouchEvent&gt; factory = new PoolObjectFactory &lt;TouchEvent&gt; () {           @Override          public TouchEvent createObject() {                   return new TouchEvent();          }       Pool &lt;TouchEvent&gt; touchEventPool = new Pool &lt;TouchEvent&gt; (factory, 50);      TouchEvent touchEvent = touchEventPool.newObject();      . . . do something here . . .      touchEventPool.free(touchEvent);   Thanks!
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
38358
I've been learning C#, and I'm trying to understand lambdas.  In this sample below, it prints out 10 ten times.  class Program {     delegate void Action();     static void Main(string[] args)     {         List&lt;Action&gt; actions = new List&lt;Action&gt;();          for (int i = 0; i &lt; 10; ++i )             actions.Add(()=&gt;Console.WriteLine(i));          foreach (Action a in actions)             a();     } }   Obviously, the generated class behind the lambda is storing a reference or pointer to the int i variable, and is assigning a new value to the same reference every time the loop iterates.  Is there a way to force the lamda to grab a copy instead, like the C++0x syntax  [&amp;](){ ... } // Capture by reference   vs.  [=](){ ... } // Capture copies
109596
As a Java developer who is reading Apple's Objective-C 2.0 documentation: I wonder what "sending a message to nil" means - let alone how it is actually useful.  Taking an excerpt from the documentation:     There are several patterns in Cocoa   that take advantage of this fact. The   value returned from a message to nil   may also be valid:         If the method returns an object, any pointer type, any integer scalar   of size less than or equal to   sizeof(void*), a float, a double, a   long double, or a long long, then a   message sent to nil returns 0.   If the method returns a struct, as defined by the Mac OS X ABI Function   Call Guide to be returned in   registers, then a message sent to nil   returns 0.0 for every field in the   data structure. Other struct data   types will not be filled with zeros.   If the method returns anything other than the aforementioned value   types the return value of a message   sent to nil is undefined.      Has Java rendered my brain incapable of grokking the explanation above?  Or is there something that I am missing that would make this as clear as glass?  I do get the idea of messages/receivers in Objective-C, I am simply confused about a receiver that happens to be nil.
51671
When working with views and view controllers in an iPhone app, can anyone explain the difference between loadView and viewDidLoad?    My personal context, is that I build all my views from code, I do not and will not use Interface Builder, should that make any difference.  I've found that often when I add init code to loadView, I end up with an infinite stack trace, so I typically do all my child-view building in viewDidLoad...but it's really unclear to me when each gets executed, and what is the more appropriate place to put init code.  What would be perfect, is a simple diagram of the initialization calls.  Thanks!
80489
There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other.  First type:  List&lt;string&gt; someList = &lt;some way to init&gt; foreach(string s in someList) {    &lt;process the string&gt; }   Other Way:  List&lt;string&gt; someList = &lt;some way to init&gt; someList.ForEach(delegate(string s) {     &lt;process the string&gt; });   I suppose off the top of my head, that instead of the anonymous delegate I use above, you'd have a reusable delegate you could specify...
92031
Whats this syntax useful for :       function(String... args)   Is this same as writing       function(String[] args)    with difference only while invoking this method or is there any other feature involved with it ?
80428
I have a class like this:  public class myClass {   public List&lt;myOtherClass&gt; anewlist = new List&lt;myOtherClass&gt;;    public void addToList(myOtherClass tmp)   {     anewList.Add(tmp);   }  }   So I call "addToList" a hundred times, each adding a unique item to the list.  I've tested my items to show that before I run the "addToList" method, they are unique.  I even put a line in to test "tmp" to make sure it was what I was expecting.  However, when I do this (lets say myClass object is called tmpClass):  int i = tmpClass.anewList.Count(); for (int j = 0; j&lt;i; j++) {    //write out each member of the list based on index j... }   I get the same exact item, and it's the last one that was written into my list.  It's as if when I add, I'm overwriting the entire list with the last item I've added.    Help?  This makes no sense. I've also tried List.Insert, where I'm always inserting at the end or at index 0.  Still no dice.  Yes, I'm doubly source my indexing is correct and when I do my test I'm indexing through each of the elements.  :)  UPDATE: Okay, I tried this and still had the same problem:  foreach(myOtherClass tmpC in tmpClass.anewList) {         Console.WriteLine(tmpC.theStringInMyClass.ToString()); }   and still for each of the 100 items, I got the same string output... I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.  -Adeena    Okay, I tried this and still had the same problem:  foreach(myOtherClass tmpC in tmpClass.anewList) {     Console.WriteLine(tmpC.theStringInMyClass.ToString()); }   and still for each of the 100 items, I got the same string output...  I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.  -Adeena
83735
private void activateRecords(long[] stuff) {     ...     api.activateRecords(Arrays.asList(specIdsToActivate));   }   Shouldn't this call to Arrays.asList return a list of Longs?  Instead it is returning a List&lt;long[]&gt;  public static &lt;T&gt; List&lt;T&gt; asList(T... a)   The method signature is consistent with the results, the varargs throws the entire array into the list.  It's the same as new ArrayList(); list.add(myArray)  And yes, I know it's meant to be used like this: Arrays.asList(T t1, T t2, T t3)  I guess what I'm getting at, is instead of the varargs form, why can't I just have my old asList method (at least I think this is how it used to work) that would take the contents and put them individually into a list?  Any other clean way of doing this?
8886
I've seen this done in Borland's  environment, but I'm not sure how to go about it for a C# application I'm working on. Are there best practices or gotchas to look out for?
94364
I'm trying to understand what single and multiple dispatch are, exactly.  I just read this:   And from that definition is seems to me that C# and VB.Net are multiple-dispatch, even though the choice of which overload to call is made at compile-time.  Am I correct here, or am I missing something? Thanks!
9770
I'm sure you all know the behaviour I mean - code such as:  Thread thread = new Thread(); int activeCount = thread.activeCount();   provokes a compiler warning.  Why isn't it an error?  EDIT:  To be clear: question has nothing to do with Threads.  I realise Thread examples are often given when discussing this because of the potential to really mess things up with them.  But really the problem is that such usage is always nonsense and you can't (competently) write such a call and mean it.  Any example of this type of method call would be barmy.  Here's another:  String hello = "hello"; String number123AsString = hello.valueOf(123);   Which makes it look as if each String instance comes with a "String valueOf(int i)" method.
28265
I need to hide (make private) the -init method of my class in Objective-C.  How can I do that?
24911
Just a quick question from an Objective-C beginner: what does the asterisk in the parenthesis mean in the following sample code? Is it a pointer? Thanks in advance.  #import &lt;Foundation/Foundation.h&gt;  @interface Tire : NSObject @end @implementation Tire  - (NSString *) description {    return (@"I am a tire."); }  @end
69913
In Objective-C, what's the difference between declaring a variable id versus declaring it NSObject *?