Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-18-2011, 11:32 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default Invalid operands to binary /

I'm using this code for the moving, scaling and rotating an UIImageView.

Code:
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	CGPoint currentTouch1;
    	CGPoint currentTouch2;
    	NSArray *allTouches = [touches allObjects];
    	UITouch* t;
    	float scale,rotation;
    	
    	if([[event allTouches] count]==1){
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]))
    		{ 
    			touch2=[t locationInView:nil];
    			Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
    			touch1=touch2;
    		}
    	}
    	else if([[event allTouches] count]==2)
    	{
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		currentTouch1=[t locationInView:nil];
    		
    		t=[[[event allTouches] allObjects] objectAtIndex:1];
    		currentTouch2=[t locationInView:nil];
    		
    		
    		scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
    		rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
    		if(isnan(scale)){
    			scale=1.0f;
    		}
    		NSLog(@"rotation %f",rotation);
    		
    		NSLog(@"scale %f",scale);
    		
    		if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]) &&
    			CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:1] locationInView:self.view]))
    		{
    			
    			Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
    			Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
    		}
    		else // In case of scaling or rotating the background imageView
    		{
    			imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
    			imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
    		}
    		
    		touch1=currentTouch1;
    		touch2=currentTouch2;
    	}
    }
    
    -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
    {
    	return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
    }

However I get one error the whole time, Invalid operands to binary /.
I get this error on the next line:

Code:
		scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
Thanks in advanced for your help!
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 12:18 PM   #2 (permalink)
Registered Member
 
simplyDusty's Avatar
 
Join Date: Jan 2011
Posts: 158
simplyDusty is on a distinguished road
Default

I'm not sure if this is causing your issue or not, but you should be making sure you're not dividing by zero.
simplyDusty is offline   Reply With Quote
Old 04-18-2011, 12:25 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 324
mariano_donati is on a distinguished road
Default

or maybe your operands are too big?
mariano_donati is offline   Reply With Quote
Old 04-18-2011, 12:42 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

How can I check if it's zero or if it's too big? Because I can't run it, because of the error. Thanks for replying!
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 12:44 PM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Code:
double op1 = [self distance:currentTouch1 toPoint:currentTouch2];
double op2 = [self distance:touch1 toPoint:touch2];

NSLog(@"op1:%f op2:%f",op1,op2);

double ris = op1/op2;

NSLog(@"ris:%f",ris);
__________________
dany_dev is offline   Reply With Quote
Old 04-18-2011, 12:46 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

I've replaced this code with the
Code:
		scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
But it still gives an error, this time 'incompatible types of initialization'. Thanks in advanced!
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 12:50 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

is the method distance:toPoint: declared in the class header (or just declared before the method you use it in the .m file)? If the compiler haven't seen the method, it will assume a return type of (id) and probably will complain if you try to do (id) / (id)
nobre84 is offline   Reply With Quote
Old 04-18-2011, 12:52 PM   #8 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

where you declared touch1? and how?
__________________
dany_dev is offline   Reply With Quote
Old 04-18-2011, 12:53 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

I'm not really sure if it's declared, but if it's not declared it would give an error about "undeclared...". I'm sure that it's not declared in the class header, so if it's declared, it's in the piece of code I've written.
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 12:54 PM   #10 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by Sketchiie View Post
I'm not really sure if it's declared, but if it's not declared it would give an error about "undeclared...". I'm sure that it's not declared in the class header, so if it's declared, it's in the piece of code I've written.
loool

search it....and post it...
__________________
dany_dev is offline   Reply With Quote
Old 04-18-2011, 12:55 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

Can you help me with this error? I guess it's the part with:
Code:
    		t=[[[event allTouches] allObjects] objectAtIndex:0];
    		currentTouch1=[t locationInView:nil];
    		
    		t=[[[event allTouches] allObjects] objectAtIndex:1];
    		currentTouch2=[t locationInView:nil];
