如何在Swift中枚举和删除存储的凭据

时间:2015-09-11 08:33:49

标签: ios swift optional

(斯威夫特初学者问题) 在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)
    }
}

0 个答案:

没有答案