Dictionary Iteration in Swift 3

I am trying to iterate through a Dictionary to trim unacknowledged entries. Swift 3 translation of the following Objective-C code does not work:

[[self sharingDictionary] enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) { SharingElement* element=[[self sharingDictionary] objectForKey:key]; if (!element.confirmed){ dispatch_async(dispatch_get_main_queue(), ^{ [element deleteMe]; }); [[self sharingDictionary] performSelector:@selector(removeObjectForKey:) withObject:key afterDelay:.2]; } else{ element.confirmed=NO; }]; 

And so I tried using the following compact numumerated () method as follows:

 for (key, element) in self.sharingDictionary.enumerated(){ if (!element.confirmed){ element.deleteMe() self.perform(#selector(self.removeSharingInArray(key:)), with:key, afterDelay:0.2); } else{ element.confirmed=false } } 

However, the compiler reports the following error while processing the use of the variable 'element':

Value of type tuple '(key: Int, value: SharingElement)' does not have a member 'Confirmed'

Like the "element", he took a full motorcade, than part of his competence. Is there a problem when using enumeration () or processing a dictionary and how can I fix it?

+6
source share
3 answers

As a result, I realized:

 DispatchQueue.global(attributes: .qosBackground).async{ for (key, element) in self.sharingDictionary{ if !element.confirmed{ DispatchQueue.main.async({ element.deleteMe() self.removeSharingInArray(key:key) }) } else{ element.confirmed=false } } } 

So, to securely delete an object without changing the dictionary while viewing it, which was used to split the application, even if I do not know if this is the whole thing.

-3
source

Use element.value.confirmed . element is a collection containing both key and value .

But you probably just want to remove enumerated() :

 for (key, element) in self.sharingDictionary { ... } 

enumerated() iterates and adds indexes starting at zero. This is not very common for use with dictionaries.

+23
source

That should do the trick,

  localDictionary.enumerateKeysAndObjects ({ (key, value, stop) -> Void in }) 
0
source

All Articles