2013-04-11 64 views
0

這是相關的代碼:在NSMutableArray的值元素改變奇怪

@property (nonatomic, strong) NSMutableArray *coordinateInPixels; 

... 

NSLog(@"First log %@",self.coordinateInPixels); 
NSLog(@"self.zoomScale %f",self.zoomScale); 

if ([self.coordinateInPixels count]>0) { 
    NSMutableArray *coordInScala = self.coordinateInPixels; 

    for (int i=0; i<[coordInScala count]; i++) { 
     CGPoint puntoRicalcolato = CGPointMake([[coordInScala objectAtIndex:i] CGPointValue].x*self.zoomScale, 
     [[coordInScala objectAtIndex:i] CGPointValue].y*self.zoomScale); 

     [coordInScala replaceObjectAtIndex:i withObject:[NSValue valueWithCGPoint:puntoRicalcolato]]; 
    } 

    [self DrawRoute:coordInScala]; 

} 

NSLog(@"Third log %@",self.coordinateInPixels); 

DrawRoute很簡單:

- (void)DrawRoute:(NSMutableArray *)coords 
{ 
    self.route = [[ALCRoute alloc] init]; 
    [self.route setCoord:coords]; 

    CGRect rettangolo = CGRectMake([[coords objectAtIndex:0] CGPointValue].x, [[coords lastObject] CGPointValue].y, [[coords lastObject] CGPointValue].x-[[coords objectAtIndex:0] CGPointValue].x, [[coords objectAtIndex:0] CGPointValue].y-[[coords lastObject] CGPointValue].y); 

    NSLog(@"Second log %@",self.coordinateInPixels); 

    [self.route setFrame:rettangolo]; 
    [self addSubview:self.route]; 

} 

問題定義:

的問題是,self.coordinateInPixels'元素與第一個日誌中的第二個和第三個日誌不同。 在drawroute之前沒有任何其他操作,但神奇的元素沒有(明顯地)任何原因改變... 任何想法?

這裏的輸出控制檯:

2013-04-11 12:00:05.265 APP[4233:14c03] First log (
    "NSPoint: {2442.0994, 2088.6094}", 
    "NSPoint: {2442.0994, 2088.6094}", 
    "NSPoint: {1968.8776, 1395.6793}", 
    "NSPoint: {1965.4939, 1390.1688}", 
    "NSPoint: {1962.0531, 1384.6427}", 
    "NSPoint: {2442.0752, 2088.6128}", 
    "NSPoint: {2442.0752, 2088.6128}", 
    "NSPoint: {2167.9409, 1604.3606}", 
    "NSPoint: {2164.2456, 1598.6599}", 
    "NSPoint: {2160.8596, 1593.2056}", 
    "NSPoint: {2157.3921, 1587.6027}" 
) 
2013-04-11 12:00:05.266 APP[4233:14c03] self.zoomScale 0.552063 
2013-04-11 12:00:05.266 APP[4233:14c03] Second log (
    "NSPoint: {1348.1931, 1153.0443}", 
    "NSPoint: {1348.1931, 1153.0443}", 
    "NSPoint: {1086.9448, 770.50317}", 
    "NSPoint: {1085.0768, 767.461}", 
    "NSPoint: {1083.1772, 764.41022}", 
    "NSPoint: {1348.1798, 1153.0461}", 
    "NSPoint: {1348.1798, 1153.0461}", 
    "NSPoint: {1196.8403, 885.70837}", 
    "NSPoint: {1194.8003, 882.56128}", 
    "NSPoint: {1192.931, 879.55011}", 
    "NSPoint: {1191.0167, 876.45697}" 
) 
2013-04-11 12:00:05.267 APP[4233:14c03] Third log (
    "NSPoint: {1348.1931, 1153.0443}", 
    "NSPoint: {1348.1931, 1153.0443}", 
    "NSPoint: {1086.9448, 770.50317}", 
    "NSPoint: {1085.0768, 767.461}", 
    "NSPoint: {1083.1772, 764.41022}", 
    "NSPoint: {1348.1798, 1153.0461}", 
    "NSPoint: {1348.1798, 1153.0461}", 
    "NSPoint: {1196.8403, 885.70837}", 
    "NSPoint: {1194.8003, 882.56128}", 
    "NSPoint: {1192.931, 879.55011}", 
    "NSPoint: {1191.0167, 876.45697}" 
) 

解決方案!

更改此:

if ([self.coordinateInPixels count]>0) { 
     NSMutableArray *coordInScala = self.coordinateInPixels; 

     for (int i=0; i<[coordInScala count]; i++) { 
      CGPoint puntoRicalcolato = CGPointMake([[coordInScala objectAtIndex:i] CGPointValue].x*self.zoomScale, 
      [[coordInScala objectAtIndex:i] CGPointValue].y*self.zoomScale); 

      [coordInScala replaceObjectAtIndex:i withObject:[NSValue valueWithCGPoint:puntoRicalcolato]]; 
     } 

     [self DrawRoute:coordInScala]; 

    } 

有了這個:

if ([self.coordinateInPixels count]>0) { 
    NSMutableArray *coordInScala = [[NSMutableArray alloc] init]; 

    for (int i=0; i<[self.coordinateInPixels count]; i++) { 
     CGPoint puntoRicalcolato = CGPointMake([[self.coordinateInPixels objectAtIndex:i] CGPointValue].x*self.zoomScale, 
                [[self.coordinateInPixels objectAtIndex:i] CGPointValue].y*self.zoomScale); 

     [coordInScala addObject:[NSValue valueWithCGPoint:puntoRicalcolato]]; 
    } 

    [self DrawRoute:coordInScala]; 

} 

,並都開始正常工作......

回答

1

在這一行:

NSMutableArray *coordInScala = self.coordinateInPixels; 

你是不做一個新的陣列。 coordInScala是對self.coordinateInPixels的引用,所以您做出的任何更改,實際上都會使它們變爲self.coordinateInPixels

你可能想這樣的:

NSMutableArray *coordInScala = self.coordinateInPixels.mutableCopy; 
+0

好,我很愚蠢的...三江源交朋友! – Lucabro 2013-04-11 10:32:49