(斯威夫特初学者问题) 在ObjC中,您可以枚举和删除特定凭据,如下所示:
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:networkHelper.protectionSpace];
[credentials enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSURLCredential *credential = obj;
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:networkHelper.protectionSpace];
}];
我正在尝试将其移植到Swift,但我遇到了编译器的困难,在该方法credentialsForProtectionSpace
中返回类型[NSObject: AnyObject]?
。因此,我想问一下社区,在Swift中枚举和删除存储凭据的正确方法是什么?我想出了这个代码,但我不确定这是否是正确的方法。特别是,我担心铸件正在进行中。
if let credentials = NSURLCredentialStorage.sharedCredentialStorage().credentialsForProtectionSpace(networkHelper.protectionSpace) {
for (index, element) in enumerate(credentials) {
NSURLCredentialStorage.sharedCredentialStorage().removeCredential(element.1 as! NSURLCredential, forProtectionSpace: networkHelper.protectionSpace)
}
}