Extra argument in call when try to call convenience init, Swift - swift

I have simple class:
class WmAttendee{
var mEmail:String!
var mName:String!
var mType:Int!
var mStatus:String = "0"
var mRelationShip:String!
init( email:String, name:String, type:Int) {
self.mEmail = email
self.mName = name
self.mType = type
}
convenience init( email:String, name:String, type:Int, status:String, relationShip:String) {
self.init(email: email, name: name, type: type)
self.mStatus = status
self.mRelationShip = relationShip
}
}
When I try to test 2nd constructor with 5 parameters, I get: Extra argument 'status' in call
var att1 = WmAttendee(email: "myMail", name: "SomeName", type: 1); // OK
var att2 = WmAttendee(email: "mail2", name: "name2", type: 3, status: "2", relationShip: 3)
// ERROR Extra argument 'status' in call
Why? Do I miss something?
Thanks,

Based on your method signature:
convenience init( email:String, name:String, type:Int, status:String, relationShip:String)
relationshipStatus should be a String and not an Int:
var att2 = WmAttendee(email: "mail2", name: "name2", type: 3, status: "2", relationShip: "3")
Since you're not passing the correct type for relationshipStatus, the compiler can't match the method signature for your convenience init and falls back the the default init (the closest match it can find) which triggers the Extra argument error.

Your are passing a parameter of the wrong type to your function. 'RelationShip' must be of type String, but you are passing an Integer. Yes, the compiler error is misleading, but then again swift is still in beta.

Related

Understanding struct initialiser if it contain private property

I want to understand how initialiser work if struct contains private properties. I have following code:
struct Doctor {
var name: String
var location: String
private var currentPatient = "No one"
}
let drJones = Doctor(name: "Esther Jones", location: "Bristol")
This throws an error:
Cannot invoke initializer for type 'Doctor' with an argument list of
type '(name: String, location: String)'
My Assumption is: Default Memeberwise initialiser contains private property which can't be called from outside.
But I am confused by following code:
struct Doctor {
private var currentPatient = "No one"
}
let drJones = Doctor()
How this is working?, it is not throwing any error.
You can't use default memberwise initialiser for assigning struct's property with private access level modifier.
Your second example works, because you gave your property default value so there is no need to assign it while you're initalizing it.
If you need to assign your private property using initializer, you have to write your own
init(name: String, location: String, currentPatient: String) {
self.name = name
self.location = location
self.currentPatient = currentPatient
}

Swift Protocols: Difference between { get } and { get set } with concrete examples?

I'm pretty new to Swift, and although I've read Apple's documentation and many topics and threads about this, I still can't understand what's the difference between { get } and { get set }. I mean, I'm looking for an explanation with a concrete example.
Like, for example:
protocol PersonProtocol {
var firstName: String { get }
var lastName: String { get set }
}
What would be the actual difference between these two properties? I tried to play with these properties in a playground:
struct Person: PersonProtocol {
var firstName: String
var lastName: String
}
var p = Person(firstName: "John", lastName: "Lennon")
print(p.firstName) // John
print(p.lastName) // Lennon
p.firstName = "Paul"
p.lastName = "McCartney"
print(p.firstName) // Paul
print(p.lastName) // McCartney
Did not help... Thanks for your help.
You are creating a variable of type Person and there are no restrictions on that struct. If you instead create a variable of type PersonProtocol then firstName will be read only
var p1: PersonProtocol = Person(firstName: "John", lastName: "Lennon")
print(p1.firstName) // John
print(p1.lastName) // Lennon
p1.firstName = "Paul" <== error: cannot assign to property: 'firstName' is a get-only property
protocol — is a requirement of some minimal interface of the type implementing it.
var name: Type { get } requires type to have property with at least a getter (accessible from outside of the type, not private), i.e. outside code should be able to read value of the property. In the implementing type it could be let name: Type, var name: Type, private(set) var name: Type, fileprivate(set) var name: Type, etc.
var name: Type { get set } requires type to have property with both accessible getter and setter, i.e. outside code should be able to read and write to the property. Here only var name: Type would be allowed.
If protocol requires for getter but you also provide a setter — it's not against protocol requirements.
But if protocol requires for both getter and setter — you must provide both, and not having any of them won't be valid implementation.
Your Person class defined both properties as var(with accessible getter and setter) therefore you can change them both. But PersonProtocol haven't required ability to set firstName.
And as #JoakimDanielson shows, if you will use just interface required by protocol you won't be to change the firstName value.

swift 3.1.1 ERROR extra argument '{property}' in call

