> 在 2016年7月18日,02:08,Dmitri Gribenko <[email protected]> 写道:
> 
> On Sun, Jul 17, 2016 at 2:00 AM, 张国晔 via swift-evolution
> <[email protected]> wrote:
>> I'm building an document-based app, and I'm having a problem regarding to 
>> Set.
>> 
>> What I hope to achieve is to have a Set of unique objects by their 
>> references.
> 
> Try this:
> 
> public final class ReferenceEqualityBox<Wrapped : Equatable> : Hashable {
>  public var value: Wrapped
>  public init(_ value: Wrapped) {
>    self.value = value
>  }
>  public var hashValue: Int {
>    return ObjectIdentifier(self).hashValue
>  }
> }
> public func == <Wrapped> (
>  lhs: ReferenceEqualityBox<Wrapped>,
>  rhs: ReferenceEqualityBox<Wrapped>
> ) -> Bool {
>  return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
> }
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <[email protected]>*/

Thanks, I haven't thought of using ObjectIdentifier instead of 
unsafeAddress(of:). That's a lot better than my option 2.

- Guoye Zhang
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to