-2

This is my code

func enteredByStr(S:String) -> String? {
    var healthactionplan : MyHealthActionPlanCondition

    if healthactionplan.addedBy != nil  {
        healthactionplan.addedBy = "System"
    } else {
        healthactionplan.addedBy = "Member"
    }
}

I have even followed this This and This. But still remains Unsolvable.Can anyone explain Why???

Community
  • 1
  • 1
Niroj
  • 1,114
  • 6
  • 29

3 Answers3

5

Value of Type String can never be nil,Comparison isn't allowed

Error message is showing you everything

addedBy is a String. so check isEmpty on addedBy

 if healthactionplan.addedBy.isEmpty   {
    healthactionplan.addedBy = "System"
} else {
    healthactionplan.addedBy = "Member"
}
Sahil
  • 9,096
  • 3
  • 25
  • 29
4

I think healthactionplan.addedBy is String as error said Value of Type String can never be nil that means you can't compare String with nil .. so try to check that string is empty or not

 if healthactionplan.addedBy.isEmpty { 
      healthactionplan.addedBy = "Member"
 } else {
      healthactionplan.addedBy = "System"
 }
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
0

In my example the string cannot be nil. I need to declare a string which can accept nil. So i have declared optional string:

var addedBy: String? = String()

After this i am able to check the nil value for the string.

Niroj
  • 1,114
  • 6
  • 29