来自AnyObject的Swift,Self [英] Swift, Self from AnyObject

查看:81
本文介绍了来自AnyObject的Swift,Self的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从AnyObject获取Self?

Is it possible to get Self from AnyObject?

以此为例:

// Superclass
class ManagedObject {
  class func findByID(id: String) -> AnyObject? {
    let objects = objectsWithPredicate(NSPredicate(format: "id == %@", id))
    return objects.firstObject() // Returns AnyObject
  }
}

// Subclass
class User : ManagedObject {
  class func returnFirstSelf() -> Self? {
    return findById("1") // This doesn't work because it returns AnyObject, but I need Self.
  }
}

如果没有,最好的替代方法是什么?当调用 User.returnFirstSelf()时,编译器返回用户,并在调用 UserSubclass.returnFirstSelf(),它返回 UserSubclass

If not, what is the best alternative way to ensure that when calling User.returnFirstSelf(), the compiler gives back a User, and when calling UserSubclass.returnFirstSelf(), it gives back a UserSubclass.

推荐答案

如果这是一个选项,您只需从您的班级函数返回用户?

You can simply return User? from your class function, if this is an option:

public class func returnFirstSelf() -> User? {
    if let found = findByID("1") as? User {
        return found
    }
    return nil
}

目前无法(我知道)使用Swift返回 Self?。问题是 Self 有一些......动态含义,与具体类型,协议甚至泛型分开。演示这一点的一个特定示例是:如果您的类 StudentUser 扩展用户,该怎么办?如果你试图像这样实现它:

There's currently no way (I'm aware of) to return Self? with Swift as it stands. The problem is that Self has a somewhat... "dynamic" meaning, separate from concrete types, protocols, and even generics. A particular example that demonstrates this is: What if you have a class StudentUser that extends User? If you tried to implement it like this:

class func returnFirstSelf() -> Self? {
    if let found = findById("1") as? Self { // Note the check that 'found' is a 'Self'
        return found
    }
    return nil
}

然后遇到编译器错误,因为你不能在协议或类方法的结果之外使用 Self 。如果你试图像这样实现它:

Then you encounter a compiler error because you cannot use Self outside the result of a protocol or class method. And if you try to implement it like this:

class func returnFirstSelf() -> Self? {
    if let found = findById("1") as? User { // Note 'User' instead of 'Self'
        return found
    }
    return nil
}

然后你冒着 Self 的风险实际意味着 StudentUser ,即使您将需要找到的支票通过用户,也不能保证找到将是 StudentUser 。如果 StudentUser 未覆盖该方法以确保为?检查<$ c,则会发生这种情况$ C> StudentUser 。

Then you run the risk of Self actually meaning StudentUser, and even if you pass the check that requires found to be a User, it doesn't guarantee that found will be a StudentUser. This will occur in the case that StudentUser does not override the method to ensure that the as? checks against StudentUser.

我认为这里的关键缺陷是你不能使用 required 关键字在类方法上,要求子类覆盖它们。这将允许编译器确保任何子类重写了该方法,并提供了一个可以保证类型安全的实现。

The critical flaw here in my opinion is that you cannot use the required keyword on class methods, requiring subclasses to override them. This would allow the compiler to ensure that any subclasses have overridden the method and provided an implementation that can guarantee type safety.

这篇关于来自AnyObject的Swift,Self的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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