How to call a class method in a convenience initializer in Swift - swift

I am trying to consume an API where every object names its ID field differently. Example: Group.groupid, Team.teamid, etc.
I have a BaseAPIObject that has a required initializer that accepts a parsed JSON dictionary and a convenience initializer that takes just the ID field (the only required property of my class).
I've dealt with the changing id field names by adding a static aka "class" method that returns the ID field name and subclasses override that function to return their own field name.
The problem I have is that in my base class' convenience initializer I can't call self.dynamicType before I've called self.init() but I need the results of that static class function before I can properly construct my object.
public class BaseAPIObject {
var apiID: String!
var name: String?
var createdBy: String?
var updatedBy: String?
//Construct from JSONSerialization Dictionary
required public init(_ data: [String: AnyObject]) {
name = data["name"] as String?
createdBy = data["created_by"] as String?
updatedBy = data["updated_by"] as String?
super.init()
let idName = self.dynamicType.idFieldName()
apiID = data[idName] as String?
}
/// Creates an empty shell object with only the apiID set.
/// Useful for when you have to chase down a nested object structure
public convenience init(id: String) {
// THIS is what breaks! I can't call self.dynamicType here
// How else can I call the STATIC CLASS method?
// in ObjC this would be as simple as self.class.getIdFieldName()
let idFieldName = self.dynamicType.getIdFieldName()
let data = [idFieldName: id]
self.init(data)
}
//This gets overridden by subclasses to return "groupid" or whatever
class func idFieldName() -> String {
return "id"
}
}
Question: How can I solve the problem of calling a subclass' class function before I run init on the instance itself?

Instead of creating a class function for figuring out the id, create init functions instead. Since you already have to create one of these functions per subclass, you're not really losing anything. The subclasses init function then calls the super's init with the id name.
Here's an example, I changed some of the properties of your group just for the sake of making the example simple to illustrate the concept.
public class BaseAPIObject {
var objData: [String:String]
required public init(_ data: [String: String]) {
println("Data: \(data)")
self.objData = data
}
public convenience init(id: String, idFieldName: String) {
let data = [idFieldName: id]
self.init(data)
}
}
And then in your subclass, just conceptually something like this:
public class GroupObject: BaseAPIObject {
public convenience init (id: String) {
self.init(id: id, idFieldName: "group")
}
}
let go = GroupObject(id: "foo")
println(go.objData["group"]!) //prints "foo"

Related

Generating auto-incrementing Instance IDs in Swift using Protocols and protocol-extensions only

