Invalid initializer call with same type 'FileAttributeKey' as parameter

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Ran into a little bug tonight going through the official quick start guide for the Realm Database on iOS. The app which I’m building needs to be able to access the database while running in the background, so according to the Realm documentation developers need to change some security settings for the folder containing the database in order to access that database from the background. Without changing the security settings, the folder containing the database will be encrypted and not accessible when a user’s iPhone is locked. The official documentation suggests adding the following lines of code so that the Realm Database is accessible even when the phone is locked:

let realm = try! Realm()
// Get our Realm file's parent directory
let folderPath = realm.configuration.fileURL!.deletingLastPathComponent().path
// Disable file protection for this directory
try! FileManager.default.setAttributes([FileAttributeKey(rawValue: NSFileProtectionKey): NSFileProtectionNone], ofItemAtPath: folderPath)

However, when I tried using this little snippet of code in my current iOS project (WingsuitGP) I received the following error message: Invalid initializer call with same type ‘FileAttributeKey’ as parameter
Luckily, someone on StackOverflow got me pointed in the right direction: invalid initializer call with same type ‘XXXX’ as parameter
But I still needed a little help from Xcode’s suggested bug fixes to get it to work just right. So here is what I ended up with if anyone else is struggling with this annoying error:

let realm = try! Realm()
// Get our Realm file's parent directory
let folderPath = realm.configuration.fileURL!.deletingLastPathComponent().path
// Disable file protection for this directory
try! FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none], ofItemAtPath: folderPath)

 

topherPedersen