Questions tagged [customstringconvertible]

16 questions
125
votes
4 answers

Swift equivalent of Java toString()

What is the Swift equivalent of Java toString() to print the state of a class instance?
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
6
votes
1 answer

Why is accessing CustomStringConvertible’s description discouraged?

Apple’s docs for CustomStringConvertible say: Accessing a type’s description property directly […] is discouraged. Why? Below is an example where using description seems useful to me. How would I get the same results otherwise? func…
1
vote
1 answer

Is it possible to change Swift String Interpolation for the String typealias?

I have a lot of DTOs in my app which log some field. That field should not be logged because the data is kind of sensitive. The model looks like this: typealias HiddenFieldType = String struct DTO1 { var field1_1: String var fieldToHide:…
olha
  • 2,132
  • 1
  • 18
  • 39
1
vote
1 answer

How to make a reusable view accept generic types

I created a reusable control to be used in a project I'm working on. It's just a UITextField which shows a UIPickerView as its inputView. class InputPickerView: UIView { @IBOutlet private var view: UIView! @IBOutlet weak private var…
Isuru
  • 30,617
  • 60
  • 187
  • 303
1
vote
0 answers

Automatically generate a CustomStringConvertible implementation for any Object

I have to write a lot of code like this: extension MyData: CustomStringConvertible { public var description: String { return "x: \(x), y: \(y), z: \(z)" } } Is there a good way to automatically synthesise this repetitive code?
scrrr
  • 5,135
  • 7
  • 42
  • 52
1
vote
3 answers

When and why would i use Protocols in Swift?

So I came across the subject of protocols and I have searched the internet a bunch for an answer but I couldn't find one, atleast one that solved my problem. So I understand that Protocols are a "blueprint" of methods, properties and such and that…
1
vote
2 answers

Is there a way to simplify this 'matrix of overloads' based on argument types which are all ultimately representable by a specific type?

We're trying to create a function addQueryItem which ultimately uses a string and an optional string internally. For more flexibility in the API, rather than use String for the argument types, we are instead using CustomStringConvertible (which…
1
vote
2 answers

Using CustomStringConvertible in UITableView

I've declared the following: class Song: CustomStringConvertible { let title: String let artist: String init(title: String, artist: String) { self.title = title self.artist = artist } var description: String { …
Jacob Cavin
  • 2,169
  • 3
  • 19
  • 47
1
vote
1 answer

Why CustomStringConvertible protocol description being called multiple times?

I have written a struct in iOS playground and want to customize the print format of it. struct Point { let x: Int, y: Int } extension Point: CustomStringConvertible { var description: String { switch (x, y) { case let (x,…
shoujs
  • 1,113
  • 1
  • 11
  • 24
0
votes
3 answers

Swift iOS Formatting data from JSON codable / API with CustomStringConvertible

I have some JSON that I would like to reformat before use, preferably in an initializer (or extension ??) [ { "name": "Diesel", "id": "1", "maj": "2022-07-06 18:28:29", "value": "2.81" }, { "name": "SP95", "id": "5", "maj": "2022-07-06…
jat
  • 183
  • 3
  • 14
0
votes
1 answer

Swift equivalent of Haskell Show

What is the Swift equivalent of Haskell Show to print values inside Enumerations with cases? I have read Show is pretty similar to the Java toString() method and that the Swift CustomStringConvertible maybe a good option. For example, using print on…
stcol
  • 131
  • 1
  • 11
0
votes
0 answers

as.POSIXct sometimes could not convert

Please see the following example: some values could not be translated by as.POSIXct as.POSIXct("2019-03-21T07:51:41.521Z") as.POSIXct("2019-03-22T05:35:47.824Z") The first is working, however, the second one did not work what is the difference!
MeY
  • 1
0
votes
1 answer

How to resolve overlapping conformances of CustomStringConvertible

Based on this article by John Sundell I have following struct: protocol Identifiable { associatedtype RawIdentifier: Codable, Hashable = String var id: Identifier { get } } struct Identifier: Hashable { let…
Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
0
votes
1 answer

Override URL description

Originally I tried to use something like this: extension URL: CustomStringConvertible{ public override var description: String { let url = self return url.path.removingPercentEncoding ?? "" } } After fixing compiler…
Shmidt
  • 16,436
  • 18
  • 88
  • 136
0
votes
1 answer

CustomStringConvertible in enum

I have following enum in a class. enum Attributes: String, CustomStringConvertible { case eventDate case eventName case eventType case country var description: String { return self.rawValue } } When I try have the…
Ham Dong Kyun
  • 756
  • 4
  • 28
1
2