替换 NSMutableArray 中出现的字符串只工作一次

Replacing Occurrences of Strings in NSMutableArray Only Working Once

我很困惑,也许漏掉了一些简单的东西,但我找不到。

我会在理论上解决这个问题,因为我已经做了一个测试,但仍然无法使用精简代码。我正在使用 SpriteBuilder,但这不应该是导致问题的原因 - 我可以记录我在文本输入中获得的值,但根本无法再次将该值输入数组 - 但足够咆哮,时间一些代码。

Main.h

#import "Content.h"
#import "ReplaceMe.h"

@property Content *contentInstance;
@property ReplaceMe *replaceMeInstance; 

Main.m

   -(void)someFunction{
    _contentInstance = (Content*)[CCBReader load:@"ContentScene"];
  [_contentInstance.changeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([_contentInstance.base[idx] containsString:@"one"]){
            NSLog(@"I contain one!");
            NSString *replace = [_content.testArray[idx] stringByReplacingOccurrencesOfString:@"one" withString:changeTheText.string];
            [_content.testArray replaceObjectAtIndex:idx withObject:replace];
            NSLog(@"%@",_content.testArray[idx]);
        }
    }];
[_contentInstance updateLabels];
    }

Content.h

@property NSArray *base;
@property NSArray *changeArray;

Content.m

-(void)someFunction{
    _base = @[@"This is one",@"This is two",@"This is two point one"];
    _changeArray = [base mutableCopy];
}
-(void)updateLabels{
    [_changeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        _labeler = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",_changeArray[idx]] fontName:@"Helvetica" fontSize:12.0f];
        }
    }];
}

ReplaceMe.h

@property CCTextField *changeTheText;
-(void)_changeTheTextSelector;

ReplaceMe.m

-(void)_changeTheTextSelector{
self.visible=NO;
}

当我调用 MainScene 的 someFuction 时,这个设置在第一次就可以正常工作 - 而且只是第一次。第一次 运行 枚举后,我无法让 changeArray 更新。

我知道 changeTheText 在退出时正在更改,但是当我登录 changeArray[idx] 时,它卡在第一个 changeTheText 处。

有什么想法吗?

试试这个并与您的代码进行比较。它对我来说很好用;

NSArray* base = @[@"This is one",@"This is two",@"This is two point one"];
NSMutableArray *changeArray = [base mutableCopy];

[base enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj containsString:@"one"]) {
        NSLog(@"data : %@", changeArray[idx]);
        //--
        [changeArray replaceObjectAtIndex:idx withObject:[((NSString*) obj) stringByReplacingOccurrencesOfString:@"one" withString:@""]];
    }
}];

NSLog(@"changeArray %@", changeArray);