I have a struct variable that is wrapped into an NSObject
class.
Here is how it looks:
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 append the myVar
variable with values from a different class and to access them.
I'm trying this code for appending:
var classAccess = myClass()
classAccess.myVar.append(array1:["value 3"], additionalInfo:nil, answers:["value 3"])
However, I get all sorts of nasty errors when I try to compile.
How do you access it the proper way?
Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one. It is called inner class.
Accessing data fields in structs Although you can only initialize in the aggregrate, you can later assign values to any data fields using the dot (.) notation. To access any data field, place the name of the struct variable, then a dot (.), and then the name of the data field.
The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.
You can do that, but you have to use the MyStruct
initializer and you have to reference the struct from outside the class with MyClass.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 struct and class names
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With