1

I have a subclass of UIScrollView, it is also the delegate.

When I have the next protocol function called:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
id  element;
NSEnumerator *setEnum =   [touches objectEnumerator];

while ((element = [setEnum nextObject]) != nil)
{
    NSLog(@"element:%@", element);    
}

return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}

The only thing that NSLog is showing is:

element:<UITouch: 0x15a9a0> phase: Ended tap count: 3 window: <UIWindow: 0x392390; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x382cd0>> view: <UIButton: 0x3a5e90; frame = (0 0; 106 138); opaque = NO; layer = <CALayer: 0x3a3e90>> location in window: {228, 126} previous location in window: {227, 126} location in view: {89.6667, 77} previous location in view: {88.3333, 77}

The problem is, it shows the content of the NSSet as one big NSString. If I ask allObjects from the objectEnumerator, I just get one object in an NSArray. Exactly the same object (the NSString) as NSLog is showing.

Could someone please tell me if I am doing something wrong, or if it is not normal that the NSSet is just giving one NSString.

Thank you!

MiiChiel
  • 201
  • 1
  • 4
  • 14
  • I got confused, it looked like a string indeed. Placing the object from the [objectEnumerator nextObject] in a UITouch variable helped! Thank you all for the quick respones – MiiChiel Feb 09 '12 at 18:26

2 Answers2

3

The NSLog() output is the result of the description method called on the object. NSLog() output is designed to be human readable for debugging only.

<UITouch: 0x15a9a0> indicates the the object is a UITouch at address 0x15a9a0. The rest of the description just displays some interesting values of the UITouch.

Try:

NSLog(@"tap count:%d", element.count);

etc. and you will se that it is not just a string.

As an aside there is no need for an explicit NSEnumerator, fast enumeration ca be used: you can simplify the code to:

for (UITouch *element in touches)
{
    NSLog(@"element:%@", element);    
}
zaph
  • 111,848
  • 21
  • 189
  • 228
  • I understand, but should it not be that when I use NSLog for just one element in the 'while' loop, that I would get only one of all objects been showing in the console? – MiiChiel Feb 09 '12 at 18:16
  • You are getting a description of one object, the `UITouch` and as a b onus some of it's state. – zaph Feb 09 '12 at 18:25
  • Great answer man, I kept using `UITouch *touch = [[touches objectEnumerator] nextObject]` and I hate that syntax. – bobobobo Dec 08 '12 at 01:21
  • When you say "fast enumeration", do you mean it performs better? – bobobobo Dec 08 '12 at 01:21
  • @bobobobo Fast enumeration means the form of enumeration using a for loop with the keyword "**in**". `for (id obj **in** collection) – zaph Dec 08 '12 at 23:19
1

The NSSet touches is a set of UITouch instances, not NSStrings. When you send it to NSLog, it calls the "description" method of the UITouch instance which does return an NSString.

picciano
  • 22,341
  • 9
  • 69
  • 82