App crashes with reason: Collection <__NSArrayM: 0x7071700> was mutated while being enumerated

前端 未结 3 667
离开以前
离开以前 2021-01-24 13:04

Can you please help me to find reason why my app crashes after clicking on segmented control? Not allways, but usualy after some clicks.

Error message:

         


        
3条回答
  •  长发绾君心
    2021-01-24 13:39

    I think the problem is in these line of code

    for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
    

    and you are using it in two methods didLoad and didUnload. The reason that you are getting because you are updating the content of the following array

    [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]
    

    When you go thorough enumerator class you will get this caution of "not updating the content while enumerator is in use" ... So to overcome this problem simply do following to all the places where you have used this code....

    NSArray* temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in temp) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
    

    i hope this would solve your problem...

提交回复
热议问题