9 Replies
      Latest reply on Feb 12, 2019 3:33 PM by Claude31
      japio Level 1 Level 1 (0 points)

        How do I create a structure with a dictionary in it and then when I use the structure how do I use the key to return the value of the key? For example, something like this.

         

        struct someStruct {
             var opened = Bool()
             var title = String()
             var sectionData = DictionaryValue()
        }
        
        var tableViewData = [someStruct]()
        
        override func viewDidLoad() {
                super.viewDidLoad()
                
                someStruct = [cellData(opened: false, title: "Title1", sectionData: ["Cell1", "Cell2"]),
                                 cellData(opened: false, title: "Title1", sectionData: ["Cell1", "Cell2"]),
                                 cellData(opened: false, title: "Title1", sectionData: ["Cell1", "Cell2"]),
                                 cellData(opened: false, title: "Title1", sectionData: ["Cell1", "Cell2"])]
            }
        
             tableViewData = [someStruct(opened: false, title: "Title 1", Dictionary: ?)]
        
        
        
        • Re: Create a structure with a dictionary
          Claude31 Level 8 Level 8 (6,365 points)

          You should explain more precisely.

           

          When you initialize someStruct =

          you do not create a dictionaty for sectionData, but just an array.

           

          In addition, why do you copy 4 times the same content in someStruct ?

          Your code is also wrong, you cannot write someStruct =

          but you need to declare a var

          So I suppose you meant tableViewData

           

          You should show on an example what you want to store in sectionData.

          How did you define DictionaryValue ?

           

          Typically you should have:

          typealias DictionaryValue = [String : String]     // key is a String and value is a String

          or

          typealias DictionaryValue = [Int : String]          // Key is an Int, value a String

           

          Then, you shoud write

           

          typealias DictionaryValue = [Int : String]          // Key is an Int, value a String
          
          struct SomeStruct {          // Should start with upperCase
               var opened = Bool()
               var title = String()
               var sectionData = DictionaryValue()
          }
          
          var tableViewData = [SomeStruct]()
          
          override func viewDidLoad() {
                  super.viewDidLoad()
                
                  tableViewData = [cellData(opened: false, title: "Title1", sectionData: [1:"Cell1", 2:"Cell2"]),
                                   cellData(opened: false, title: "Title2", sectionData: [3:"Cell3", 4:"Cell4"]),
                                   cellData(opened: false, title: "Title3", sectionData: [5:"Cell5", 6:"Cell6"]),
                                   cellData(opened: false, title: "Title4", sectionData: [7:"Cell7", 8:"Cell8"])]
              }
          
              // See how defined above     tableViewData = [someStruct(opened: false, title: "Title 1", Dictionary: ?)]
            • Re: Create a structure with a dictionary
              japio Level 1 Level 1 (0 points)

              I have a dictionary in a different file. The key and the value are both of type String.

               

              let someDictionary = ["String":"String"]

               

               

              I copied it 4 times to create 4 rows in a table view.

               

              I have a dictionary and I want to show the dictionary key in the title of the struct and I want to show the dictionary value in the sectionData of the struct when the table view cell expands to show a new cell.

               

              struct SomeStruct {     
                   var opened = Bool()  // To create a new cell below the title cell.
                   var title = String()  // To store the key of someDictionary in a different file
                   var sectionData = DictionaryValue() // To store the value of someDictionary in a different file. 
              }  
              
              

               

               

              Thank you

                • Re: Create a structure with a dictionary
                  Claude31 Level 8 Level 8 (6,365 points)

                  Looks like you are confusing many things here.

                   

                  You declare a struct to define the template of data. You will not assign values (except default initialization) in the struct declaration.

                  typealias DictionaryValue = [Int : String]          // Key is an Int, value a String

                   

                  struct SomeStruct {

                       var opened = Bool()

                       var title = String()

                       var sectionData = DictionaryValue()

                  }

                  You have to do it in each instance of the struct that you create.

                   

                  So you have define the dataSource for your table:

                  var tableViewData = [SomeStruct]()

                   

                  You say the dictionary is defined in a different file. You mean a file of the XCode project or a file on iPhone ?

                   

                  Anyway, you have in viewDidLoad, you have to set the content of this array (you will update if you add, remove, change, move a cell

                  Something like

                          tableViewData = [SomeStruct(opened: false, title: "Title1", sectionData: [1:"Cell1", 2:"Cell2"]),

                                           SomeStruct(opened: false, title: "Title2", sectionData: [1:"Cell3", 4"Cell4"]),     // No key 2 here may be

                                           SomeStruct(opened: false, title: "Title3", sectionData: [1:"Cell5", 2:"Cell6"]),

                                           SomeStruct(opened: false, title: "Title4", sectionData: [1:"Cell7", 3:"Cell8"])]     // No key 2 here maybe

                  Note:

                       let someDictionary = ["String":"String"] 

                  is incorrect and will not compile

                  Note: you have content named Cell1, Cell2: I don't understand why you refer to cells here, but let's look at it just as Strings.

                  In the same way, how do you want to define keys ?

                   

                  Then in the delegate func cellForRowAt, you will use the dataSource to populate the cell, like below

                   

                  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                  
                          let cell = tableView.dequeueReusableCell(withIdentifier: "CellItem", for: indexPath)
                          let myCellData = tableViewData[IndexPath.row]     // I assume you have only one section
                          // Configure the cell...
                          cell.textLabel!.text = myCellData.title       
                          cell.detailTextLabel?.text = myCellData.sectionData[1) ?? ""     // I use key 1, but it may not exist hence ??
                  
                          return cell
                      }

                  So, please explain better what you want to do, in terms of data content, because I am forced to guess too many things.

                    • Re: Create a structure with a dictionary
                      japio Level 1 Level 1 (0 points)

                      The dictionary is defined in a different swift file in the same project. In viewDidLoad, I want the struct to return the value of a key of the dictionary in the other swift file that I type into sectionData. Thank you for your help.

                        • Re: Create a structure with a dictionary
                          Claude31 Level 8 Level 8 (6,365 points)

                          This other Swift file defines a class ? Is it another viewController ?

                          If so, do you segue from this controller to the viewController where the table is ?

                           

                          So please, show the code of this class where you define the dictionary.

                            • Re: Create a structure with a dictionary
                              japio Level 1 Level 1 (0 points)

                              Yes, it is part of a class and no it is not a view controller. It is just another file in the project.

                               

                              import Foundation
                              import UIKit
                              
                              class BusinessInformationSystemsDictionary: UIViewController {
                                   let businessInformationDictioary = ["Information Technology":"Some information about technology","Data":"???"
                                      ]  
                              

                               

                               

                              It looks something like that. There is a lot more keys and values in businessInformationDictionary but for some reason it woukldn't let me put it here.

                                • Re: Create a structure with a dictionary
                                  Claude31 Level 8 Level 8 (6,365 points)

                                  From what you say

                                   

                                  class BusinessInformationSystemsDictionary: UIViewController { 

                                   

                                  this class is effectively a UIViewController. Why do you think it is not ?

                                   

                                  I have enough information for the dictionary, no need to post more.

                                   

                                  What I need to know:

                                  how do you transtion from this BusinessInformationSystemsDictionary to the class that contains the tableView ? Let us call this class ViewControllerB

                                   

                                  - if you segue, it would be very easy to create a property in ViewControllerB

                                  var theDict: DictionaryValue?

                                   

                                  In the prepare for segue in BusinessInformationSystemsDictionary, you should set the dictionary:

                                      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                                  
                                          if let destVC = segue.destination as? ViewControllerB {
                                              destVC.theDict = businessInformationDictioary
                                          }
                                      }

                                   

                                  Now, you can access theDict within ViewControllerB

                                   

                                  - if you instantiate durectly ViewControllerB as viewControllerB inside BusinessInformationSystemsDictionary, you would set

                                  viewControllerB.theDict = businessInformationDictioary

                                   

                                   

                                  - In all cases, you can also use delegation pattern, but that may be overkill here.

                                    • Re: Create a structure with a dictionary
                                      japio Level 1 Level 1 (0 points)

                                      It is a subclass of UIViewController. I didn't think it is because I don't have it connected to a ViewController in the story board. Thank you for helping me. I will work with this and then get back to this discussion board. Thank you

                                        • Re: Create a structure with a dictionary
                                          Claude31 Level 8 Level 8 (6,365 points)

                                          Do you want it to be really a viewController ? If you want it just to be a class, you should declare it just as

                                           

                                          class BusinessInformationSystemsDictionary {

                                               let businessInformationDictioary = ["Information Technology":"Some information about technology","Data":"???"

                                          // etc

                                          }