1

I'm trying to implement this code for accelerometer use, but I'm getting a compiler error:

stray /342, stray/200, stray/223 ... for size,currentTouch, position object's and so on ...

Why?

Code

File GravityObj.h

@interface GravityObj : UIView  {
    CGPoint position;
    CGSize size;
    CGPoint velocity;
    NSTimer *objTimer;
    NSString *pngName;
    CGFloat bounce;
    CGFloat gravity;
    CGPoint acceleratedGravity;
    CGPoint lastTouch;
    CGPoint currentTouch;
    BOOL dragging;
}

@property CGPoint position;
@property CGSize size;
@property CGPoint velocity;
@property(nonatomic,retain)NSString *pngName;
@property(nonatomic,retain)NSTimer *objTimer;
@property CGFloat bounce;
@property CGFloat gravity;
@property CGPoint acceleratedGravity;
@property CGPoint lastTouch;
@property CGPoint currentTouch;
@property BOOL dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s;
- (void)update;
- (void)onTimer;

@end

File GravityObj.m

#import "GravityObj.h"

@implementation GravityObj

@synthesize position, size;
@synthesize objTimer;
@synthesize velocity;
@synthesize pngName;
@synthesize bounce;
@synthesize gravity, acceleratedGravity;
@synthesize lastTouch, currentTouch;
@synthesize dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
    if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
        [self setPngName:imageName];
        [self setPosition:p];
        [self setSize:s];
        [self setBackgroundColor:[UIColor clearColor]];

        // Set default gravity and bounce
        [self setBounce:-0.9f];
        [self setGravity:0.5f];
        [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
        [self setDragging:NO];

        UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
        prezzie.image = [UIImage imageNamed:imageName];

        [self addSubview:prezzie];
        [prezzie release];

        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    }
    return self;
}

- (void)update {
    [self setNeedsDisplay];

    if(dragging)
        return;

    velocity.x += acceleratedGravity.x;
    velocity.y += acceleratedGravity.y;
    position.x += velocity.x;
    position.y += velocity.y;

    if(position.x + size.width >= 320.0) {
        position.x = 320.0 – size.width;
        velocity.x *= bounce;
    }
    else if(position.x <= 0.0) {
        velocity.x *= bounce;
    }

    if(position.y + size.height >= 416.0) {
        position.y = 416.0 – size.height;
        velocity.y *= bounce;
    }
    else if(position.y <= 0.0) {
        velocity.y *= bounce;
    }
        self.frame = CGRectMake(position.x, position.y, size.width, size.height);
}

- (void)onTimer {
    [self update];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}
/* EVENTS */

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    acceleratedGravity.x = acceleration.x * gravity;
    acceleratedGravity.y = -acceleration.y * gravity;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // First, let's check to make sure the timer has been initiated
    if (objTimer == nil) {
        objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }

    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    CGFloat dx = currentTouch.x – position.x;
    CGFloat dy = currentTouch.y – position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist < size.width) {
        [self setVelocity:CGPointMake(0.0, 0.0)];
        [self setDragging:YES];
    }
    [self setLastTouch:currentTouch];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    [self setDragging:YES];
    [self setVelocity:CGPointMake(currentTouch.x – lastTouch.x, currentTouch.y – lastTouch.y)];
    [self setLastTouch:currentTouch];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setDragging:NO];
}

- (void)dealloc {
    [objTimer release], objTimer = nil;
    [pngName release], pngName = nil;
    [super dealloc];
}

