How to Check If AttributedString isEmpty in SwiftUI

117 Views Asked by At

I have private var remark: AttributedString?, I want to check is remark is notEmpty then Show the View.

While debugging, I'm getting this

(lldb) po remark
▿ Optional<AttributedString>
  ▿ some :      {
    NSStrokeColor = kCGColorSpaceModelRGB 0 0 0 1 
    NSStrokeWidth = 0.0
    NSFont = <UICTFont: 0x7ff5bb43d310> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 12.00pt
    NSColor = kCGColorSpaceModelRGB 0 0 0 1 
    SwiftUI.Font = Font(provider: SwiftUI.(unknown context at $1248d26b0).FontBox<SwiftUI.Font.(unknown context at $12492b8dc).PlatformFontProvider>)
    NSKern = 0.0
    NSParagraphStyle = Alignment Natural, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode WordWrapping, Tabs (
), DefaultTabInterval 36, Blocks (
), Lists (
), BaseWritingDirection LeftToRight, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''
}
    ▿ _guts : <Guts: 0x600002446e80>

Want to check if remark: AttributedString? is empty or not?

1

There are 1 best solutions below

3
Kushagra On

You can check the number of characters in the AttributedString. So in your case, to check if the remark is not empty, do this:

if !remark.characters.isEmpty {
  // show your view
}