2016-11-03 2 views
1

사전의 값에 매핑하는 데 UIColor를 사용하고 있지만 정말 이상한 점이 있습니다. 내 키의 절반은 올바른 값을 반환하고 나머지 절반은 그렇지 않습니다. isEqual을 사용하여 UIColors를 비교할 때 false를 반환하지만 해시는 올바르게 일치합니다. 이 잘못 이유에UIColors를 비교하는 데 실패하지만 해시가 동일합니까?

144048128 ---> 151431738 
false 
155123712 ---> 151431738 
false 
147739933 ---> 151431738 
false 
151431738 ---> 151431738 <-------- EQUAL? 
false 

어떤 아이디어 :

for key in colorToAllocationCurrent.keys { 
      print("\(key.hash) ---> \(currentColor!.hash)") 
      print(key.isEqual(currentColor)) 
     } 

은 다음을 반환? 나는 UIColor의 내용을 확인했고, 그것들은 동일합니다.

해시 대신 색상 설명을 인쇄하면 색상이 다시 똑같이 나타납니다. 이상한 점은 색상의 반 정도가 작동한다는 것입니다.

for key in colorToAllocationCurrent.keys { 
      print("\(key.description) ---> \(currentColor!.description)") 
      print(key.isEqual(currentColor)) 
     } 

UIExtendedSRGBColorSpace 1 0 0 1 ---> UIExtendedSRGBColorSpace 1 0.666667 0 1 
false 
UIExtendedSRGBColorSpace 1 1 0 1 ---> UIExtendedSRGBColorSpace 1 0.666667 0 1 
false 
UIExtendedSRGBColorSpace 1 0.333333 0 1 ---> UIExtendedSRGBColorSpace 1 0.666667 0 1 
false 
UIExtendedSRGBColorSpace 1 0.666667 0 1 ---> UIExtendedSRGBColorSpace 1 0.666667 0 1 
false 
+0

색상이 실제로 동일합니까? 확실하지는 않지만 isEqual 함수는 해시가 아닌 색상 값을 검사 할 수 있습니다. – Yan

+0

그래, 인쇄 할 때 색상이 동일하게 보입니다. – Recusiwe

+0

해시가 동일하고 객체가 아닌 한이 기사를 검토하십시오. http://nshipster.com/equality/ "그러나 역방향은 보유하지 않습니다. 두 객체가 해시 값을 같게 만들 필요는 없습니다. is equal "isEqual이 false를 나타내는 색상을 인쇄 할 수 있습니까? – Yan

답변

1

내가이 일의 목적 꽤 잘 모르겠지만, 당신이 Equatable는 당신이 예를 들어, ==를 사용하여 UIColor 인스턴스의 평등을 확인 할 수 있음을 의미 UIColor에 의해 채택 않음을주의해야한다 :

let col1 = UIColor.red 
let col2 = UIColor.red 

// the output is "matched" 
print(col1 == col2 ? "matched" : "no match") 

let customCol1 = UIColor(colorLiteralRed: 123.0/255.0, green: 243.0/255.0, blue: 46.0/255.0, alpha: 0.9) 
let customCol2 = UIColor(colorLiteralRed: 123.0/255.0, green: 243.0/255.0, blue: 46.0/255.0, alpha: 0.9) 

// the output is "matched" 
print(customCol1 == customCol2 ? "matched" : "no match") 

let customCol3 = UIColor(colorLiteralRed: 123.0/255.0, green: 243.0/255.0, blue: 46.0/255.0, alpha: 0.9) 
let customCol4 = UIColor(colorLiteralRed: 123.0/255.0, green: 243.0/255.0, blue: 46.0/255.0, alpha: 1.0) 

// the output is "no match" 
print(customCol3 == customCol4 ? "matched" : "no match") 

희망이있었습니다.

관련 문제