如何使用泛型类型来获取具有相同类型的对象 [英] How to use generic types to get object with same type

查看:119
本文介绍了如何使用泛型类型来获取具有相同类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展名为 NSManagedObject ,它可以帮助我在上下文之间传递对象:

 扩展NSManagedObject {

func transferTo(#context:NSManagedObjectContext) - > NSManagedObject? {

return context.objectWithID(objectID)
}

}

现在它返回 NSManagedObject 的对象,我应该将它转换为我想要的类,如下所示:

  let someEntity:MyEntity = // ...创建someEntity 
让entity:MyEntity = someEntity.transferTo(context:newContext)as? MyEntity

有没有办法在 Swift 避免这种无用的转换,如果我从类 MyEntity 的对象调用 transferTo(context:...)使它返回键入 MyEntity 更新:更好的解决方案,请参阅 Rob's answer




类似于如何创建托管对象子类的实例一个NSManagedObject Swift扩展?
这可以用一个通用的帮助方法来完成:

 扩展NSManagedObject {

func transferTo(context context:NSManagedObjectContext) - > self {
return transferToHelper(context:context)
}

private func transferToHelper< T>(context context:NSManagedObjectContext) - > T {
返回context.objectWithID(objectID)as! T






$ b请注意,我已将返回类型更改为
objectWithID() does not 返回一个可选的
(与 objectRegisteredForID() / code>,所以没有必要
在这里返回一个可选项。



更新: Jean-Philippe Pellet建议的
来定义一个全局可重用函数,而不是辅助方法
来将返回值转换为



我建议定义两个(重载)版本,以使
与可选和非可选对象一起工作(没有不需要的b

  func objcast< T>(obj:AnyObject) - > T {b $ b自动包装成可选项) 
return obj as!T
}

func objcast< T>(obj:AnyObject?) - > T?{
return obj as!T?


扩展NSManagedObject {

func transferTo(context context:NSManagedObjectContext) - > Self {
let result = context.objectWithID(objectID)// NSManagedObject
return objcast(result)// Self
}

func transferUsingRegisteredID(context context:NSManagedObjectContext) - >自? {
let result = context.objectRegisteredForID(objectID)// NSManagedObject?
return objcast(result)// Self?




$ b我已经更新了Swift 2 / Xcode的代码7.早期
Swift版本的代码可以在编辑历史中找到。)


I have extension for NSManagedObject that should help me to transfer objects between contexts:

extension NSManagedObject {

    func transferTo(#context: NSManagedObjectContext) -> NSManagedObject? {

        return context.objectWithID(objectID)
    }

}

for now it return object of NSManagedObject and i should cast it to class what i want, like this:

let someEntity: MyEntity = // ...create someEntity
let entity: MyEntity = someEntity.transferTo(context: newContext) as? MyEntity

Is there a way in Swift to avoid that useless casting and if i call transferTo(context: ...) from object of class MyEntity make it return type to MyEntity?

解决方案

Update: For a better solution, see Rob's answer.


Similarly as in How can I create instances of managed object subclasses in a NSManagedObject Swift extension?, this can be done with a generic helper method:

extension NSManagedObject {

    func transferTo(context context: NSManagedObjectContext) -> Self {
        return transferToHelper(context: context)
    }

    private func transferToHelper<T>(context context: NSManagedObjectContext) -> T {
        return context.objectWithID(objectID) as! T
    }
}

Note that I have changed the return type to Self. objectWithID() does not return an optional (in contrast to objectRegisteredForID(), so there is no need to return an optional here.

Update: Jean-Philippe Pellet's suggested to define a global reusable function instead of the helper method to cast the return value to the appropriate type.

I would suggest to define two (overloaded) versions, to make this work with both optional and non-optional objects (without an unwanted automatic wrapping into an optional):

func objcast<T>(obj: AnyObject) -> T {
    return obj as! T
}

func objcast<T>(obj: AnyObject?) -> T? {
    return obj as! T?
}

extension NSManagedObject {

    func transferTo(context context: NSManagedObjectContext) -> Self {
        let result = context.objectWithID(objectID) // NSManagedObject
        return objcast(result) // Self
    }

    func transferUsingRegisteredID(context context: NSManagedObjectContext) -> Self? {
        let result = context.objectRegisteredForID(objectID) // NSManagedObject?
        return objcast(result) // Self?
    }
}

(I have updated the code for Swift 2/Xcode 7. The code for earlier Swift versions can be found in the edit history.)

这篇关于如何使用泛型类型来获取具有相同类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