我要实现的是:
class func getSomeObject() -> [SomeObject]? { let objects = Realm().objects(SomeObject) return objects.count > 0 ? objects : nil }
我怎样才能返回对象[SomeObject],而是如果Results?
[SomeObject]
Results
我找到了解决方案。在结果上创建了扩展名。
extension Results { func toArray<T>(ofType: T.Type) -> [T] { var array = [T]() for i in 0 ..< count { if let result = self[i] as? T { array.append(result) } } return array } }
并使用像
class func getSomeObject() -> [SomeObject]? { let objects = Realm().objects(SomeObject).toArray(SomeObject) as [SomeObject] return objects.count > 0 ? objects : nil }