2011-07-22 323 views
2

我在我的應用程序中委託一個包含一系列LPProduct對象的NSArray,每個對象都有一個LPColor對象數組,其中我們有一個CGFloat屬性,名爲,價格對象的屬性沒有一致性

爲了得到一個包含所有可定製LPProduct的數組,我在App Delegate中創建了一個名爲getCustomizableProducts的方法。該代碼是在這裏:

- (NSArray *)getCustomizableProducts 
{ 
    NSMutableArray *customizables = [[NSMutableArray alloc] initWithArray:[self products]]; 

    for (LPProduct *product in [self products]) 
    { 
     if (![product isCustomizable]) 
     { 
      [customizables removeObject:product]; 
     } 
    } 

    for (NSInteger i = 0; i < [customizables count]; i++) 
    { 
     for (NSInteger j = 0; j < [[[customizables objectAtIndex:i] colors] count]; j++) 
     { 
      NSLog(@"prices 1: %f", [[[[customizables objectAtIndex:i] colors] objectAtIndex:j] price]); 
     } 
    } 

    return (NSArray *) customizables; 
} 

的NSLog的結果如下:

2011-07-22 14:06:44.463 La Portegna[2937:707] prices 1: 315.000000 
2011-07-22 14:06:44.470 La Portegna[2937:707] prices 1: 315.000000 
2011-07-22 14:06:44.472 La Portegna[2937:707] prices 1: 315.000000 
2011-07-22 14:06:44.473 La Portegna[2937:707] prices 1: 195.000000 
2011-07-22 14:06:44.475 La Portegna[2937:707] prices 1: 195.000000 
2011-07-22 14:06:44.477 La Portegna[2937:707] prices 1: 195.000000 
2011-07-22 14:06:44.478 La Portegna[2937:707] prices 1: 145.000000 
2011-07-22 14:06:44.482 La Portegna[2937:707] prices 1: 145.000000 
2011-07-22 14:06:44.484 La Portegna[2937:707] prices 1: 145.000000 
2011-07-22 14:06:44.489 La Portegna[2937:707] prices 1: 145.000000 
2011-07-22 14:06:44.492 La Portegna[2937:707] prices 1: 55.000000 
2011-07-22 14:06:44.495 La Portegna[2937:707] prices 1: 55.000000 
2011-07-22 14:06:44.498 La Portegna[2937:707] prices 1: 55.000000 
2011-07-22 14:06:44.501 La Portegna[2937:707] prices 1: 55.000000 
2011-07-22 14:06:44.504 La Portegna[2937:707] prices 1: 120.000000 
2011-07-22 14:06:44.507 La Portegna[2937:707] prices 1: 120.000000 
2011-07-22 14:06:44.510 La Portegna[2937:707] prices 1: 120.000000 
2011-07-22 14:06:44.513 La Portegna[2937:707] prices 1: 120.000000 
2011-07-22 14:06:44.516 La Portegna[2937:707] prices 1: 215.000000 
2011-07-22 14:06:44.519 La Portegna[2937:707] prices 1: 215.000000 
2011-07-22 14:06:44.521 La Portegna[2937:707] prices 1: 215.000000 
2011-07-22 14:06:44.524 La Portegna[2937:707] prices 1: 110.000000 
2011-07-22 14:06:44.527 La Portegna[2937:707] prices 1: 110.000000 
2011-07-22 14:06:44.530 La Portegna[2937:707] prices 1: 110.000000 
2011-07-22 14:06:44.533 La Portegna[2937:707] prices 1: 110.000000 
2011-07-22 14:06:44.536 La Portegna[2937:707] prices 1: 110.000000 
2011-07-22 14:06:44.539 La Portegna[2937:707] prices 1: 95.000000 
2011-07-22 14:06:44.542 La Portegna[2937:707] prices 1: 95.000000 
2011-07-22 14:06:44.545 La Portegna[2937:707] prices 1: 95.000000 
2011-07-22 14:06:44.548 La Portegna[2937:707] prices 1: 95.000000 
2011-07-22 14:06:44.550 La Portegna[2937:707] prices 1: 95.000000 
2011-07-22 14:06:44.553 La Portegna[2937:707] prices 1: 180.000000 
2011-07-22 14:06:44.556 La Portegna[2937:707] prices 1: 180.000000 
2011-07-22 14:06:44.559 La Portegna[2937:707] prices 1: 180.000000 
2011-07-22 14:06:44.561 La Portegna[2937:707] prices 1: 65.000000 
2011-07-22 14:06:44.564 La Portegna[2937:707] prices 1: 65.000000 
2011-07-22 14:06:44.568 La Portegna[2937:707] prices 1: 65.000000 
2011-07-22 14:06:44.571 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.573 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.576 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.579 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.582 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.585 La Portegna[2937:707] prices 1: 35.000000 
2011-07-22 14:06:44.588 La Portegna[2937:707] prices 1: 35.000000 

