Home > Mobile >  Check for a string in a debug message in Swift
Check for a string in a debug message in Swift

Time:05-30

I have a View with a view.backgroundColor. When I print the value to the debug-log i get

<UIImage:0x283b31b00 named(main: bg_200_200) {200, 200} renderingMode=automatic>

Is it possible to check this "String" (I know it's not a real string) for an occurence of a text, e.g. if it contains "bg_200"? Thanks!

CodePudding user response:

UI is always generated from some data model. Instead of checking the values inside UI, it's better to test the values in data model that were used to generate the UI.

This is especially obvious in SwiftUI which is always data driven and is always generated again when data model changes.

In UIKit this might be less obvious - for example, when you are changing background from an action, e.g.:

@IBAction func onButtonClicked() {
   myView.backgroundColor = ...
}

If you need to access that value later, it's always better to store it inside data model and then use that value to change the UI, e.g.:

private var buttonWasClicked = false

@IBAction func onButotnClicked() {
   buttonWasClicked = true
   updateBackground()
}

private func updateBackground() {
   myView.backgroundColor = buttonWasClicked ? UIColor.red : UIColor.clear
}
  • Related