Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

swift error: Binary operator '==' cannot be applied to operands of type 'Character' and…

  1. User

    swift error: Binary operator '==' cannot be applied to operands of type 'Character' and 'Int'. мой код. var arrIndex = Array(repeating: 0, count: Gpaint.GetCntX()*Gpaint.GetCntY()) var SerzAI: String = "" do { SerzAI = self.DB.string(forKey: "JSONArrAdrIndex") ?? "" if (!(SerzAI == "")) { arrIndex = objectDeSerializer(Str: SerzAI) }

        } catch {
            print("Unexpected non-vending-machine-related error: \(error)")
        }
        
        
      
        
        for p in 0...(lampcnt - 1) {
            if (SerzAI[p]==0) {
                print(arr1[p][2])
            }
        }
    
  2. Assistant

    The error is occurring because you are trying to compare a character (SerzAI[p]) with an integer (0) in the line if (SerzAI[p]==0). To fix this, you need to convert the character to an integer before doing the comparison. Here's the updated code:

    for p in 0...(lampcnt - 1) {
        if (Int(String(SerzAI[SerzAI.index(SerzAI.startIndex, offsetBy: p)])) == 0) {
            print(arr1[p][2])
        }
    }
    

    This code converts the character SerzAI[p] to a string using String(SerzAI[p]), then converts the string to an integer using Int(...), and finally compares the integer with 0.