views:

865

answers:

1

Ok so I am still kind of trying to get my bearings in Objective-C and I thought I had it down but now I cam across this method. So there are is something that confuses me that I would like a detailed explanation on if possible.

The first line: 'UITouch *touch = [touches anyObject];', now to my understanding anyObject is a hashtable? But how can you define it in this context? I don't get where its defined at, I know it belongs to NSSet, but I am confused with the scope in this context...

- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];

    point =  [[Director sharedDirector] convertCoordinate: point];
    NSLog(@"ccTouchesMoved x=%f y=%f", point.x, point.y);
    return YES;

}
+3  A: 

anyObject in this case is a method on NSSet. Why do think it is a hashtable? It simply returns an object from set - any object.

[touches anyObject]

invokes anyObject method on touches object (e.g. a method call in other languages) and it returns an object from the set.

NSTouch * touch = [touches anyObject];

touch is a pointer to one of the objects stored in touches (NSSet).

From Cocoa reference:

anyObject Returns one of the objects in the receiver, or nil if the receiver contains no objects.

- (id)anyObject

Return Value One of the objects in the receiver, or nil if the receiver contains no objects. The object returned is chosen at the receiver’s convenience—the selection is not guaranteed to be random.

stefanB
Thanks, I see it now...No idea why I thought it was a hashtable...
emalamisura