然後,在其他的UIViewController,在viewDidLoad中,我叫getCustomizableProducts這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view from its nib. 
    [[self navigationController] setNavigationBarHidden:YES]; 

    La_PortegnaAppDelegate *appDelegate = (La_PortegnaAppDelegate *) [[UIApplication sharedApplication] delegate]; 

    [self setAll:[appDelegate getCustomizableProducts]]; 

    for (NSInteger i = 0; i < [[self all] count]; i++) 
    { 
     for (NSInteger j = 0; j < [[[[self all] objectAtIndex:i] colors] count]; j++) 
     { 
      NSLog(@"prices 2: %f", [[[[[self all] objectAtIndex:i] colors] objectAtIndex:j] price]); 
     } 
    } 

    [self setProducts:[[NSMutableArray alloc] init]]; 

    [self getCollection:0]; 

    [self printCollection]; 
} 

但是NSLog的結果並不是我們所期望的:

2011-07-22 14:06:44.590 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.593 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.596 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.600 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.602 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.605 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.608 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.611 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.614 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.617 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.620 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.622 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.625 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.628 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.631 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.634 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.637 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.640 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.643 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.646 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.649 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.652 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.654 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.657 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.660 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.663 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.666 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.669 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.672 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.675 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.678 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.681 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.683 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.686 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.689 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.692 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.695 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.697 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.701 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.704 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.707 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.709 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.712 La Portegna[2937:707] prices 2: 0.000000 
2011-07-22 14:06:44.715 La Portegna[2937:707] prices 2: 0.000000 

全部被聲明爲NSArray。

任何人都可以說我做錯了什麼?爲什麼價格突然消失?

非常感謝您提前,最好的問候!

+0

在您的NSLog中打印i&j。我認爲這可能是有用的。 –

回答

0

看起來像all設置不正確。檢查代表返回的內容以及all中存儲的內容以查明問題。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view from its nib. 
    [[self navigationController] setNavigationBarHidden:YES]; 

    La_PortegnaAppDelegate *appDelegate = (La_PortegnaAppDelegate *) [[UIApplication sharedApplication] delegate]; 

    NSLog(@"customizables: %@",[appDelegate getCustomizableProducts]); // <-- add this 
    [self setAll:[appDelegate getCustomizableProducts]]; 
    NSLog(@"all: %@", [self all]);          // <-- and this 

    for (NSInteger i = 0; i < [[self all] count]; i++) 
    { 
     for (NSInteger j = 0; j < [[[[self all] objectAtIndex:i] colors] count]; j++) 
     { 
      NSLog(@"prices 2: %f", [[[[[self all] objectAtIndex:i] colors] objectAtIndex:j] price]); 
     } 
    } 

    [self setProducts:[[NSMutableArray alloc] init]]; 

    [self getCollection:0]; 

    [self printCollection]; 
} 

你也有你的getCustomizableProducts方法內存泄漏。考慮將其最後一行更改爲

return (NSArray *)[customizables autorelease]; 
+0

非常感謝,@Alex,我已經解決了內存泄漏問題。 關於我的問題,沒有任何問題,它是NSLog行不承認價格屬性爲CGFloat。在這一行中進行LPColor轉換讓我看到正確的值。 –