0

I have a function to save a HTTPCookie between app restarts (which I found on some website):

static fileprivate func loadCookieProperties(from data: Data) -> [HTTPCookiePropertyKey : Any]? {
    let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
    return unarchivedDictionary as? [HTTPCookiePropertyKey : Any]
}

The issue is that the post I found was from 2018 and uses deprecated code. I get this warning:

'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

I have tried searching for that error but all the answers are from 2019 and converts to other functions which are also deprecated.

Any ideas on how to convert this 2 line Swift function so I don't use stuff deprecated in iOS 12?

Thanks!

EDIT:

The linked answer to the question this was marked as a duplicate of doesn't work for me. I found this when searching but couldn't get it to work. Here's what I tried:

let unarchivedDictionary = try NSKeyedUnarchiver.unarchivedObject(
  ofClasses: [NSArray.self, Dictionary.self], from: data) as! [Dictionary]

Errors:

  • Cannot convert value of type 'Dictionary<Key, Value>.Type' to expected element type 'Array.ArrayLiteralElement' (aka 'any AnyObject.Type')
  • Generic parameter 'Key' could not be inferred
  • Generic parameter 'Key' could not be inferred in cast to 'Array'
let unarchivedDictionary = try NSKeyedUnarchiver.unarchivedObject(
  ofClass: Dictionary.self, from: data)

Errors:

  • Generic parameter 'Key' could not be inferred
  • Generic parameter 'Value' could not be inferred
  • Static method 'unarchivedObject(ofClass:from:)' requires that 'Dictionary<Key, Value>' conform to 'NSCoding'
  • Static method 'unarchivedObject(ofClass:from:)' requires that 'Dictionary<Key, Value>' inherit from 'NSObject'
let unarchivedDictionary = try NSKeyedUnarchiver.unarchivedObject(
  ofClass: [HTTPCookiePropertyKey : Any].self, from: data)

Errors:

  • Static method 'unarchivedObject(ofClass:from:)' requires that '[HTTPCookiePropertyKey : Any]' conform to 'NSCoding'
  • Static method 'unarchivedObject(ofClass:from:)' requires that '[HTTPCookiePropertyKey : Any]' inherit from 'NSObject'

So I'll rephrase my question a bit:

How is the solution suggested in the hint and in the answers to other similar questions actually applied the my specific code example? I just can't crack this nut.

Christoffer Reijer
  • 1,925
  • 2
  • 21
  • 40

0 Answers0