@end
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omri
  • 305
  • 1
  • 3
  • 10
  • There isn't any need to retype anything. The triplet 342, 200, 223 makes it clear what it is: 342 200 223 (octal) → 0xE2 0x80 0x93 (hexadecimal) → UTF-8 sequence for Unicode code point U+2013 ([EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). It can be searched (and replaced) for by using the regular expression `\x{2013}` in any modern text editor or IDE. Note: The notation is different in Visual Studio Code (and probably others): `\u2013` (instead of `\x{2013}`) – Peter Mortensen May 23 '23 at 16:00
  • cont' - There are six instances of U+2013 (and no other of the common ones, like NO-BREAK SPACE, BROKEN BAR, LEFT-POINTING DOUBLE ANGLE QUOTATION MARK, REGISTERED SIGN, RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK, LATIN SMALL LETTER A WITH DIAERESIS, EM SPACE, THIN SPACE, ZERO WIDTH SPACE, ZERO WIDTH NON-JOINER, EN DASH, EM DASH, LEFT SINGLE QUOTATION MARK, RIGHT SINGLE QUOTATION MARK, LEFT DOUBLE QUOTATION MARK, RIGHT DOUBLE QUOTATION MARK, LINE SEPARATOR, PARAGRAPH SEPARATOR, LEFT-TO-RIGHT EMBEDDING, RIGHT-TO-LEFT EMBEDDING, POP DIRECTIONAL FORMATTING, WORD JOINER, etc.) – Peter Mortensen May 23 '23 at 16:05
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 23 '23 at 16:06

3 Answers3

4

It looks like the minus signs in some of your lines were Unicode characters that display as minuses and aren't. I copied your code into Xcode and got the same error. I then retyped each line that had the error and it worked.

Here's the fixed version (.m file only):

@implementation GravityObj

@synthesize position, size;
@synthesize objTimer;
@synthesize velocity;
@synthesize pngName;
@synthesize bounce;
@synthesize gravity, acceleratedGravity;
@synthesize lastTouch, currentTouch;
@synthesize dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
    if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
        [self setPngName:imageName];
        [self setPosition:p];
        [self setSize:s];
        [self setBackgroundColor:[UIColor clearColor]];

        // Set default gravity and bounce
        [self setBounce:-0.9f];
        [self setGravity:0.5f];
        [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
        [self setDragging:NO];

        UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
        prezzie.image = [UIImage imageNamed:imageName];

        [self addSubview:prezzie];
        [prezzie release];

        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    }
    return self;
}

- (void)update {
    [self setNeedsDisplay];

    if(dragging) return;

    velocity.x += acceleratedGravity.x;
    velocity.y += acceleratedGravity.y;
    position.x += velocity.x;
    position.y += velocity.y;

    if(position.x + size.width >= 320.0) {
        position.x = 320.0 - size.width;
        velocity.x *= bounce;
    }
    else if(position.x <= 0.0) {
        velocity.x *= bounce;
    }

    if(position.y + size.height >= 416.0) {
        position.y = 416.0 - size.height;
        velocity.y *= bounce;
    }
    else if(position.y <= 0.0) {
        velocity.y *= bounce;
    }
    self.frame = CGRectMake(position.x, position.y, size.width, size.height);
}

- (void)onTimer {
    [self update];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}
/* EVENTS */

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    acceleratedGravity.x = acceleration.x * gravity;
    acceleratedGravity.y = -acceleration.y * gravity;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // First, lets check to make sure the timer has been initiated
    if (objTimer == nil) {
        objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }

    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    CGFloat dx = currentTouch.x - position.x;
    CGFloat dy = currentTouch.y - position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist < size.width) {
        [self setVelocity:CGPointMake(0.0, 0.0)];
        [self setDragging:YES];
    }
    [self setLastTouch:currentTouch];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    [self setDragging:YES];
    [self setVelocity:CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y)];
    [self setLastTouch:currentTouch];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setDragging:NO];
}

- (void)dealloc {
    [objTimer release], objTimer = nil;
    [pngName release], pngName = nil;
    [super dealloc];
}

@end

If you get that error again, I found that viewing the file in FileMerge can help; it doesn't seem to know Unicode as well as Xcode.

Cajunluke
  • 3,103
  • 28
  • 28
1

You can pick up stray /342 /200 from copy/paste, usually from PDF documents and sometimes HTML.

Paste to a terminal and copy back out for a quick fix.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
t011phr33
  • 271
  • 2
  • 2
0

Yes, I had also faced such types of error. It can be solved by just removing some unwanted formatted line space, like gray focused line, which is copied with content of code which you copied from other sites, a web page, or a PDF file.

It will definitely remove such types of error by removing the extra formatted line or by just copy paste that code portion into Notepad and original text copy back to your code. It's done!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sandip Patel - SM
  • 3,346
  • 29
  • 27