Reputation: 1303
I am having trouble in converting this Block code from Objective C into Swift. I searched the web found some examples but none fixed the errors I get.
Any help would be appreciated.
- (void)didReceiveMemoryWarning {
[[self.viewControllersByIdentifier allKeys] enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
if (![self.destinationIdentifier isEqualToString:key]) {
[self.viewControllersByIdentifier removeObjectForKey:key];
}
}];
[super didReceiveMemoryWarning];
}
Here is what I tried:
override func didReceiveMemoryWarning() {
var array : NSArray = self.viewControllersByIdentifier.allKeys
array.enumerateObjectsUsingBlock { (key, idx, stop) in
if (![self.destinationIdentifier == key]) {
self.viewControllersByIdentifier .removeObjectForKey(key)
}
}
super.didReceiveMemoryWarning()
}
And the error I am getting is on the "if" statement and it tells me that "String is not convertible to "MirrorDisposition".
Upvotes: 2
Views: 787
Reputation: 40965
You’ve left a bit of Objective-C in your Swift (some rogue square brackets):
if(![self.destinationIdentifier == key]) {
However, you would probably find it easier to use Swift’s for-in than array.enumerateObjectsUsingBlock
:
override func didReceiveMemoryWarning() {
for key in self.viewControllersByIdentifier.allKeys {
// note key will be an AnyObject so you need to cast it to an appropriate type…
// also, this means you can use != rather than ! and ==
if self.destinationIdentifier != key as? NSString {
self.viewControllersByIdentifier.removeObjectForKey(key)
}
}
super.didReceiveMemoryWarning()
}
Upvotes: 2
Reputation: 80265
Did you look at the documentation?
func enumerateObjectsUsingBlock(_ block: (AnyObject!,
Int,
UnsafeMutablePointer<ObjCBool>) -> Void)
Upvotes: 0