Re: [swift-users] enum compare

2017-01-23 Thread Quinn "The Eskimo!" via swift-users

On 21 Jan 2017, at 12:56, tridiak via swift-users  wrote:

> How do I fix this? Simply cannot see it.

Use a switch statement:

switch type {
case .noType, .dodge, .other: return true
case .natural, .armour, .shield, .deflection, .sacred, .luck: return false
}

Note that I’ve spelt out all the cases so that the compiler warns me if I add 
or remove a case in the future.

Share and Enjoy
--
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] enum compare

2017-01-21 Thread Rien via swift-users
Oh, I am not using the correct terminology, sorry. Enum’s don’t have 
“instances” right?
Anyhow, I guess you get my drift…

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl




> On 21 Jan 2017, at 14:23, Rien  wrote:
> 
> The “==“ operation can only compare concrete instances of the enum.
> With “type == ACType.other” the right side is not an instance. as the 
> associated value is missing.
> 
> Regards,
> Rien
> 
> Site: http://balancingrock.nl
> Blog: http://swiftrien.blogspot.com
> Github: http://github.com/Swiftrien
> Project: http://swiftfire.nl
> 
> 
> 
> 
>> On 21 Jan 2017, at 13:56, tridiak via swift-users  
>> wrote:
>> 
>> Been searching and trying to figure out a comparison problem.
>> 
>> func ==(lhs : ACType, rhs : ACType) -> Bool {
>>  switch (lhs,rhs) {
>>  case (.other, .other):
>>  return true
>>  case (.other, _):
>>  return false
>>  case (_, .other):
>>  return false
>>  default:
>>  return lhs == rhs
>>  }
>>  }
>> 
>> enum ACType {
>>  case noType
>>  case natural
>>  case armour
>>  case shield
>>  case deflection
>>  case dodge
>>  case sacred
>>  case luck
>>  case other(String)
>> 
>>  static func Stacks(type : ACType) -> Bool {
>>  return type == ACType.noType || type == ACType.dodge || type == 
>> ACType.other
>>  }
>> }
>> 
>> I get an error concerning ‘type == ACType.other’.
>> Compiler blab: Binary operator '==' cannot be applied to operands of type 
>> 'ACType' and '(String) -> ACType'
>> 
>> How do I fix this? Simply cannot see it.
>> 
>> TIA Mark
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] enum compare

2017-01-21 Thread Rien via swift-users
The “==“ operation can only compare concrete instances of the enum.
With “type == ACType.other” the right side is not an instance. as the 
associated value is missing.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl




> On 21 Jan 2017, at 13:56, tridiak via swift-users  
> wrote:
> 
> Been searching and trying to figure out a comparison problem.
> 
> func ==(lhs : ACType, rhs : ACType) -> Bool {
>   switch (lhs,rhs) {
>   case (.other, .other):
>   return true
>   case (.other, _):
>   return false
>   case (_, .other):
>   return false
>   default:
>   return lhs == rhs
>   }
>   }
> 
> enum ACType {
>   case noType
>   case natural
>   case armour
>   case shield
>   case deflection
>   case dodge
>   case sacred
>   case luck
>   case other(String)
> 
>   static func Stacks(type : ACType) -> Bool {
>   return type == ACType.noType || type == ACType.dodge || type == 
> ACType.other
>   }
> }
> 
> I get an error concerning ‘type == ACType.other’.
> Compiler blab: Binary operator '==' cannot be applied to operands of type 
> 'ACType' and '(String) -> ACType'
> 
> How do I fix this? Simply cannot see it.
> 
> TIA Mark
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users