Code:
    		touch1=currentTouch1;
    		touch2=currentTouch2;
I don't think the toPoint and distance are declared.

Last edited by Sketchiie; 04-18-2011 at 01:04 PM.
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 02:08 PM   #12 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Seems like you copied this over the internet and have no idea how it works, right ? :P

Move this method :
Code:
   -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
    {
    	return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
    }
To a place before this one :
Code:
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
The compiler does not know it will return you a double, so it wont allow you to divide.
nobre84 is offline   Reply With Quote
Old 04-18-2011, 02:52 PM   #13 (permalink)
Registered Member
 
simplyDusty's Avatar
 
Join Date: Jan 2011
Posts: 158
simplyDusty is on a distinguished road
Default

Seems like you should also be getting the "may not respond to..." warning. Usually best to really understand warnings before ignoring them.
simplyDusty is offline   Reply With Quote
Old 04-18-2011, 03:43 PM   #14 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

Quote:
Originally Posted by nobre84 View Post
Seems like you copied this over the internet and have no idea how it works, right ? :P

Move this method :
Code:
   -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
    {
    	return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
    }
To a place before this one :
Code:
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
The compiler does not know it will return you a double, so it wont allow you to divide.
So it has to read it before it's possible to divide them. Thank you for this answer, however when I try to rotate or scale the image (using multitouch, what the code is for) the whole application crashes.

Code:
2011-04-18 22:40:04.647 [6511:707] rotation 0.274011
2011-04-18 22:40:04.657 [6511:707] scale inf
2011-04-18 22:40:04.726 [6511:707] *** Terminating app due to uncaught exception
I'm just a rookie, so I try to teach myself some coding with the help of the internet .
Sketchiie is offline   Reply With Quote
Old 04-18-2011, 07:43 PM   #15 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
I'm not really sure if it's declared, but if it's not declared it would give an error about "undeclared...". I'm sure that it's not declared in the class header, so if it's declared, it's in the piece of code I've written.
The C language compiler is a "single pass" compiler. It compiles from top to bottom. If a function/method is declared at the bottom of a file, but you try to use it above, the compiler does not know how the method function is declared.

The compiler will make an educated guess as to how it's declared (it will assume that the result is type "id", which is a universal object pointer.)

In order to fix this problem, add the declaration of the method to the .h file for your class. Add the following line to your .h file, after the { and } that contain your local variables, and after any @property directives:


Code:
    -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2;
The declaration in the header needs a semicolon at the end.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-18-2011, 07:47 PM   #16 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
I'm using this code for the moving, scaling and rotating an UIImageView.

