Reputation: 545
I have following class with struct:
class JsonHelper {
//for tests only
struct arrivalScan {
let TOID: String
let UserId: String
let GateId: String
let TimeStamp: String
}
func sendArrival(scan: arrivalScan){
//do something
}
}
In my view controller I am trying to create initialise arrivalScan:
let scan = JsonHelper.arrivalScan.init(TOID:"D/0098/123456",UserId:"O0124553",GateId: "G/0098/536371",TimeStamp: "11/04/2018 11:51:00")
and then pass this as argument to the sendArrival
function in JsonHelper
JsonHelper.sendArrival(scan)
But getting error 'JsonHelper.arrivalScan' is not convertible to 'JsonHelper'
What am I doing wrong?
Upvotes: 0
Views: 77
Reputation: 31645
The issue is you are trying to call sendArrival
directly from the class itself without creating an instance from it. So you have two choices to solve the issue:
1- Declare sendArrival
emthod as static:
static func sendArrival(scan: arrivalScan){
//do something
}
thus:
JsonHelper.sendArrival(scan: scan)
2- Create an instance from JsonHelper
and call the method using it:
let scan = JsonHelper.arrivalScan.init(TOID:"D/0098/123456",UserId:"O0124553",GateId: "G/0098/536371",TimeStamp: "11/04/2018 11:51:00")
let jsonHelperObject = JsonHelper()
jsonHelperObject.sendArrival(scan: scan)
Choosing one of the solutions depends on what is the appropriate design of JsonHelper
class.
Aside bar notes:
As naming convention, naming a structure should follow the upper camel case, which means ArrivalScan
instead of arrivalScan
.
When calling the sendArrival
method, you have to mention the label, as: sendArrival(scan: scan)
instead of sendArrival(scan) // compile-time error
. If you want to call it without mentioning the label, you could declare it as func sendArrival(_ scan: arrivalScan)
therefore you would be able to call it as sendArrival(scan)
.
You could instantiate scan
as let scan = JsonHelper.arrivalScan(TOID:"D/0098/123456",UserId:"O0124553",GateId: "G/0098/536371",TimeStamp: "11/04/2018 11:51:00")
, without the need of .init(...
.
Upvotes: 1
Reputation: 22701
There are a few issues:
First, always name your classes and structs with an initial capital letter. arrivalScan
should be ArrivalScan
. This will help you differentiate between a class (or struct) and an instance.
The sendArrival
function is an instance function. You are trying to access it as if it were a class function. Create an instance of the JsonHelper class, then call the function on that instance.
Variable names inside your struct should begin with lowercase.
Example:
class JsonHelper {
struct ArrivalScan {
let toId: String
let userId: String
let gateId: String
let timestamp: String
}
func sendArrival(scan: ArrivalScan) {
//do something
}
}
let helper = JsonHelper()
let scan = JsonHelper.ArrivalScan(toId: "value", userId: "value", gateId: "value", timestamp: "value")
helper.sendArrival(scan: scan)
Upvotes: 3
Reputation: 15238
You should call this method like below,
JsonHelper().sendArrival(scan: scan)
Because sendArrival
is not a static/class
method so it should be accessed by calling initializer and also you were missing the parameter name scane:
Upvotes: 1
Reputation: 3465
You need to create an instance of your JsonHelper
class to be able to call the sendArrival()
method and thus solve your error:
let jsonHelper = JsonHelper()
jsonHelper.sendArrival(scan)
Upvotes: 0