Goal
To create an "AutoIDable" protocol with the following behaviour.
Every instance of a class conforming to this protocol will get an auto-generated "id" property of String type.
The code should generate id strings in the format <prefix><Instance-count-starting-from-1> (Eg: E-1, E-2, ...E-<n> and so on for 1st , 2nd ... nth Instance of the conforming class.
The protocol & protocol extensions should do ALL of the required work to generate the id strings. The conforming class will only have to subscribe to the protocol and nothing more.
Current status:
I have achieved Goal-1 & Goal-2 with the following implementation:
protocol Identifiable {
var id: String { get }
}
protocol AutoIDable: Identifiable{
static var _instanceCount: Int { get set }
}
class AutoID: AutoIDable {
init(idPrefix: String) {
setAutoID(prefix: idPrefix)
}
internal static var _instanceCount: Int = 0
var id: String = ""
func setAutoID(prefix: String = ""){
Self._instanceCount += 1
self.id = "\(prefix)\(Self._instanceCount)"
}
}
class Employee: AutoID {
init(){
super.init(idPrefix: "E-")
}
}
let e1 = Employee()
let e2 = Employee()
let e3 = Employee()
print(e1.id)
print(e2.id)
print(e3.id)
print(e1.id)
The output from running the above code:
E-1
E-2
E-3
E-1
Todo:
To achieve Goal-3, I need to eliminate the AutoID superclass and implement the same functionality using protocol extensions.
I ran into trouble because:
Protocol extensions do not allow static stored properties. I do know how to work around this limitation without using a superclass.
I do not know how to inject code into all the initialisers the creator of the Employee class might create. Again, I could not think of a workaround without using a superclass.
I would be grateful if you can point me in the right direction.
PS: New to Swift programming. If you’ve suggestions for implementing the code in a more “swifty” way, please do let me know. :-)
Since you want to use protocols, you can't have a stored property in the protocol. So, you'll need some place to store the incrementing ID value, if not the IDs themselves.
Not sure if it violates your requirements of using only protocols, because it would require a type for storage, but at least it won't require conforming classes to have a superclass.
So, let's say we build such a class that holds all the IDs and keeps the incrementing counter:
class AutoIncrementId {
static private var inc: Int = 0
static private var ids: [ObjectIdentifier: String] = [:]
static func getId(_ objectId: ObjectIdentifier, prefix: String) -> String {
if let id = ids[objectId] { return id }
else {
inc += 1
let id = "\(prefix)\(inc)"
ids[objectId] = id
return id
}
}
}
Then the protocol requirement could be:
protocol AutoIdentifiable {
static var prefix: String { get }
var id: String { get }
}
So, a class would need to define its prefix. But we could define a default implementation for id:
extension AutoIdentifiable where Self: AnyObject {
var id: String {
AutoIncrementId.getId(ObjectIdentifier(self), prefix: Self.prefix)
}
}
The usage would be:
class Employee: AutoIdentifiable {
static let prefix = "E-"
}
let e1 = Employee()
let e2 = Employee()
let e3 = Employee()
print(e1.id) // E-1
print(e2.id) // E-2
print(e3.id) // E-3
print(e1.id) // E-1

Swift 5 subclassing from super with required init

I'm working through Swift apprentice 3rd Edition.
The book quickly skips through required initialisers.
If I have a super class :
class Animal {
let animalType: String
required init(animalType: String) {
self.animalType = animalType
}
convenience init(animal: Animal) {
self.init(animalType: animal.animalType)
}
}
and then subclass like:
class WildAnimal: Animal {
let name: String
required init(animalType: String) {
name = ""
super.init(animalType: animalType)
}
}
The only way I can initialise the subclass is to assign placeholder values in the required init. i.e. I had to put name = "".
Is there no way to initialise a subclasses parameters during initialisation if the parent class has a required init?
You can have more than one initializer. You just also have to "override" the required parent initializers.
class WildAnimal: Animal {
var name: String
required init(animalType: String) {
name = ""
super.init(animalType: animalType)
}
init(animalType: String, name: String) {
self.name = name
super.init(animalType: animalType)
}
}
Forcing subclasses to define de required initializers is the point of required.
Write the required modifier before the definition of a class
initializer to indicate that every subclass of the class must
implement that initializer
Source

self.name = name in Swift . I don't understand why this codes are needed

class person {
var name : String
init(name: String) {
self.name = name
}
}
I am learning Swift class chapter
I don't understand why init(name:String) self.name = name code is needed
what the purpose of this code is.
I declared var name: String
and again init(name: String), why is self.name = name needed?
what's different between just var name and self.name = name?
Look into something called variable scope. In your code - there are two "name variables - a class (or instance) variable and a "parameter in your init.
The latter - init(name:) only is in use within your initialization function, meaning the instance has no name without the self.name = name once you've created the instance.
The former is available - to the instance - as long as your instance of the class person is.
To explain further, try this. Change your code to be:
class Person {
var myName : String
var myOtherName : String
init(name: String) {
self.myName = name
}
}
In your app or view controller, do this:
let myPerson = Person(name: "john")
print(myPerson.myName) // prints "jihoon"
print(myPerson.myOtherName) // prints nothing
print(myPerson.name) // generates a build error because name doesn't exist
One last note - in Swift class names are capitalized, so the best name is Person, not person.
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
class person {
var name : String // undetrmined state
init(name: String) {
self.name = name
}
}
class person2 {
var name : String = "default value" // detrmined state
// Now these intializer are not optional. You can use these initializer or not.
init(name: String) {
self.name = name
}
init() {
}
}
// Aother example for optional stored property
class person3 {
var name : String? // detrmined state, because default value for optional is nil
// Now these intializer are not optional. You can use these initializer or not.
init(name: String) {
self.name = name
}
init() {
}
}
For more info read this one Apple Doc

