Reputation: 28
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'"
Upvotes: 1
Views: 1642
Reputation: 5450
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
Upvotes: 0
Reputation: 26383
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.
Upvotes: 0
Reputation: 658
Your passedAccountStruct Should be like this in your second ViewController
:
var passedAccountStruct = [AccountStruct]()
Upvotes: 1
Reputation: 6379
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.
Upvotes: 1