对象的属性没有一致性

发布于 2024-11-25 19:21:53 字数 7736 浏览 1 评论 0原文

我的应用程序委托中有一个 NSArray,其中包含一系列 LPProduct 对象,每个对象都有一个 LPColor 对象数组,其中有一个名为 price 的 CGFloat 属性。

为了获取包含所有可自定义 LPProduct 的数组,我也在 App Delegate 中创建了一个名为 getCustomizedProducts 的方法。代码在这里:

    - (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 处,我像这样调用 getCustomizedProducts

- (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

all 被声明为 NSArray。

任何人都可以告诉我我做错了什么?为什么价格突然消失了?

预先非常感谢您,最诚挚的问候!

I have in my App Delegate an NSArray that contains a range of LPProduct objects, each of them has an array of LPColor objects where we have a CGFloat property called price.

In order to get an array with all the customizable LPProduct I made a method called getCustomizableProducts in the App Delegate too. The code is here:

    - (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;
}

The result of the NSLog is the following:

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

Then, in other UIViewController, at viewDidLoad, I call getCustomizableProducts like that:

- (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];
}

But the result of the NSLog is not that we would expect:

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

all is declared as NSArray.

Anyone can say me what I doing wrong? Why the prices disappear suddenly?

Thank you very much in advance, best regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-12-02 19:21:53

看起来 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];
}

您的 getCustomizedProducts 方法中也存在内存泄漏。考虑将其最后一行更改为

return (NSArray *)[customizables autorelease];

It looks like all is not set properly. Check what's returned from the delegate and what's stored in all to pinpoint the problem.

- (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];
}

You also have a memory leak in your getCustomizableProducts method. Consider changing its last line to

return (NSArray *)[customizables autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文