1

Usually, I create a class's instance like this:

The Parent class:

@interface baseClass  
+ (instancetype)task;  
@end  
@implement baseClass  
+ (instancetype)task {  
    return [[[self class] alloc] init];  
}  
@end  

and then in children class:

@interface childClass : baseClass  
@end  
@implement childClass  
@end  

Finally, I can create a instance using:

childClass *pChild = [childClass task];

How could I implement this feature by using the Swift programming language?

In another word, how could I implement the [[[self class] alloc] init] in swift way?

imeteora
  • 13
  • 4

4 Answers4

0

I feel a bit insecure about you tagged your question as a singleton but basically the code you presented has nothing to do with singletons at all, so I tried to mimic your original classes and show a few options here.

these are written in Swift 2.2.


case #1

that works flawlessly initially:

class BaseClass {

    class func task() -> BaseClass {
        return BaseClass()
    }

    // ...

}

class ChildClass : BaseClass {

    override class func task() -> ChildClass {
        return ChildClass()
    }

    // ...

}

then you would be able to get instances like this:

let bTask: BaseClass = BaseClass.task()
let cTask: ChildClass = ChildClass.task()

case #2

you can also do something like this, if such thing looks more reasonable for you:

class BaseClass {
    
    class var task: AnyObject {
        get {
            return self.init()
        }
    }
    
    required init() {
        
    }
    
    // ...

}

class ChildClass : BaseClass {
    
    // ...

}

then you can instantiate your classes like:

let bTask: BaseClass = BaseClass.task as! BaseClass
let cTask: ChildClass = ChildClass.task as! ChildClass

case #3

here is an other option for you too, if you are not happy with any of the ideas above:

class BaseClass {
    
    class func task() -> Self {
        return self.init()
    }
    
    required init() {
        
    }
    
    // ...

}

class ChildClass : BaseClass {
    
    // ...

}

the instantiation is similar to the first case:

let bTask: BaseClass = BaseClass.task()
let cTask: ChildClass = ChildClass.task()

NOTE: you may need to refine the chosen concept for your final code if you want to deal with real singletons, these examples are not perfectly worked out as I mentioned at the beginning but they show you a few options you can have and you can use as template.

Community
  • 1
  • 1
holex
  • 23,961
  • 7
  • 62
  • 76
  • Thanks a lot! The **Case #3** is works for me. Thanks a again! I think about that the Swift has many misunderstand syntax. So I had been confusing in many times. – imeteora Jun 29 '16 at 09:31
  • @imeteora, not a problem, I will leave the other two options here as well; who knows, maybe someone could use them in future. – holex Jun 29 '16 at 09:33
0

If AClass not subclass of NSObject

class AClass {
    required init() {

    }
    class func task() -> Self {
        return self.init()
    }
}

class BClass : AClass {

}

let x = BClass.task()

You need implement init() and add required keyword, because if want subclass BClass or other class which inherited from AClass can use task, you have to ensure subclass can use init();

Otherwise you need add final keyword:

final class AClass {
    init() {

    }
    class func task() -> Self {
        return self.init()
    }
}

but i think you don't need it.

And if AClass is inherited from NSObject, you need override init() and add required too:

class AClass : NSObject {
    required override init() {

    }
    class func task() -> Self {
        return self.init()
    }
}

class BClass : AClass {

}
maquannene
  • 2,257
  • 1
  • 12
  • 10
-1

At Swift (add class before every func):

class baseClass: NSObject {
    class func task() {

    }
}

class childClass: baseClass {

}

let test = childClass.task()
Altimir Antonov
  • 4,966
  • 4
  • 24
  • 26
-1
class var sharedInstance :CommonUtil {
    struct Singleton {
        static let instance = CommonUtil()
    }

    return Singleton.instance
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65