ios - 具有相同索引的不同对象的 NSMutableArray 的奇怪行为

标签 ios objective-c

我遇到了一个奇怪的问题,我找不到我写的测试代码的错误。我正在编写一段代码来生成 AttributedString 并将其放入 NSMutableArray,但是当我使用 For 循环显示从 NSMutableArray 添加的对象时,输出显示具有相同索引的同一对象 3 次,我正在尝试调试它2 天了,找不到错误,希望我能在这里得到一些帮助/建议。

实例化NSMutableArray并将AttributedString加入数组的代码

-(instancetype)init
{
    self = [super init];

    if (self) {
        for (NSMutableAttributedString *attrString in [SetPlayingCard symbolArray]) {
            for (NSString *attrColor in [SetPlayingCard colorsArray]) {
                if ([attrColor isEqualToString:@"redColor"]) {
                    [attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
                                            NSStrokeColorAttributeName : [UIColor redColor]}
                                    range:NSMakeRange(0, [attrString length])];
                    NSLog(@"%@ %@",attrColor, attrString);
                } else if ([attrColor isEqualToString:@"blueColor"]){
                    [attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
                                            NSStrokeColorAttributeName : [UIColor blueColor]}
                                    range:NSMakeRange(0, [attrString length])];
                    NSLog(@"%@ %@",attrColor, attrString);
                } else if ([attrColor isEqualToString:@"purpleColor"]){
                    [attrString setAttributes:@{NSStrokeWidthAttributeName : @3,
                                            NSStrokeColorAttributeName : [UIColor purpleColor]}
                                    range:NSMakeRange(0, [attrString length])];
                    NSLog(@"%@ %@",attrColor, attrString);
                }
                [self addCard:attrString];
            }
        }
    }
    return self;
}

被调用的类方法

+ (NSArray *)colorsArray
{
    return @[@"redColor",@"blueColor",@"purpleColor"];
}

+ (NSArray *)symbolArray
{
    NSMutableAttributedString *triangle = [[NSMutableAttributedString alloc] initWithString:@"Triangle"];
    NSMutableAttributedString *square = [[NSMutableAttributedString alloc] initWithString:@"Square"];
    NSMutableAttributedString *round = [[NSMutableAttributedString alloc] initWithString:@"Round"];

    return @[triangle,square,round];
}

添加和显示输出的方法

- (void)addCard:(NSAttributedString *)card
{
    NSLog(@"String to be added to array: %@", card);
    [self.cards addObject:card];
    NSLog(@"Index is %d for %@", [self.cards indexOfObject:card], card);
}


- (NSAttributedString *)printCard
{
    NSAttributedString *card;

    if ([self.cards count]) {
        for (card in self.cards) {
            NSLog(@"Count: %d, Array index: %d, Card from the deck is: %@, ", [self.cards count], [self.cards indexOfObject:card], card);
        }
    } 
    return card;
}

我在 addCard 方法中从 NSLog 得到的输出如下。

2014-04-01 22:40:56.184 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.185 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.186 UnitTest[1008:60b] Count: 9, Array index: 0, Card from the deck is: Triangle{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.187 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.188 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.189 UnitTest[1008:60b] Count: 9, Array index: 3, Card from the deck is: Square{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.190 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.191 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
}, 
2014-04-01 22:40:56.192 UnitTest[1008:60b] Count: 9, Array index: 6, Card from the deck is: Round{
    NSStrokeColor = "UIDeviceRGBColorSpace 0.5 0 0.5 1";
    NSStrokeWidth = 3;
},

最佳答案

您的 printCard 方法有误,将其更改为:

- (NSAttributedString *)printCard
{
    if ([self.cards count] > 0) {
        for (NSAttributedString *card in self.cards) {
            NSLog(@"Count: %d, Card from the deck is: %@, ", [self.cards count], card);
        }
    } 
    return card;
}

如果你想要索引,你可以使用:

for (int i = 0; i < self.cards.count; i++) {
    NSAttributedString *card = self.cards[i];
    NSLog(@"Count: %d, Array index: %d, Card from the deck is: %@, ", [self.cards count], i, card);
}

您应该回顾一下 for in 循环的工作原理。

关于ios - 具有相同索引的不同对象的 NSMutableArray 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789506/

相关文章:

iphone - Iphone 应用程序中的推送通知

ios - 如何从 Pan 手势触发 Segue?

ios - 从NSMutableArray删除for循环内的对象

ios - 如何在导航 Controller 中将数据从一个 View 传递到另一个 View

iphone - 更改 reg-ex 以在 iphone 上进行电子邮件验证

iphone - 在 didSelectRowAtIndexPath 访问自定义标签属性

ios - 无法跨多个 iOS 设备同步 Amazon Cognito 数据集

ios - 无法使用 ARKit (ARKit + CoreLocation) 对场景节点进行动画处理

ios - UITapGestureRecognizer 根本不工作

iphone - 如何在 iOS 中更改 GPS 更新时间