类函数可以返回类本身吗?

时间:2018-12-29 08:04:34

标签: swift

类函数可以返回类本身吗?那么currentUser函数的作用是什么?

class FUser: FObject {
     class func currentUser() -> FUser {
       if let dictionary = UserDefaults.standard.object(forKey: "CurrentUser") as? [String: Any] {
          return FUser(path: "User", dictionary: dictionary)
       }
    return FUser(path: "User")
 }

class FObject: NSObject {
    init(path: String, subpath: String?) {
      super.init()
      pathX = path
      subpathX = subpath
   }

 convenience init(path: String) {
    self.init(path: path, subpath: nil)
   }
 }

2 个答案:

答案 0 :(得分:1)

如果您的意思是单例对象,那么这就是这样做的方法:

FATAL EXCEPTION: main
                  Process: com.*****.*******, PID: 2036
                  java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException: 

                  ******************************************************************************
                  * Invalid application ID. Follow instructions here: ***** *
                  * find your app ID.                                                          *
                  ******************************************************************************


                      at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
                      at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
                      at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5017)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.IllegalStateException: 

                  ******************************************************************************
                  * Invalid application ID. Follow instructions here: ***** to *
                  * find your app ID.                                                          *
                  ******************************************************************************


                      at com.google.android.gms.internal.ads.zzze.attachInfo(Unknown Source)
                      at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(Unknown Source)
                      at android.app.ActivityThread.installProvider(ActivityThread.java:4790)
                        ... 12 more

由于class ClassA { static let shared = ClassA() private init() { // Init } } 是私有的,因此只有一个实例。

答案 1 :(得分:0)

第一个可能的选择是您要创建静态工厂方法。是的,您可以这样做。只需返回FUser对象的 new 实例

class FUser: FObject {
    private override init() {...} // then make your init `private`
    class func currentUser() -> FUser {
        return FUser()
    }
}

或者您正在寻找类的 singleton 静态实例

class FUser: FObject {
    static var current = FUser()
}

然后您可以像这样使用它

FUser.current