How to Set Struct Values Outside the Class?

I've got a struct in my singleton class and I'm trying to set the values in a different class. I tried setting it like I'd set any other variable in the singleton class but i get an error "Static member 'bigStruct', cannot be used on instance of type "SharingManager" "

I tried -

let sm = SharingManager.sharedInstance

sm.bigStruct  //Autocomplete doesn't work and i get an error


Then when I add line 15 in the singleton class, I can't set it because i get the error "Cannot assign to property: function calls immutable value" (From what i undertab this mens that it makes a copy of the struct so it can be viewed but not set)

sm.bigStruct.smallStruct.init().name = "my name"


Singleton Class -

class SharingManager {
  

    struct bigStruct {
      
        struct smallStruct {
            var name = String()
          
        }//Small Struct End

   }  //Big Struct End


    static let sharedInstance = SharingManager()
    var big = bigStruct.self
  
}//Class End
Answered by Rishaad in 285372022

I looked around somemore and tried this and it seems to work -


I added this to the singleton class

var big = SharingManager.bigStruct()
var small = SharingManager.bigStruct.smallStruct()


Then to use it I just have to use -

let sm = SharingManager.sharedInstance

sm.small.name = "this is a name"
print(sm.small.name) //this printed the coorect value I just set

Here is how I use it (and works perfectly):


class GlobalData {

    class var sharedGlobal : GlobalData {
        struct Singleton {
            static let instance = GlobalData()
        }
        return Singleton.instance;
    }     

    var firstVar : Int = 1

}


Then I just call :


    GlobalData.sharedGlobal.firstVar = 10


anywhere in code

My singleton class works fine for settings normal variables likle that. It's when i want to set something thats inside a struct I'm not sure how to do it properly. Like in the code I put, if i want to set "name" which is inside a struct (which is inside another struct) in the singleton class.

I think the confusion here is that Swift structs are not like C structs.


— In C, a struct is a data item with a value, essentially a kind of a variable.


— In Swift, a struct is a type, not a data item.


So, in your SIngleton class, you declared the struct type named "bigStruct", but you really meant to declare a variable (well, an instance property) named "bigStruct". You would fix your code something like this (using initial caps for types, initial lowercase for variables and properties):


class SharingManager {
    struct BigStruct { 
        struct SmallStruct { 
            var name = String()
        }
        var smallStruct: SmallStruct

   }
    static let sharedInstance = SharingManager() 
    var bigStruct: BigStruct
}

Now you can refer to the BigStruct value like this:


let sm = SharingManager.sharedInstance 
… sm.bigStruct …
… sm.bigStruct.smallStruct.name …
… etc …

With a struct inside :


    var gclock = GCL()


where


class GCL {
    var interLock : [Int: Set<Int>]


// With all the needed init


// I define functions as:
    func askToChange(forID: Int) {
            interLock[0]!.insert(forID)  // Just an example
     }

}


This is called like this :


GlobalData.sharedGlobal.gclock.askToChange(forID: 1)
Accepted Answer

I looked around somemore and tried this and it seems to work -


I added this to the singleton class

var big = SharingManager.bigStruct()
var small = SharingManager.bigStruct.smallStruct()


Then to use it I just have to use -

let sm = SharingManager.sharedInstance

sm.small.name = "this is a name"
print(sm.small.name) //this printed the coorect value I just set

Thanks for th explnation, I understand what you mean.


I tried this but then i get an error for the line "SharingManager cannot be constructed because it has no accessible initializers"

static let sharedInstance = SharingManager()


I looked around a little more and tried this (answer in post below) and it seems to be working properly so far.

How to Set Struct Values Outside the Class?
 
 
Q