0

Hi I have a problem with this function. If I try to set and read data 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.

Try to import this code inside playground to reproduce the issue

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)
RFComp
  • 1
  • 2
  • Replace `#function` with a string literal (for example `"info"`). And if all struct member have always a value remove all question marks in the structs. – vadian Jan 10 '21 at 15:45
  • Not works. I tried to replace "#function" but result is the same – RFComp Jan 10 '21 at 15:50
  • I cannot reproduce the error. – vadian Jan 10 '21 at 15:57
  • Check this : https://stackoverflow.com/a/45909055/14733292 – Raja Kishan Jan 10 '21 at 15:58
  • @RajaKishan I already tried but is the same – RFComp Jan 10 '21 at 16:04
  • If I use in struct only 8 variables works correctly – RFComp Jan 10 '21 at 16:06
  • There's currently (in Xcode 12.3 at least, as that's where I can reproduce this) a bug in Swift Playgrounds where structs with more than eight properties cannot be encoded/decoded using `Codable`. Try doing this in a real app instead of Playgrounds, or try removing all but 8 properties in your `UserInfo` struct. https://stackoverflow.com/questions/64598849/potential-bug-with-json-decoder-swift https://developer.apple.com/forums/thread/665159 – TylerP Jan 10 '21 at 16:06

0 Answers0