结构数据在从NSValue检索时加扰

时间:2012-05-05 20:41:19

标签: objective-c struct objective-c++

我在这里输入结构的值:

[self createEntityWithX:20 andY:20 withType:0 withWidth:20 andLength:20 atSpeed:5];
...
...
-(void)createEntityWithX:(int)newEntityX andY:(int)newEntityY withType:(int)newEntityType withWidth:(int)newEntityWidth andLength:(int)newEntityLength atSpeed:(int)newEntitySpeed
{
  Entity tmpEntity;
  tmpEntity.entityX = newEntityX;
  tmpEntity.entityY = newEntityY;
  tmpEntity.entityLength = newEntityLength;
  tmpEntity.entityWidth = newEntityWidth;
  tmpEntity.entityType = newEntityType;
  tmpEntity.entitySpeed = newEntitySpeed;

  NSValue *tmp = [NSValue valueWithBytes:&tmpEntity objCType:@encode(struct Entity)];

  [entityArray addObject:tmp];
}


-(Entity)retreiveEntityFromValue:(NSValue *)value
{
  Entity entity1;
  [value getValue:&entity1];
  return entity1;
}

当我拿回来时数据正在被扰乱?我创建了实体:X:20 Y:20宽度:20并且返回x:1712736 Y:0宽度:0? X是完全随机的,但其余的仍为0

我哪里出错?

1 个答案:

答案 0 :(得分:1)

不使用整数,而是尝试使用浮点数(1),或者可以将数据作为点(2)传递。

1)http://www.techotopia.com/index.php/Objective-C_2.0_Data_Types#float_Data_Type 2)http://mobiledevelopertips.com/c/cgrect-cgsize-and-cgpoint.html