0
import UIKit
import Foundation

var FamilyOne = ["Sarah", "Female", "Granddaughter"]
var FamilyTwo = ["Isabel", "Female", "Granddaughter"]
var FamilyThree = ["Maya", "Female", "Granddaughter"]
var FamilyFour = ["Jean", "Female", "Daughter"]
var FamilyFive = ["Jennie", "Female", "Daughter"]

var MainArray = FamilyOne

var FemaleArray = ["Ava", "Mary", "Ann", "Carolina", "Jessica", "Emily",     "Elizabeth"]
var Photo = MainArray[0]
var SImage = UIImage(named: Photo)

var Gender = MainArray[1]
var Relation = MainArray[2]

class ViewController: UIViewController, UIImagePickerControllerDelegate,
    UINavigationControllerDelegate {

    @IBOutlet var image: UIImageView!
    @IBOutlet var label: UILabel!
    @IBOutlet var ButtonOne: UIButton!
    @IBOutlet var BUttonTwo: UIButton!
    @IBOutlet var ButtonThree: UIButton!
    @IBOutlet var BUtton4: UIButton!

    var SimageView = UIImageView(image: SImage!)

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = ("What is the name of your \(Relation) ?")

        SimageView.frame = CGRect(x: 67, y: 40, width: 240, height: 128)
        view.addSubview(SimageView)

        ButtonOne.setTitle(FemaleArray[4], for: UIControlState.normal)
        BUttonTwo.setTitle(MainArray[0], for: UIControlState.normal)
        ButtonThree.setTitle(FemaleArray[5], for: UIControlState.normal)
        BUtton4.setTitle(FemaleArray[2], for: UIControlState.normal)
    }
}

How can I put this code into a for loop and have it change "MainArray"'s value to FamilyTwo the second time the loop runs, FamilyThree the third time the loop runs, ect. ect.? When I put a for loop before the ViewController I constantly get the error 'Expression not allowed on top level'.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
J.Doe
  • 1
  • 2
  • 2
    if each array is meant to represent a family member, then it might be better to create a custom class to represent each one. You could name it something like FamilyMember and give it string properties like name, gender, and relation. – Dalton Sweeney Feb 09 '17 at 16:17
  • 1
    How I understand, put it/call it in `viewDidLoad()`. If you are not familiar with a viewControllers life cycle, [check it out](http://stackoverflow.com/a/12608364/1457385). – shallowThought Feb 09 '17 at 16:19

1 Answers1

-1

I'm not sure what you are trying to accomplish, but if you simply want to change the value of your MainArray in runtime with the set of families you can store them in a new collection.

var FamilyOne = ["Sarah", "Female", "Granddaughter"]
var FamilyTwo = ["Isabel", "Female", "Granddaughter"]
var FamilyThree = ["Maya", "Female", "Granddaughter"]
var FamilyFour = ["Jean", "Female", "Daughter"]
var FamilyFive = ["Jennie", "Female", "Daughter"]
let Families = [FamilyOne, FamilyTwo, FamilyThree, FamilyFour, FamilyFive]

var MainArray = FamilyOne //set initial value?

override func viewDidLoad() {
    super.viewDidLoad()
    //loop here
    for family in Families{
       MainArray = family
    }
}

Expression not allowed on top level is normal if you write the loop outside a function or a class.

janusfidel
  • 8,036
  • 4
  • 30
  • 53