I was testing out inheritance in Swift 3 and ran into an unexpected error. No matter how I alter the super.init() call, I can't make the error trace less problematic than this.
I have read through the documentation and other similar posts here on SO, but they don't have any examples of this particular problem. I am trying to create a class with properties that are either variable or constant and then creating another class that inherits from the first class and adds new properties.
The error comes from my subclass's init function: I set the new properties first, then call super.init() with the superclass's appropriate arguments. Swift then tells me that the third parameter "birthday" is an "extra argument."
Is there some sort of problem between my superclass having three properties and my subclass having two? I can't think of any other problem that could throw an error like this. Not sure which part is confusing me.
Here's my code:
class Person {
var name: String
var age: Int
let birthday: String
init(name: String, age: Int, birthday: String) {
self.name = name
self.age = age
self.birthday = birthday
}
}
class Student: Person {
var isEnrolled: Bool
var numberOfClasses: Int
init(isEnrolled: Bool, numberOfClasses: Int) {
self.isEnrolled = isEnrolled
self.numberOfClasses = numberOfClasses
super.init(name: name, age: age, birthday: birthday) {
self.name = name
self.age = age
self.birthday = birthday
}
}
}
Your initializer does not have a closure, so naturally, your approach cannot work. It says birthday is an extra argument, because the (non-existent) closure is the fourth argument in the list.
Student's initializer does not know what name, age or birthday is. You should include those parameters in the initializer as well and call super.init at the end.
init(name: String, age: Int, birthday: String, isEnrolled: Bool, numberOfClasses: Int) {
self.isEnrolled = isEnrolled
self.numberOfClasses = numberOfClasses
super.init(name: name, age: age, birthday: birthday)
}

Literal Convertibles in Swift

I want to know how Literal Convertibles work in Swift. The little I know is that the fact that, in var myInteger = 5, myInteger magically becomes an Int is because Int adopts a protocol, ExpressibleByIntegerLiteral and we don't have to do var myInteger = Int(5). Similarly String, Array, Dictionary etc all conform to some Literal protocols.
My Question is
Am I right in my little understanding of Literal Convertibles?
How can we implement these in our own types. For example
class Employee {
var name: String
var salary: Int
// rest of class functionality ...
}
How can I implement Literal Protocols to do var employee :Employee = "John Doe" which will automatically assign "John Doe" to employee's name property.
You are partially correct in your understanding of the various ExpressibleBy...Literal protocols. When the Swift compiler parses your source code into an Abstract Syntax Tree, it already identified what literal represents what data type: 5 is a literal of type Int, ["name": "John"] is a literal of type Dictionary, etc. Apple makes the base type conform to these protocols for the sake of completeness.
You can adopt these protocols to give your class an opportunity to be initialized from a compile-time constant. But the use case is pretty narrow and I don't see how it applies to your particular situation.
For example, if you want to make your class conform to ExpressibleByStringLiteral, add an initializer to set all your properties from a String:
class Employee: ExpressibleByStringLiteral {
typealias StringLiteralType = String
var name: String
var salary: Int
required init(stringLiteral value: StringLiteralType) {
let components = value.components(separatedBy: "|")
self.name = components[0]
self.salary = Int(components[1])!
}
}
Then you can init your class like this:
let employee1: Employee = "John Smith|50000"
But if you dream about about writing something like this, it's not allowed:
let str = "Jane Doe|60000"
let employee2: Employee = str // error
And if you pass in the wrong data type for salary, it will be a run time error instead of a compile-time error:
let employee3: Employee = "Michael Davis|x" // you won't know this until you run the app
TL, DR: it is a very bad idea to abuse these ExpressibleBy...Literal types.
This can be a scenario to work with Convertibles in custom types.
struct Employee : ExpressibleByStringLiteral {
var name: String = ""
init() {}
init(stringLiteral name: String) {
self.name = name
}
}
func reportName(_ employee: Employee) {
print("Name of employee is \(employee.name)")
}
reportName("John Doe") //Name of employee is John Doe

How can I create a optional value from a my class

I create a class User, and I want to create an optional out of it, compiler then fires an error
class User {
var firstName: String = ""
var lastName: String = ""
}
var Tom = User?(firstName: "Tom", lastName: "Soya")
error: cannot invoke initializer for type 'User?' with an argument list of type '(firstName: String, lastName: String)'
Question: What's wrong with it? Do I need to put more stuff inside class before I can create an optional value? If so what is it?
Thanks
The problem is that your User class has no initializers. Thus, there is only one way to initialize it: namely, by saying User(). So if you are going to define User like this:
class User {
var firstName: String = ""
var lastName: String = ""
}
Then the best you can do is this:
var tom : User? = User()
tom?.firstName = "Tom"
tom?.lastName = "Soya"
#matt's answer is correct, but here's another way, which I think is clearer:
var tom = Optional(User())
See Optional.init(_:).