简体   繁体   English

为什么删除AllObjects,分配nil然后初始化NSMutableArray会使iOS App崩溃?

[英]Why RemovingAllObjects, Assigning nil and then initializing NSMutableArray crashes iOS App?

I have to assign countries, states & cities getting in response from JSON, in an NSMutableArray, Which is initialized in Modal Class. 我必须在NSMutableArray中分配要从JSON响应的国家,州和城市,该模块在模式类中初始化。

I will have to remove all objects in order to set new states and cities, doing that crashes with error 我将必须删除所有对象才能设置新的州和城市,这样做会导致错误崩溃

incorrect checksum for freed object - object was probably modified after being freed. 释放对象的校验和不正确-释放对象后可能已修改了该对象。 *** set a breakpoint in malloc_error_break to debug ***在malloc_error_break中设置断点以进行调试

then in answer https://stackoverflow.com/a/12050676/5568169 came to know that assigning nil to mutableArray will work, and it worked, But now User can again select another country, So now allocating memory [myMutableArray alloc] init]] , gives me the same error i was getting in starting. 然后在回答https://stackoverflow.com/a/12050676/5568169知道将nil分配给mutableArray将起作用,并且它起作用了,但是现在用户可以再次选择另一个国家,所以现在分配内存[myMutableArray alloc] init]] ,给了我与开始时同样的错误。

    -(void)fetchStates:(NSString*)idString {

        [registrationModalContactVC.allStateArray removeAllObjects];
        registrationModalContactVC.allStateArray = nil;
        [registrationModalContactVC.allStateDict removeAllObjects];
        registrationModalContactVC.allStateDict = nil;
        registrationModalContactVC.allStateArray = [NSMutableArray new];
        registrationModalContactVC.allStateDict = [NSMutableDictionary new];
}

Kindly help 请帮助

You should not be dong this : 您不应该这样:

[myMutableArray alloc] init]

What you mean to do is : 您的意思是:

myMutableArray = [[NSMutableArray alloc] init];

我认为您的mutablearray是Pickview的数据源,当您删除所有对象时,Pickview的状态如何。

Removing below code is working, but it may give the same error again 删除下面的代码是可行的,但可能会再次出现相同的错误

registrationModalContactVC.allStateArray = nil;
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM