UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

core data enum to string impossible

Forums > SwiftUI

Hi, i am trying to show enum in Text field and i am absolutely Frustrated. Please can you help me? Compilation is ok but from simulator i got error: Thread 1: "-[Device category]: unrecognized selector sent to instance 0x600003f9b6b0"

this is my definition of category in viewmodel:

@NSManaged public var category: String

var deviceCategory: Category {
    set {
        self.category = newValue.rawValue

    }
    get {
        Category(rawValue: category) ?? .iphone
    }
}

enum Category: String {
case iphone = "iphone"
case macbook = "macbook"
case ipad = "ipad"
case appleWatch = "appleWatch"

}

this is my code in view

HStack { Text("Device category: ") Spacer() Text(device.deviceCategory.rawValue) }

thank you

2      

You probably want device.deviceDescription.description

also, you don't need to use:

case iphone = "iphone"

Because you can just put this in the enum:

  var description: String {
      return "\(self)"
  }

and then you can just use:

enum Category: String {
    case iphone
    case macbook
    case ipad
    case appleWatch 

      var description: String {
      return "\(self)"
  }
}

2      

i made an edit accordingly to you but still the same error. Please chech my implementation. Compilation is ok but when i try to call Text error accured

@NSManaged public var category: String?

var wrappedCategory: Category {
    set {
        self.category = newValue.rawValue

    }
    get {
        Category(rawValue: category!) ?? .iphone
    }
}

enum Category: String {
case iphone
case macbook
case ipad
case appleWatch

var description: String {
    return "\(self)"
}

}

HStack { Text("Device battery health: ") Spacer() Text(device.batteryHealth.description) }

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.