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

bug when initialising some model

Forums > Swift

Hey guys, here is my model:

// swiftlint:disable all
import Amplify
import Foundation

public struct ProductStored: Model {
  public let id: String
  public var barcode: String
  public var productName: String?
  public var category: String?
  public var brand: String?
  public var image: String?

  public init(id: String = UUID().uuidString,
      barcode: String,
      productName: String? = nil,
      category: String? = nil,
      brand: String? = nil,
      image: String? = nil) {
      self.id = id
      self.barcode = barcode
      self.productName = productName
      self.category = category
      self.brand = brand
      self.image = image
  }
}

When I try to create one:

 let producto = ProductStored(id: <#T##String#>,
                                     barcode: <#T##String#>,
                                     productName: <#T##String?#>,
                                     category: <#T##String?#>,
                                     brand: <#T##String?#>,
                                     image: <#T##String?#>)

It still asks me to provide id.. Why is that? I thought that id will assign UUID().uuidString straight away. What can I do with that?

Many thanks

2      

still learning mysef so cant recall now what is "best practice" / syntactically allowed but have you tried:

instead of self.id = id use: self.id =UUID()

if that doesnt work then you can pass in UUID() when you create your object instance

*btw I tend to use UUID type rather than string. Others may mot

2      

When I try your code, I get two options for initializers from Xcode's autocomplete, one of which only asks me for the only property without a default value:

autocomplete options

Do you not get both of these?

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.