2

How do I load data into an array of structures in Swift?

What is the required syntax in the following?

struct TEST {
    var a = 0
    var b = 0
    var c = 0
}

var test: [TEST] = []

// Data to load:
// .a   1,2,3
// .b   4,5,6
// .c   7,8,9

UPDATE 1

@pacification Sorry for the delay in responding. Based on your Update 1, I have this code, which runs with no errors:

struct TEST {
    var a: Int
    var b: Int
    var c: Int
}

var a: [Int] = [1,2,3]
var b: [Int] = [4,5,6]
var c: [Int] = [7,8,9]

let test: [TEST]

for (index, _) in a.enumerate() {
    TEST(a: a[index], b: b[index], c: c[index])
}

However, I cannot see how to store the values in the 'test' array. What am I missing?

1
  • 2
    what is the type of data to load? array, dict, set, array of dicts, dict of arrays etc.? Commented Apr 22, 2016 at 5:14

4 Answers 4

1

You should use this:

struct Test {
    var a = 0
    var b = 0
    var c = 0
}


var test: [Test] = [
    Test(a: 1, b: 4, c: 7),
    Test(a: 2, b: 5, c: 8),
    Test(a: 3, b: 6, c: 9)
]

Pay attention, that structs have default init method (more info here), so you don't need create it.


UPDATE

I want to initialize the 'test' structure array with a length of 200

You can do that like this:

var test: [Test] = Array(count: 200, repeatedValue: Test())

I want to enter the data for 'a', 'b' and 'c' using this syntax: [1,2,3, ...] Is this possible?

Ahm. What type of a, b or c should be in your case? Array?


UPDATE 1

// Data to load:
// .a   1,2,3
// .b   4,5,6
// .c   7,8,9

So, if you are sure that a, b and c same length arrays you can do like this:

for (index, _) in a.enumerate() {
    Test(a: a[index], b: b[index], c: [index])
}

And all your data will be stored.

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

3 Comments

Thanks to 芮星晨, ishdeep and pacification for your similar answers. That syntax works, but it is still not quite what I need. I want to initialize the 'test' structure array with a length of 200. I want to enter the data for 'a', 'b' and 'c' using this syntax: [1,2,3, ...] Is this possible?
The data type is Int. I have working test code where I am declaring and loading three separate arrays, using this syntax: var a: [Int] = [1,2,3, ...]
I would prefer to have these three arrays combined into one structure,
0

maybe you should use this

struct test{
        var a:Int
        var b:Int
        var c:Int
        init(a:Int,b:Int,c:Int){
            self.a = a
            self.b = b
            self.c = c
        }
    }

func addSomeData(){
        var array:[test] = []
        array.append(test(a: 1, b: 2, c: 3))
        array.append(test(a: 4, b: 5, c: 6))
        array.append(test(a: 7, b: 8, c: 9))
    }

1 Comment

You don't need to write such inits for structs, because they are provided by the compiler automatically (that's also noted in @pacification's answer).
0

What i interpret from your question is you want to make an array object for a structure and store data in it. Here is an example that might help you.

struct TableData{
let a : Int
let b : Int
let c : Int }

Where ever you wish to access the struct array you will have to declare that array in that class.

var details = [TableData]()

Now adding data to the array depends on how you want store the data in the struct array. So i will tell you how to access each element of the array.

First you create a struct object that will store one set of struct details at a time.

let data = Tabledata(a:1,b:4,c:7)
details.append(data)

Ofc this will just a static allocation, i would suggest you use a loop to keep getting the details and adding them to the table array.

for i..in 3{
     let data = Tabledata(a:some value,b:some value,c:some value)
     details.append(data)
            }

Assuming you know about loops and concept of dictionaries you can get the values for some value and this will create 3 entries in the details array.

Let me know if it helps or if you need further clarification.

Comments

0

Could this be semantics? Structures in Swift are general purpose types (like classes). They are fairly similar but have relevant differences e.g. structures are value types.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html

Maybe you are looking for a Dictionary (key - value data)?

    var arryOfDictionaries = [
      ["a": 1, "b": 2, "c": 3],
      ["a": 4, "b": 5, "c": 6],
      ["a": 7, "b": 8, "c": 9]
    ]

    // Iterate of the array
    for dict in arryOfDictionaries {
      print(dict["a"]) // 1 4 7
      print(dict["b"]) // 2 5 8
    }

    // Add more "rows"
    var oneMore = ["a": 10, "b": 11, "c": 13]
    oneMore["c"] = 12
    arryOfDictionaries.append(oneMore) // Added [10, 11, 12]

If not then @pacification answer is a cleanest way to use structs. Also remember that Structures are not reference types like classes so you are not using instances, but rather copied values (as when using Int or Strings)

If you are not looking for an array of dictionaries as my example above suggest, a simple static class is also a good candidate.

Just my gut feeling, but your example above does not look like a typical use case for structs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.