iPhone fetch data dictionary from keychain

前端 未结 3 1923
无人共我
无人共我 2020-12-13 02:59

So I\'m trying to convert an old project to Automatic Reference Counting. I\'m trying to use the conversion tool that xCode has but it says to fix a couple things before it

3条回答
  •  渐次进展
    2020-12-13 03:48

    Method 3: Let ARC do the heavy lifting (or a combination of method 1 and method 2):

    NSMutableDictionary* query = [NSMutableDictionary dictionaryWithDictionary:
    @{
        (__bridge id) kSecClass : (__bridge id) kSecClassGenericPassword,
        (__bridge id) kSecAttrService : nssService,
    #if ! TARGET_IPHONE_SIMULATOR
        (__bridge id) kSecAttrAccessGroup : @"PRODUCT.com.COMPANY.GenericKeychainSuite",
    #endif
    
        (__bridge id) kSecMatchLimit : (__bridge id) kSecMatchLimitOne,
        (__bridge id) kSecReturnAttributes : (__bridge id) kCFBooleanTrue,
    }];
    
    if ( [nssAccount length] != 0 )
        [query setObject:nssAccount forKey:(__bridge id) kSecAttrAccount];
    
    CFDictionaryRef cfresult;
    auto err =  ::SecItemCopyMatching((__bridge CFDictionaryRef)query,
                                      (CFTypeRef*)&cfresult);
    if ( err == errSecItemNotFound )
        return std::wstring();
    else if ( err != noErr)
        throw std::exception();
    
    NSDictionary* result = (__bridge_transfer NSDictionary*) cfresult;
    

    SecItemCopyMatching doesn't need to own the incoming dictionary, so __bridge is adequate and then ARC continues to manage the lifetime of query.

    And by transferring ownership of the result to arc, it will manage the lifetime of result as well and free us of the need to remember to CFRelease it on all code paths.

提交回复
热议问题