UserDefaults.standard return error EXCBADACCESS (code=1, address=0x38)

Hi I'm using this code to save into UserDefault some datas


Code Block
import UIKit
//GLOBAL STRUCT USER
struct User: Codable{
  var info: UserInfo?
}
//MARK:-USER INFO
struct UserInfo: Codable{
  var companyName: String?
  var companyAddress: String?
  var companyCity: String?
  var companyCap: String?
  var companyTelephone: String?
  var companyFax: String?
  var companyMobile: String?
  var companyWeb: String?
  var companyEmail: String?
  var companyVat: String?
  var companySDI: String?
  var companyPEC: String?
  var companyLogo: String?
   
}
struct LocalData{
  static var userDatas: User?{
    get{
      guard let datas = UserDefaults.standard.data(forKey: #function) else {
        return User(info: UserInfo(companyName: "", companyAddress: "", companyCity: "", companyCap: "", companyTelephone: "", companyFax: "", companyMobile: "", companyWeb: "", companyEmail: "", companyVat: "", companySDI: "", companyPEC: "", companyLogo: ""))
         
      }
      do{
        let myData = try JSONDecoder().decode(User.self, from: datas)
        return myData
      }
      catch{
        print(error)
      }
      return User(info: UserInfo(companyName: "", companyAddress: "", companyCity: "", companyCap: "", companyTelephone: "", companyFax: "", companyMobile: "", companyWeb: "", companyEmail: "", companyVat: "", companySDI: "", companyPEC: "", companyLogo: ""))
    }
    set{
      guard let mydatas = try? JSONEncoder().encode(newValue) else { return }
      print(mydatas)
      UserDefaults.standard.setValue(mydatas, forKey: #function)
       
    }
  }
}
LocalData.userDatas?.info?.companyAddress = "TEST"
print(LocalData.userDatas?.info)

If I try to run this code into PlayGround I get this error


error: Execution was interrupted, reason: EXCBADACCESS (code=1, address=0x38).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

How can I solve this?
If I reduce var into "UserInfo" to max 8 it works correctly. There's a limit into struct?
Can you share more details on exactly how you’re running this?

For context, I pasted your code into Playgrounds 3.4 running on my Mac (macOS 10.15.7) and it ran just fine.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
UserDefaults.standard return error EXCBADACCESS (code=1, address=0x38)
 
 
Q