0

I am working in xCode 10.2 with swift. I have created global variables in my first view controller for 6 tableviews. In storyboard, I have 6 tableViewControllers. In a separate swift file, I have created a table struct to hold an array and display the data in each corresponding cell. In each view controller in didSelectRowAt connects the next table view. My problem is when I get to the last table view. I need to associate website URLs to the array on the fifth table. I keep getting an error stating cannot convert string to URL. Please Help!

 var fifthArray = [
    FifthTableStruct(FifthTitle: ["Energy Guide", "https://www.google.com", "Warranty Page", "Use & Care Guide", "Specification Sheet", "FIT System", "Installation Instructions"]),
    FifthTableStruct(FifthTitle: ["Energy Guide", "Warranty Page", "Use & Care Guide", "Specification Sheet", "FIT System", "Installation Instructions"])
    ]

var sixthArray = [
    SixthTableStruct(SixthTitle:  ["https://www.whirlpool.com/content/dam/global/documents/201708/EnergyGuide-W11037203-RevA.pdf", "https://www.whirlpool.com/content/dam/global/documents/201708/WarrantyPage-W11037201-W.pdf", "https://www.whirlpool.com/content/dam/global/documents/201708/UseandCareGuide-W11037201-RevA.pdf", "https://www.whirlpool.com/content/dam/global/documents/201711/WL170160A_p2.pdf", "https://www.whirlpool.com/content/dam/global/documents/201901/wash-performance-guarantee-en.pdf", "https://www.whirlpool.com/content/dam/global/documents/201711/InstallationInstructions-W10682737-RevA.pdf"])
    ]

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let urlString = self.sixthArray[indexPath.row]
    if let url = URL(fileURLWithPath: urlString)
    {
        UIApplication.shared.openURL(url)
    }

}

I have the code for the tableStruct in an Array file separate from the viewController.

    import Foundation
    import UIKit


    struct SecondTableStruct {
        var SecondTitle = [String]()
    }
    struct ThirdTableStruct {
        var ThirdTitle = [String]()
    }
   struct FourthTableStruct {
        var FourthTitle = [String]()
    }
    struct FifthTableStruct {
        var FifthTitle = [String]()
    }
    struct SixthTableStruct {
        var SixthTitle = [String]()
    }
  • Google's URL in fifthArray was a test. not supposed to be there. –  Apr 17 '19 at 22:06
  • Use `URL(string: urlString)` to convert the string to an url. – Rudedog Apr 17 '19 at 22:25
  • I tried it still says that the url cannot be converted to a string... –  Apr 17 '19 at 23:08
  • Here is a link to a screenshot of my code and error when I change it to your suggestion. https://drive.google.com/file/d/1FCusnpnzmRRDr702VSdxaohEjGJBmprq/view?usp=sharing –  Apr 17 '19 at 23:18
  • Screenshots are not very useful, but elements of `SixthArray` are of type `SixthTableStruct`, not `String`. The error in your screenshot clearly states that. Your screenshot definitely does not say that “the url cannot be converted to a string”. The exact text of errors is important when you are asking questions. That means using copy and pasted to get them and post them here. – Rudedog Apr 17 '19 at 23:39
  • Thanks for the advice. I have tried several different ways, one of which gave me the original error i mentioned. When i changed it to your suggestion, Cannot convert value of type 'SixthTableStruct' to expected argument type 'String' was returned. If you have any helpful suggestions I would appreciate it. –  Apr 18 '19 at 00:05
  • Show your code for SixthTableStruct and FifthTableStruct. – Tomas Jablonskis Apr 18 '19 at 07:18
  • I've update my post with the code. Thanks for the help. –  Apr 18 '19 at 07:45

2 Answers2

0

sixthArray is an array of SixthTableStructs, a SixthTableStruct has a single field, SixthTitle, whose type is an array of String.

So to get to a single string stored within sixthArray you need to:

  1. Index into sixthArray to obtain a single value of type SixthTable, let's call this intermediate1
  2. Select the SixthTitle field of intermediate1 To obtain a value of type array of String, let's call this intermediate2
  3. Index into intermediate2 to obtain a single String value

In code:

let intermediate1 = sixthArray[someIndex]
let intermediate2 = intermediate1.SixthTitle
let urlString = intermediate2[someOtherIndex]

We can't tell you what the two index values you need are, one is presumably indexPath.row. (You can of course write the above three lines as one without the intermediates if you wish.)

A couple of suggestions, first you appear to have page titles and associated URLs, which form a closely connected pair of data values, broken up and stored in separate arrays requiring you to carefully manage the order of items in those arrays and losing the close association between the items. Consider a single array of some struct, say PageDetails, with appropriate properties, say title and URL, to keep these together.

Second, arrays can hold URLs, not just Strings...

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
-1

In your didSelectRowAt do following, Currently you are directly accessing struct but not its array having name SixthTitle

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let urlString = self.sixthArray.SixthTitle[indexPath.row]
if let url = URL(fileURLWithPath: urlString)
{
    UIApplication.shared.openURL(url)
}

}

Abu Ul Hassan
  • 1,340
  • 11
  • 28