Questions tagged [identifiable]

Swift protocol used to uniquely identify instances.

Identifiable is a Swift protocol used to uniquely identify instances. You must implement the id computed-property to satisfy the protocol requirements.

Apple's description:

A class of types whose instances hold the value of an entity with stable identity.

Commonly used in conjunction with SwiftUI's ForEach.

37 questions
15
votes
3 answers

SwiftUI - Custom Identifier with Identifiable Protocol

I'm wondering whether it's possible to override the standard identifier with a custom one. I've a simple struct: struct MyUserData: Identifiable { var userId: String var userFirstName: String var userLastName: String } However, the…
SwiftUIRookie
  • 591
  • 7
  • 16
6
votes
2 answers

How to implement Identifiable using two enum variables

Using Swift 5.3, how can I implement the Identifiable protocol on a struct by having its identity depend on the combination of two enum variables? The code in question is simple, struct Card: Identifiable { let suit: Suit let rank: Rank …
Isaiah
  • 1,852
  • 4
  • 23
  • 48
6
votes
1 answer

Access property of parent struct in a nested Codable struct when decoding the child

When using a decoder in a nested Codable struct, is there any way to access a property of a parent struct? The only way I can think of that might work (haven't tested yet) is to use a manual decoder in the parent struct too, set the property in the…
Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
5
votes
1 answer

How to make my struct Identifiable? It has a unique numeric uid field

In a simple project at Github I am trying to download a list of JSON objects: struct TopResponse: Codable { let data: [Top] } struct Top: Codable /*, Identifiable */ { let uid: Int let elo: Int let given: String let photo:…
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
4
votes
1 answer

Swift Conform to Identifiable with existing property

I need to ForEach an array of structs, and so they each must conform to the Identifiable protocol. But since these structs are decoded from fetched JSON's, they already have an id property--the id used in my database. Should I give them another UUID…
John Sorensen
  • 710
  • 6
  • 29
3
votes
2 answers

Conforming an array to Identifiable in SwiftUI

I have a little question about conforming to Identifiable in SwiftUI. There are situations where we are required to have a given type MyType to conform to Identifiable. But I am facing a case where I am required to have [MyType] (an array of MyType)…
Michel
  • 10,303
  • 17
  • 82
  • 179
3
votes
1 answer

Identifiable protocol in Swift: class vs struct

I'm using Swift 5.3 Trying to understand why when I declare this construction final class MyActivity: Identifiable { public let iHaveNoId: String = "" } it compiles without any errors (even if I don't have "id" field implemented), while for…
interrupt
  • 2,030
  • 17
  • 14
2
votes
2 answers

Swift enum conformance to identifiable: Type doesn't conform to Identifiable protocol

I have an enum with associated values, which I want to use as an item in RxDataSources. I tried conforming it to identifiable by conforming it to Hashable like below enum DriverHubWidget: Hashable, Identifiable { static func == (lhs:…
1
vote
2 answers

Swift: Initializer 'init(_:rowContent:)' requires that 'HealthStat' conform to 'Identifiable'

I'm receiving an error from Xcode that my List: List(viewModel.stats) { stat in VStack(alignment: .leading) { Text(viewModel.value(from: stat.stat).desc) Text(stat.date, style: .date).opacity(0.5) } Is…
Dezkiir
  • 19
  • 4
1
vote
1 answer

Trouble Decoding JSON Data with Swift

Trying to get a little practice in decoding JSON data, and I am having a problem. I know the URL is valid, but for some reason my decoder keeps throwing an error. Below is my model struct, the JSON object I'm trying to decode, and my decoder. Model…
1
vote
2 answers

Collections of Protocol Types - protocol oriented programming

I want a collection of a protocol type: in this case I want a variable "party" to be an array of type GameCharacter so I can put all things that conform to GameCharacter inside it. the code below produces the following error: Protocol…
user14534957
  • 336
  • 3
  • 10
1
vote
1 answer

How do I overwrite an identifiable object by refrencing it with an ID in swiftui?

I've simplified my code down so it's easier to see what i'm trying to do, I am building a feed of ratings that are sourced from firebase documents of whomever I am "following" All that I want to do is whenever firebase says there is a new update to…
1
vote
0 answers

Swift Error Message. "Type of expression is ambiguous without more context."

I keep getting this error message and can't understand why. The object (struct) it is instantiating is and Identifiable struct. func parseJSON(_ dosageData: Data) -> DoseModel? { do { let json = try JSON(data: dosageData) …
AVSoftware
  • 35
  • 5
1
vote
1 answer

ForEach not working with Identifiable & id = UUID()

import SwiftUI struct TestStudentView: View { @StateObject var students = Students() @State private var name = "" @State private var numberOfSubjects = "" @State private var subjects = [Subjects](repeating: Subjects(name: "", grade:…
NotAPhoenix
  • 171
  • 4
  • 15
1
vote
1 answer

SwiftUI: I want to use the information in my array but I'm unable to find the right command

I'm very new to programming and I'm having trouble with the more complex aspects. I am trying to use a data set called playerCameCards and randomly add 5 of these to playerCards. I have managed to do that but I want to display name: of the last…
Mamco
  • 25
  • 3
1
2 3