开发者

Parameters values change when passed to function

I returned to one of my ipad projects after few days and strangest thing started to happen. App was working just fine when I left it.

I have very simple constructor for very simple class

@interface MapPathPoint : NSObject {
  int mX, mY;
}

- (id)initWithX:(int)x Y:(int)y;

@property int x, y;

@end

@implementation MapPathPoint

@synthesize x = mX, y = mY;

- (id)initWithX:(int)x Y:(int)y {
  if(self = [super init]) {
    mX = x;
    mY = y;
  }
  return self;
}

@end

When I call

MapPathPoint *start = [[MapPathPoint alloc] initWithX:posX Y:posY];

my mX and mY will contain jun开发者_JS百科k, not values I sent. I even tried to send it with constant numbers

MapPathPoint *start = [[MapPathPoint alloc] initWithX:1 Y:0];

and result are same. When I debug it and step into init function, values of x and y are what looks like random values from memory. I tried clean/rebuild multiple time, restarted xcode and my mac and iPad but nothing helped. Only thing that changes since I last worked on that project was (as far as I know) ipad OS update to 4.2.1 (from 3.2 I think)

Any idea what might be wrong?

EDIT: When I change parameters type to float, everything works as expected. If I use int or unsigned int I get values specified in my comment bellow. If I try to use unsigned short, all I get is 0.


You probably need to be using the generated accessors rather than the member variables directly especially at init . Perhaps the underlying code generation has changed from 3 to 4.

- (id)initWithX:(int)ax Y:(int)ay {
  if(self = [super init]) {
    self.x = ax;
    self.y = ay;
  }
  return self;
}


Could be a problem with the parameter-/property-names. Try renaming the parameters from x/y something different than these.

I'm thinking that you're accidentally assigning the values of the properties to your variables.


Your problem is that your properties are creating x and y instance variables, so doing:

mX = x;
mY = y;

is actually trying to assign the instance variables x and y to mX and mY, rather than the x and y passed as the init method's parameters. ivars didn't used to get created by properties without explicitly setting them in the interface - they do now.

Try this:

@interface MapPathPoint : NSObject {
  int mX, mY;
}

- (id)initWithX:(int)x Y:(int)y;

@property int x, y;

@end

@implementation MapPathPoint

@synthesize x = mX, y = mY;

- (id)initWithX:(int)xx Y:(int)yy {
  if(self = [super init]) {
    mX = xx;
    mY = yy;
  }
  return self;
}

@end


I found the problem, but I still don't know exactly what is going on. I had completely different class that used init function with same name only it accepted float values. And it got somehow mixed. When I remove this float class or rename it's init function everything is ok.

Here is little test case I made

@interface FloatClass : NSObject {
}

- (id)initWithX:(float)x Y:(float)y;

@end

@interface IntClass : NSObject {    
}

- (id)initWithX:(int)x Y:(int)y;

@end

@implementation IntClass

- (id)initWithX:(int)x Y:(int)y {
  if(self = [super init]) {
    NSLog(@"IntClass: %i %i", x, y);
  }
  return self;
}

@end

@implementation FloatClass

- (id)initWithX:(float)x Y:(float)y {
  if(self = [super init]) {
    NSLog(@"FloatClass: %f %f", x, y);
  }
  return self;
}

And when I use those classes

[[IntClass alloc] initWithX:1 Y:2];
[[FloatClass alloc] initWithX:1 Y:2];

result is

IntClass: 1065353216 1073741824
FloatClass: 1.000000 2.000000

Is this bug I should report or feature I didn't know about? Certainly looks like bug, but I have seen stranger features in my life :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