How to instantiate an object with a given type [duplicate]

This question already has answers here:
Instantiate an object of a generic type in Swift
(4 answers)
Closed 6 years ago.
I would like to instantiate an Object with a given Type. Here's the call to the function:
buildObjetFromType("data", typeClass: User.self)
Here's the function code:
public static func buildObjectFromType<T>(data: String, typeClass: T.Type) -> T{
print(typeClass) // will print "User"
var object:T?
object as! T.Type // cast the object into an User -> NOT WORKING !
// fill the object with the data, knowing
// return the object once it has been filled with the data.
}
here's my User class:
public class User {
private var id:String?
private var login:String?
private var pwd:String?
private var nom:String?
private var prenom:String?
public required init(){
id = ""
login = ""
pwd = ""
nom = ""
prenom = ""
}
public convenience init(_login: String, _pwd: String){
self.init()
login = _login
pwd = _pwd
}
public convenience init(_id: String, _login: String, _pwd: String, _nom: String, _prenom: String){
self.init(_login: _login, _pwd: _pwd)
id = _id
nom = _nom
prenom = _prenom
}
}
See: Instantiate an object of a generic type in Swift
Assuming all the classes you want to instantiate have a required initializer with no argument (you may want them to inherit from the same base class, and add this requirement to your function), you could do the following:
static func instantiate<T where T: BaseClass>(data: String, cls: T.Type) -> T {
let obj = T()
// do something with obj and data
}

Convenience inits in class hierarchy cause infinite recursion

I have a hierarchy of 2 classes in Swift 2.0. Both classes can be instantiated by passing the arguments or by passing a JSON dictionary ([String: AnyObject]) which contains the arguments.
The inits that directly take the arguments are designated inits, while those that take the JSON are convenience inits with the same signature.
class Thing {
let name : String
init(name: String){
self.name = name
}
convenience init(jsonNamed: String){
// read the json, parse and extract
// the name
self.init(name: "got this from JSON")
}
}
class SubThing : Thing{
var surname : String
init(name: String, surname: String){
self.surname = surname
super.init(name: name)
}
convenience init(jsonNamed: String){
self.init(jsonNamed: "got this from JSON")
// extract surname
self.surname = "Got this from json"
}
}
The convenience init in SubThing is not allowed to call the same init in super, and if I call it in self, it will cause an infinite recursion, as both methods have the same signature.
If I make both json inits designated ones, I can't call self.init(name:) in Thing, and I would have to repeat the same code in both initialisers in Thing.
What's the best way to get around this kind of situation?
You're doing a loop in SubThing calling the same designated initializer from itself. I see it rewritten this way:
class Thing {
private var name : String?
private init() {}
convenience init(name: String){
self.init()
self.name = name
}
convenience init(jsonNamed: String){
self.init()
self.name = getNameFromJSON()
}
private func getNameFromJSON() -> String {
// read the json, parse and extract
// the name
return "got this from JSON"
}
}
class SubThing : Thing {
private var surname : String?
convenience init(name: String, surname: String){
self.init(name: name)
self.surname = surname
}
convenience init(jsonNamed: String){
self.init()
self.name = getNameFromJSON()
// extract surname
self.surname = "Got this from json"
}
}
Tested and working. UPDATE: added private inits, so it couldn't be initialized empty.