我最近将一个庞大的库迁移到 ARC,无工具部分令人头疼。这是代码:
+ (NSString *)getKeychainItem:(NSString *)identifier
{
NSString *fullIdentifier = [NSString stringWithFormat:@"%@%@", kIdentifierPrefix, identifier];
NSMutableDictionary *queryKeychain;
OSStatus status = noErr;
queryKeychain = [NSMutableDictionary dictionary];
// Set the public key query dictionary.
[queryKeychain setObject:(__bridge id)kSecClassGenericPassword
forKey:(__bridge id)kSecClass];
// Get the key.
CFDataRef data;
CFDictionaryRef queryKeychainCF = (__bridge CFDictionaryRef)queryKeychain;
status = SecItemCopyMatching(queryKeychainCF, (CFTypeRef *)&data);
NSData *passwordData = (__bridge_transfer NSData *)data;
NSString *password;
if (status == noErr)
{
password = [[NSString alloc] initWithBytes:[passwordData bytes]
length:[passwordData length]
encoding:NSUTF8StringEncoding];
}
else if (status != errSecItemNotFound)
{
NSLog(@"Error getting keychain item %@ -- OSStatus: %lu", identifier, status);
}
return password;
}
这应该很简单,但是,passwordData 对象被过度释放,我不知道为什么,堆栈跟踪是this。如果我只是设置passwordData
并且nil
不执行__bridge__transfer
,它不会崩溃。关于为什么的任何想法?
非常感谢!