[swift-evolution] [Pitch] Set of AnyObject by reference
张国晔
cc941201 at me.com
Sun Jul 17 21:40:41 CDT 2016
> 在 2016年7月18日,02:08,Dmitri Gribenko <gribozavr at gmail.com> 写道:
>
> On Sun, Jul 17, 2016 at 2:00 AM, 张国晔 via swift-evolution
> <swift-evolution at swift.org> 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 <gribozavr at gmail.com>*/
Thanks, I haven't thought of using ObjectIdentifier instead of unsafeAddress(of:). That's a lot better than my option 2.
- Guoye Zhang
More information about the swift-evolution
mailing list