首页 > 解决方案 > 为什么我的快速初始化程序没有出现?

问题描述

我想打电话给我的func。这是代码:

class fnitekCo {
    var name = ""
    var contact = ""
    var location = ""
    var about = ""
    init(_ name:String) {
        self.name = name
    }
    func disPlay() {
        print("\(name)" )
        print("\(contact)")
        print ("\(location)")
        print ("\(about)")
    }
}

class Employee: fnitekCo {
    var owner = ""
    var manager = ""
    var salesRep = ""
    var branchRep = ""
    var role = ""
    override init (_ name: String) {
        super.init(name)
        self.role = "tecnician"
    }
    func disPlay2() {
        print("We have the finest representatives to work with you and find you the best solutions to for your IT services")      
        print("Owner: I'm \(owner), I think this world will be a better future technology")
        print("Im the manager and my name is \(manager), i oversea the branches")
        print("Im \(salesRep), and i recommend the best solutions for you")  
        print("We have a lot of branches located in \(location), \(branchRep) controls that aspect")
    }
}

class Solutions: Employee {
    var services = ""   
    func disPlay3() {
        print("We provide a range of services from Wifi routers, ethernet cables, gaming solutions, pc solutions based off your needs. This is the plan called \(services)")
    }
}

let f = fnitekCo()
f.name = "Fnitek"
f.about = "We find solutions to your needs"
f.contact = "airviewshots@gmail.com"
f.location = "Los angeles"
f.disPlay()
let e = Employee()
e.owner = "Cheebi Mere"
e.manager = "Chigo"
e.salesRep = "Santos"
e.branchRep =  "Chike"
e.disPlay2()
let s = Solutions()
s.services = "Fn1"
s.disPlay3()
//I'm trying to call these init function below
let meOwner = fnitekCo("tim")
print(meOwner.name)
let Employ = Employee("joe")
print (Employ.name)
print(Employ.role)

标签: iosswift

解决方案


没有称为Employee()(aka Employee.init()) 的初始化程序。你只提供了 Employee.init(_:). 它需要被调用的参数的String参数name(但没有标签,因此_

""更有问题的是,您正在尝试创建“空”实例(拥有一个拥有所有者为、其经理为""、角色为等的员工到底意味着什么""?这完全是胡说八道,但它是可能的模型),以及后来分配的值。这很容易出错。您应该创建设置所有字段的初始化程序,以保证对象的强不变性,并删除空字符串。

大致是这样的:

class FnitekCo { // What the heck is a FnitekCo???
    let name: String
    let contact: String?
    let location: String?
    let about: String?
    
    init(
        name: String,
        contact: String?,
        location: String?,
        about: String?
    ) {
        self.name = name
        self.contact = contact
        self.location = location
        self.about = about
    }
    
    func display() {
        print("\(name)" )
        // FIXME: Handle these optionals.
//      print("\(contact)")
//      print("\(location)")
//      print("\(about)")
    }
}

class Employee: FnitekCo {
    let owner: String // FIXME: An employee has an owner? That sounds like slavery to me...
    let manager: String
    let salesRep: String
    let branchRep: String
    let role: String
    
    init (
        name: String,
        contact: String?,
        location: String?,
        about: String?,
        owner: String,
        manager: String,
        salesRep: String,
        branchRep: String,
        role: String
    ) {
        self.owner = owner
        self.manager = manager
        self.salesRep = salesRep
        self.branchRep = branchRep
        self.role = role
        
        super.init(
            name: name,
            contact: contact,
            location: location,
            about: about
        )
    }
    
    func display2() {
        
        print ( "We have the finest representatives to work with you and find you the best solutions to for your IT services")
        
        print ("Owner: I'm \(owner), I think this world will be a better future technology")
        print("Im the manager and my name is \(manager), i oversea the branches")
        
        print("Im \(salesRep), and i recommend the best solutions for you")
        
        // FIXME: Handle these optionals.
//      print("We have a lot of branches located in \(location), \(branchRep) controls that aspect")
    }
    
}


class Solutions {
    let services: String
    
    init(services: String) {
        self.services = services
    }
    
    func display3() {
        print("We provide a range of services from Wifi routers, ethernet cables, gaming solutions, pc solutions based off your needs. This is the plan called \(services)")
    }
}




let f = FnitekCo(
    name: "Fnitek",
    contact: "airviewshots@gmail.com",
    location: "Los angeles",
    about: "We find solutions to your needs."
)

f.display()

let e = Employee(
    name: "I need a name", // FIXME
    contact: nil,
    location: nil,
    about: nil,
    owner: "Cheebi Mere",
    manager: "Chigo",
    salesRep: "Santos",
    branchRep: "Chike",
    role: "I need a role" // FIXME
)

e.display2()


let s = Solutions(services: "Fn1")

s.display3()


let meOwner = FnitekCo(
    name: "tim",
    contact: nil,
    location: nil,
    about: nil
)
print(meOwner.name)


let employee = Employee(
    name: "joe",
    contact: nil,
    location: nil,
    about: nil,
    owner: "GIVE ME A VALUE", // FIXME
    manager: "GIVE ME A VALUE", // FIXME
    salesRep: "GIVE ME A VALUE",
    branchRep: "GIVE ME A VALUE",
    role: "tecnician"
)

print(employee.name)
print(employee.role)

推荐阅读