Actually I receive a somehat strange exception: I iterate a MutableDictionary and want to aset a new value in it:
selectionIndex = [NSMutableDictionary dictionaryWithDictionary:selection];
NSString *whatever = @"999999";
id keys;
NSEnumerator *keyEnum = [selectionIndex keyEnumerator];
while (keys = [keyEnum nextObject])
{
[selectionIndex setObject:whatever forKey:keys];
}
Btw, selection, that is passed to this method is a MutableDictionary. If I run this code, I receive the following exception:
2011-12-05 15:28:05.993 lovelini[1333:207] * Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSCFDictionary: 0x6a33ed0> was mutated while being enumerated.{type = mutable dict, count = 8, entries => 0 : {contents =
Ok, I know that I can't change NSDictionary, but as far as I see it, I don't! So why do I get this exception? Is this a restriction of Fast Enumeration??? As far as I know it is not possible to add or remove entries within Fast Enumeration, but I don't add or remove anything?!
You cannot make any changes to a collection while enumerating it. You could instead enumerate the keys of the dictionary instead of the dictionary itself:
for (NSString *key in selectionIndex.allKeys) {
[selectionIndex setObject:whatever forKey:key];
}
枚举它时更改值是个坏主意,您可以将元素收集到新字典中,然后用新字典替换原始字典。
Fast enumeration in only meant for viewing objects in collection. You can't modify elements.
Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised. AppleDeveloperPortal
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/services/userDATE.php?user_id=%@",MAINDOMAINURL,[[NSUserDefaults standardUserDefaults] objectForKey:@"USERID"]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil)
{
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc]init];
jsonDict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"DATA:%@",jsonDict);
NSLog(@"driver tracking ::%@",[jsonDict objectForKey:@"response"]);
if ([[jsonDict objectForKey:@"avialabity"] isEqualToString:@"yes"]) {
isDriverTracking=YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self hideActivityIndicator];
[self performSegueWithIdentifier:@"FIRSTSEGUE" sender:nil];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[self hideActivityIndicator];
[self performSegueWithIdentifier:@"SECONDSEGUE" sender:nil];
});
}
[self hideActivityIndicator];
}else [self customeAlertController:@"Alert !" :@"Plese check Your internet connection."];
}];
[postDataTask resume];
I GOT SAME ERROR was mutated while being enumerated.
Yes, you are adding something to the thing you're enumerating over:
[selectionIndex setObject:
It's not a restriction of fast enumeration, but of enumeration generally. Fast enumeration is a more convenient (and actually faster) way of doing enumeration. (See this comparison .)
Bottom line: don't fiddle with the contents of a collection while you're enumerating over it, you'll have problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.