Access to struct variable inside class

I have a structural variable that is wrapped in a class NSObject.

Here's what it looks like:

class myClass: NSObject{

struct myStruct {
    var array1: [String]
    var additionalInfo: String?
    var array2: [String]
}

var myVar = [myStruct(array: ["value1"], additionalInfo:nil, array2: ["value1","value2"])]
}

My goal is to add a variable myVarwith values ​​from another class and access them.

I am trying to add this code:

var classAccess = myClass()
classAccess.myVar.append(array1:["value 3"], additionalInfo:nil, answers:["value 3"])

However, when I try to compile, I get all kinds of nasty errors.

How do you access it properly?

+4
source share
1 answer

You can do this, but you must use an initializer MyStruct, and you need to reference the structure outside the class usingMyClass.MyStruct

class MyClass: NSObject{

  struct MyStruct {
    var array1: [String]
    var additionalInfo: String?
    var array2: [String]
  }

  var myVar = [MyStruct(array1: ["value1"], additionalInfo:nil, array2: ["value1","value2"])]
}


var classAccess = MyClass()
classAccess.myVar.append(MyClass.MyStruct(array1:["value 3"], additionalInfo:nil, array2:["value 3"]))

Note: for better readability, I capitalized the names of structures and classes

+4
source

Source: https://habr.com/ru/post/1614193/


All Articles