0

I'm attempting to make a specialized calculation app, but have absolutely no experience with swift or iOS development and some amount of experience in Java before starting this project up a few days ago.

I'm attempting to pass a struct variable from one view controller to another, and then gain those variables through instantiation.

Here's the code including the struct:

            struct SSDCalcs {
            var patientInput: String
            var siteInput: String
            var scriptInput: Double
            var depthInput: Double
            var fieldInput: Double
            var length: Double
            var width: Double
            var squareInput: Double
        }

        //Change values of struct when enter key pressed
        @IBAction func enterPressed(_ sender: Any) {
                calcResults = SSDCalcs(patientInput: patientID.text!, siteInput: "fdfadsf", scriptInput: 5.0, depthInput: 6.0, fieldInput: 7.0, length: 8.0, width: 9.0, squareInput: 15.0)
       }


    //Initial delcaration of struct values (So the results page class doesn't complain about "calcResults" not yet existing"
    var calcResults = SSDCalcs(patientInput: "Error: Wrong struct values", siteInput: "Error", scriptInput: 0.0, depthInput: 0.0, fieldInput: 0.0, length: 0.0, width: 0.0, squareInput: 0.0)

}

And here is my results page code from the other class:

func setValues() {
    var SSDCalcs = SSDCalculation().calcResults
    self.SSDPatientRef.text = "Patient ID: " + SSDCalcs.patientInput
    self.SSDSiteRef.text = "Treatment Site: " + SSDCalcs.siteInput
    self.SSDScriptRef.text = "Script (cGy): " + String(SSDCalcs.scriptInput)
    self.SSDDepthRef.text = "Depth: " + String(SSDCalcs.depthInput)
    self.SSDFieldRef.text = "Field Size: " + String(SSDCalcs.fieldInput)
    self.SSDLengthRef.text = "Length: " + String(SSDCalcs.length)
    self.SSDWidthRef.text = "Width: " + String(SSDCalcs.width)
    self.SSDSqrRef.text = "Equivalent Square: " + String(SSDCalcs.squareInput)
}

No error is given when compiling or running the code... it just doesn't seem to update itself whenever the "enter" key is pressed on the page where you enter the fields. According to the xcode debugger (Breakpoints are life), the method does activate when the enter UIButton is pressed, the values are changed, but yet when shown on the results page, the supposed changes are gone, and instead they show they initial declaration values.

I've thought that perhaps the data is wiped when the new view is loaded... but I have no idea how to get around it.

Any help would be greatly appreciated!

1
  • As a side note, you'll get much faster compile times using string interpolation ("Width: \(SSDCalcs.width)") over the + operator ("Width: " + String(SSDCalcs.width)), and you don't have to explicitly use the String constructor Commented Dec 29, 2017 at 22:14

2 Answers 2

1

Swift structs are value types:

A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.

And this is why the changes you apply on the passed struct instance does not affect the original instance of that struct.

For more information check the Swift's documentation on structs.

Structures and Enumerations are value in types, classes are reference types.

All of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.

Sign up to request clarification or add additional context in comments.

3 Comments

How would I go about then specifically referencing the newly created values? Is it stored in an array similar to when you create a new object in java and what to specifically refer to that object?
In your case, you can fix that by making your struct a class, but by changing your struct to class you will lose the Memberwise Initializer and you will need to write it by yourself.
While all this is true it won't fix the problem because you are always creating a new instance of the struct and then taking the values which of course are still the default. I think you need to reference an instance of it that you have already created somewhere else.
0

This line:

var SSDCalcs = SSDCalculation().calcResults

always creates a new instance of the SSDCalculation structure which will have the initial results. It doesn't refer to any other instance of that you may have already created.

6 Comments

Is it possible to refer to the newly created instance?
Do you mean you want to refer to the instance you have in the first controller from the second view controller?
There is data in the first view controller, which I stored in the struct. Finally, the data was supposed to be referenced in the second view controller and used from that reference, so yes.
If you are certain there is only going to be one of each of the view controllers you can use a static (class) variable a bit like this static public calcsVar = SSDCalcs() which you can then access form anywhere like this `FirstController.calcsVar'. Otherwise you need to create a more comprehensive data model which something that has access to each of the view controllers is in control of.
If you go the static variable route then yes. P.S. my original definition should have been static public var calcsVar = SSDCalcs()
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.