Code:
    -(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
    {
    	return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
    }


Thanks in advanced for your help!
Even after you fix the problems that are preventing you from compiling, the distance function is wrong.

The equation for distance is sqrt(deltaX^2 + deltaY^2) (where "^2" means squared. Your method should be changed like this:



Code:
-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
  double deltaX, deltaY;
  deltaX = point1.x - point2.x;
  deltaY = point1.y - point2.y;
  return sqrt(deltaX * deltaX + deltaY * deltaY);
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-18-2011, 07:57 PM   #17 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
So it has to read it before it's possible to divide them. Thank you for this answer, however when I try to rotate or scale the image (using multitouch, what the code is for) the whole application crashes.

Code:
2011-04-18 22:40:04.647 [6511:707] rotation 0.274011
2011-04-18 22:40:04.657 [6511:707] scale inf
2011-04-18 22:40:04.726 [6511:707] *** Terminating app due to uncaught exception
I'm just a rookie, so I try to teach myself some coding with the help of the internet .

You'll get infinite scale if you try to divide by zero.

Change your code to something like this:

Code:
double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
double distance2 = [self distance:touch1 toPoint:touch2];

if (distance2 == 0)
{
   //handle the case where distance is zero
}
else
  scale =distance1 / distance2;
It's not my app, so I don't know what you want to do if distance2 is zero. I do know that you don't want to divide by zero, because that will crash.

I gather you're writing an app that changes the size of something depending on finger drags of some sort. You need logic that detects a starting distance of zero and rejects it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-19-2011, 06:41 AM   #18 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

Thank you so much for your answer, however I changed everything and it still crashes on the same time as before. This time with the next crash:
Code:
2011-04-19 13:40:38.341 [6755:707] rotation 1.104756
2011-04-19 13:40:38.347 [6755:707] scale 0.000000
2011-04-19 13:40:38.358 [6755:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Sketchiie is offline   Reply With Quote
Old 04-19-2011, 07:06 AM   #19 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
Thank you so much for your answer, however I changed everything and it still crashes on the same time as before. This time with the next crash:
Code:
2011-04-19 13:40:38.341 [6755:707] rotation 1.104756
2011-04-19 13:40:38.347 [6755:707] scale 0.000000
2011-04-19 13:40:38.358 [6755:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

Ok...

You changed everything, and now it's crashing in a new place, with a new error. It sounds like you introduced a new bug.

What does the error tell you? It tells you you are trying to index past the end of an array. Ok, what line is it crashing on? The odds are, at that line, you have code that is trying to extract an item from an array, but the array is empty.

You need to learn to debug your code yourself. If you depend on others on the forums to find all your bugs, you're going to drive yourself (and us) crazy.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-19-2011, 08:40 AM   #20 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

I know, I'm sorry but I just want to fix this so I can scale, rotate and move à UIImageView. How can I see which line it is? I just need to fix this but everytime I get à new error..
Sketchiie is offline   Reply With Quote
Old 04-19-2011, 09:07 AM   #21 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sketchiie View Post
I know, I'm sorry but I just want to fix this so I can scale, rotate and move à UIImageView. How can I see which line it is? I just need to fix this but everytime I get à new error..
Make sure you are running a debug build. When it crashes, XCode should show a source window, with an arrow on the left margin, pointing at the line of the crash. The line of the crash should also be highlighted. It will look something like this: (from XCode 4.02, but XCode 3.x will look similar)



My bet is that at the line of the crash, you'll find an expression like this:

Code:
[someArray objectAtIndex: index]
The name of the array and the index variable will be different, obviously.

The error says:

"-[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]"

NSArray objects are zero-indexed. So if you have one item in an array, that item is in index 0.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-19-2011, 09:38 AM   #22 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

Hey, I've debugged it a hundred times but it doesn't show a line with the error. It doesn't show anything. I've got a lot of someArray .., like
Code:
[[[event allTouches] allObjects] objectAtIndex:0]
. But I can't get to see any highlighted lines, the Debugger has exited with status 0 though.

EDIT: I however do see the next message in the debug console:
Code:
0x91f6116c  <+0000>  mov    $0xc0025,%eax
0x91f61171  <+0005>  call   0x91f003d8 <_sysenter_trap>
0x91f61176  <+0010>  jae    0x91f61186 <__kill+26>
0x91f61178  <+0012>  call   0x91f6117d <__kill+17>
0x91f6117d  <+0017>  pop    %edx
0x91f6117e  <+0018>  mov    0xe2e8767(%edx),%edx
0x91f61184  <+0024>  jmp    *%edx
0x91f61186  <+0026>  ret

Last edited by Sketchiie; 04-19-2011 at 09:43 AM.
Sketchiie is offline   Reply With Quote
Old 04-19-2011, 01:42 PM   #23 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 16
Sketchiie is on a distinguished road
Default

Got it ! Seemed that I've set two objects instead of the one I'm using. Thanks everyone for the posts!
Sketchiie is offline   Reply With Quote
Reply

Bookmarks

Tags
binary, error, iphone, operands, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 351
14 members and 337 guests
dansparrow, dre, ilmman, LezB44, lorrettaui53, Nobbsy, Objective Zero, oztemel, pbart, samdanielblr, shagor012, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:47 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0