0

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?

3
  • You should avoid using init directly. JsonHelper.arrivalScan(TOID:... will be good enough. Commented Apr 12, 2018 at 21:01
  • Any specific reason as to why to avoid using init directly? @Ryan Commented Apr 12, 2018 at 21:08
  • @BadhanGanesh Because you don't need. You don't want to type couple more characters every time, do you? If you are using swiftlint like script tool, saving characters in a line can be a big issue. Commented Apr 12, 2018 at 21:14

5 Answers 5

3

There are a few issues:

  1. 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.

  2. 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.

  3. 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)
Sign up to request clarification or add additional context in comments.

Comments

1

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:

Comments

1

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(....

Comments

0

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)

Comments

0

Or mark sendArrival function as static.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.