0

I have this Object Array mention below . Can someone please guide me how can i access various values inside the Object Array.

CellInfo(date: "Sep 2018",
         audioFileInfos: [In.PtV.AudioFileInfo(urlString: "https://GeorgeB.m4a",
                                                    text: "9/11")
                         ])

i want to Access Date ,urlString and text

struct AudioFileInfo {
    let urlString: String
    let text: String

    init(dict: [String: String]) {
        urlString = dict["AudioFileURL"] ?? ""
        text = dict["Title"] ?? ""
    }
}

struct CellInfo {
    let date: String
    let audioFileInfos: [AudioFileInfo]
}
4
  • Please clearly depict the array you are trying to access. Commented Nov 27, 2018 at 17:53
  • Updated Question Commented Nov 27, 2018 at 18:07
  • it's highly depends on what AudioFileInfo method return type is. Please put more info i.e what is the return of the AudioFileInfo method is. If that is a own declared class, then does it have any text property ? Commented Nov 27, 2018 at 18:18
  • just updated my question Commented Nov 27, 2018 at 18:21

1 Answer 1

1

Consider the following code.

let cellInfo = CellInfo(date: "Sep 2018",
     audioFileInfos: [In.PtV.AudioFileInfo(urlString: "https://GeorgeB.m4a",
                                                text: "9/11")
                     ])
print(cellInfo.date) // prints date
print(cellInfo.audioFileInfos[0].urlString) // prints urlString
print(cellInfo.audioFileInfos[0].text) // prints urlString

The things is happening here is as follows

  1. You create CellInfo struct with date and audioFileInfos.
  2. while providing audioFileInfos you create another struct using the same way as #1
  3. You pass the AudioFileInfo inside of array.
  4. So incase of accessing the date you can directly access the date property using dot . operator.
  5. For accessing the AudioFileInfo struct object, same way just with indexing added.

As audioFileInfos is an array, safe & complete way to access it's values is to traverse the array, meanwhile accessing the array elements.

for audioFileInfo in cellInfo.audioFileInfos {
    print(audioFileInfo.urlString)
    print(audioFileInfo.text)
}
Sign up to request clarification or add additional context in comments.

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.