是否有任何简单的内置方式来为任何应用程序中的对象生成唯一的标识符?(哈希是不可接受的,因为在我的情况下,有相同数据的对象是可以接受的) ID可以接受为字符串或整数,只需要本地唯一性。谷歌帮不上忙
发布于 2014-12-22 19:39:02
如果只需确定与==相同的两个对象是否不同,则可以使用===运算符:
let first = MyObject(value: 1)
let second = MyObject(value: 1)
first == second // returns true
first === second // returns false如果您真的想获得一个唯一的标识符,可以在Swift标准库中搜索:
/// Return an UnsafePointer to the storage used for `object`. There's
/// not much you can do with this other than use it to identify the
/// object
func unsafeAddressOf(object: AnyObject) -> UnsafePointer<Void>我不主张使用它的可取性。它甚至可能不像你所认为的那样工作,这取决于编译器所做的抄写恶作剧。
https://stackoverflow.com/questions/27609133
复制相似问题