1

I am new to the iPhone coding and I am trying to pass a struct to my second VC through PrepareForSegue.

In my main view controller I have below struct

struct AccountStruct {
        var account: String
        var balance: Double
        var objectId: String
}

var accountsFromStruct : [AccountStruct] = []

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        accountsFromStruct.append(AccountStruct(account: "Checking", balance: 451.455, objectId: "12354785"))

        accountsFromStruct.append(AccountStruct(account: "Savings", balance: 871.455, objectId: "123dfdf5"))


        let SecondVC = segue.destinationViewController as! PresentedViewController
        SecondVC.passedAccountStruct = accountsFromStruct
}

then in my second VC I have below code to setup var to hold passed struct var passedAccountStruct : [PassedAccountStruct] = []

My error I get is "Cannot convert value of type 'ViewController.AccountStruct' to expected argument type 'PresentedViewController.PassedAccountStruct'"

plv_2002
  • 28
  • 4
  • Your struct is of type AccountStruct not of type PassedAccountStruct. Change [ PassedAccountStruct] to [AccountStruct]. – Gruntcakes May 12 '16 at 03:55
  • As @SausageMachine said, the type in your second View Controller should be [AccountStruct] not [PassedAccountStruct] also there is no sense in initializing it to an empty array with = []. This should be set as an optional var, which will be nil until you set it in the segue. This would look like var passedAccountStruct: [AccountStruct]? – user3847320 May 12 '16 at 04:40

4 Answers4

1

You copied PassedAccountStruct in both ViewController and PresentedViewController. So there are two different PassedAccountStruct. Please keep one. For example, you kept ViewController one, you can access it by ViewController.AccountStruct in other view controllers.

Lumialxk
  • 6,239
  • 6
  • 24
  • 47
  • Awesome changing it to "var passedAccountStruct : [ViewController.AccountStruct]?" let me pass in the data. – plv_2002 May 12 '16 at 16:15
1

Your passedAccountStruct Should be like this in your second ViewController :

var passedAccountStruct = [AccountStruct]()
AkBombe
  • 638
  • 4
  • 10
0

The message is really self explanatory

"Cannot convert value of type 'ViewController.AccountStruct' to expected argument type 'PresentedViewController.PassedAccountStruct'"

The type of the passedAccountStruct is not an AccountStruct array, please verify the type of both data.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

Struct is passed thought your program in swift. Depending what you want to do with it.

One way is to solve the problem to is to change the variable type within the struct to "static"

struct AccountStruct {
       static var account: String
       static var balance: Double
       static var objectId: String
       static var accountsFromStruct : [AccountStruct] = []
}

Then, from any file in the project you can just access the array:

AccountStruct.accountsFromStruct[index]

Now, there could be other methods that will be better practice for your case.

Try looking into Access Control

Idan
  • 5,405
  • 7
  • 35
  • 52