user987654
user987654

Reputation: 19

Not able to pass value to another class

I want to append a certain "id" to an api. This is the initial portion of how I make the API call in my viewController..

func myAPICall() {
     APIHelper(API: WebServices.getAllOrganizations as NSString, json: bodyStr as NSString, 
      methodType: Constants.Get_Method as NSString....) 
}
           

Now, WebServices.getAllOrganizations is defined elsewhere, in a swift file like so...

public class WebServices {

static let getAllOrganizations: String = "organization/getAllOrganizationDetails

}
       
    


To pass the value, I assigned it to a global variable like so...

ArrayData.shared.plantIDForOrganization = Int("\(dic["id"]!)")!

And further, I changed my swift file to this...

public class WebServices {

static let getAllOrganizations: String = "organization/getAllOrganizationDetails/\(ArrayData.shared.plantIDForOrganization)"                                  

}

But by doing this, the value is not properly passed to the API. A 0 is passed to the API instead of the actual id number.

What could be a more efficient way of passing value from my viewcontroller to the swift file..?

Upvotes: 0

Views: 51

Answers (2)

Gereon
Gereon

Reputation: 17882

When is ArrayData.shared.plantIDForOrganization initialized, and when does WebServices.getAllOganizations get its value? Once the latter is set, it won't "react" to changes in plantIDForOrganization.

I suggest you change this to a computed property, like so:

public class WebServices {

    static var getAllOrganizations: String {
        return "organization/getAllOrganizationDetails/\(ArrayData.shared.plantIDForOrganization)"
    }
}

Also, try to eliminate thos force-unwrapping from your code.

Upvotes: 1

Frankenstein
Frankenstein

Reputation: 16371

If you have to pass a parameter use a static method instead:

public class WebServices {
    static func getAllOrganizations(id: String) -> String { "organization/getAllOrganizationDetails/\(id)" }
}

Upvotes: 0

Related Questions