Home > Mobile >  Conditional cast from 'String' to 'String' always succeeds- Swift
Conditional cast from 'String' to 'String' always succeeds- Swift

Time:02-22

I'm getting these errors: Error: Conditional cast from 'String' to 'String' always succeeds and Error: Cannot assign to value: 'anchorOffset' is a 'let'

 let text = getTextPart()

    if let text = text as? String {// Error: Conditional cast from 'String' to 'String' always succeeds
 let lastOffset = text.lengthAsNSString()
      var updatedAnchorOffset = lastOffset
      var updatedFocusOffset = lastOffset
      if var anchorOffset = anchorOffset {
        updatedAnchorOffset = lastOffset
    }
      if var focusOffset = focusOffset {
        updatedFocusOffset = lastOffset
      } else {
        anchorOffset = 0//Error: Cannot assign to value: 'anchorOffset' is a 'let' constant
        focusOffset = 0//Error: Cannot assign to value: 'focusOffset' is a 'let' constant
      }
}

This is my getTextPart():

override func getTextPart() -> String {
    guard let textNode = getLatest() as? TextNode else {
      return text
    }
    return textNode.text
  }

CodePudding user response:

Remove this line

if let text = text as? String {// Error: Conditional cast from 'String'

Change the definition of anchorOffset and focusOffset to

var anchorOffset = something

var focusOffset = something

  • Related