Search code examples
iosarraysxcodeswiftanyobject

Array cannot be bridged from Objective-C error


After instantiating an array as an AnyObject type and adding string objects to the array the following code compiles however crashes when attempting to display this array in a textfield. The program still crashes when attempting to print it out to the debug console as well with the following error message: "fatal error: array cannot be bridged from Objective-C". Is the following method not compatible with swift?

print((jumperCablesRoles as! [String]).joinWithSeparator(","))

Here is the entire code base:

    import UIKit


class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    var rejuvenatedEnterprisesEmployees = [
        Employees(name: "Jeffrey Wells", employeeRoles: ["Accountant", "Designer"], departmentID: 0),
        Employees(name: "Victor Atkins", employeeRoles: ["Director of IT", "Sales Stategist"], departmentID: 0),
        Employees(name: "Kelli Hamilton", employeeRoles: ["Director of Sales"], departmentID: 0),
        Employees(name: "Adam Braun", employeeRoles: ["Sales Lead"], departmentID: 1),
        Employees(name: "Brian Cruz", employeeRoles: ["Designer"], departmentID: 1),
        Employees(name: "Kristen Floyd", employeeRoles: ["Developer"], departmentID: 1),
        Employees(name: "Lois Martinez", employeeRoles: ["Curator", "Blogger"], departmentID: 2),
        Employees(name: "Michael Lind", employeeRoles: ["Sales, Administrator"], departmentID: 2),
        Employees(name: "Eric Bay", employeeRoles: ["Database Admin"], departmentID: 2),
        Employees(name: "Brandon Young", employeeRoles: ["Artist"], departmentID: 2)
    ]
    var rejuvenatedEnterprisesVictorsEmployees = [
        Employees(name: "Victor Atkins", employeeRoles: ["Director of IT", "Sales Stategist"], departmentID: 0),
        Employees(name: "Adam Braun", employeeRoles: ["Sales Lead"], departmentID: 1),
        Employees(name: "Brian Cruz", employeeRoles: ["Designer"], departmentID: 1),
        Employees(name: "Kristen Floyd", employeeRoles: ["Developer"], departmentID: 1)
    ]
    var rejuvenatedEnterprisesKellisEmployees = [
        Employees(name: "Kelli Hamilton", employeeRoles: ["Director of Sales"], departmentID: 0),
        Employees(name: "Lois Martinez", employeeRoles: ["Curator", "Blogger"], departmentID: 2),
        Employees(name: "Michael Lind", employeeRoles: ["Sales, Administrator"], departmentID: 2),
        Employees(name: "Eric Bay", employeeRoles: ["Database Admin"], departmentID: 2),
        Employees(name: "Brandon Young", employeeRoles: ["Artist"], departmentID: 2)
    ]
    var jumperCables: [String] = []
    var jumperCablez: [String] = []
    var jumperCablic: [String] = []
    var jumperCablesRoles: [AnyObject] = []
    var jumperCablezRoles: [String] = []
    var jumperCablicRoles: [String] = []


    @IBOutlet weak var employeePicker: UIPickerView!
    @IBOutlet weak var employeeNameLabel: UILabel!
    @IBOutlet weak var employeeListing: UITextView!
    @IBAction func addEmployee(sender: AnyObject) {

    }
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBAction func indexChanged(sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
            chosenManagerScrollView.backgroundColor = UIColor.yellowColor()
            var jumperCables: [String] = []
            for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
                    jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)

                }
                chosenManagerScrollView.text = jumperCables[0]
                employeePicker.reloadAllComponents()

        case 1 where segmentedControl.selectedSegmentIndex == 1:
            let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
            chosenManagerScrollView.backgroundColor = UIColor.greenColor()
                var jumperCablez: [String] = []
                for var i = 0; i < rejuvenatedEnterprisesVictorsEmployees.count ; ++i {
                    jumperCablez.append(rejuvenatedEnterprisesVictorsEmployees[i].name)
                    chosenManagerScrollView.text = rejuvenatedEnterprisesVictorsEmployees[i].name
                }
                employeePicker.reloadAllComponents()

        case 2 where segmentedControl.selectedSegmentIndex == 2:
            let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
            chosenManagerScrollView.backgroundColor = UIColor.orangeColor()
            var jumperCablig: [String] = []
            for var i = 0; i < rejuvenatedEnterprisesKellisEmployees.count ; ++i {
                jumperCablig.append(rejuvenatedEnterprisesKellisEmployees[i].name)
            }
            chosenManagerScrollView.text = jumperCablig[0]
            employeePicker.reloadAllComponents()

        default:
            break; 
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        employeePicker.dataSource = self
        employeePicker.delegate = self

    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            return rejuvenatedEnterprisesEmployees.count

        case 1 where segmentedControl.selectedSegmentIndex == 1:
            return rejuvenatedEnterprisesVictorsEmployees.count

        case 2 where segmentedControl.selectedSegmentIndex == 2:
            return rejuvenatedEnterprisesKellisEmployees.count

        default:
            return 5
        }

    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            var jumperCabled: [String] = []
            for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
                jumperCabled.append(rejuvenatedEnterprisesEmployees[i].name)
            }
            return jumperCabled[row]

        case 1 where segmentedControl.selectedSegmentIndex == 1:
            var jumperCabling: [String] = []
            for var i = 0; i < rejuvenatedEnterprisesVictorsEmployees.count ; ++i {
                jumperCabling.append(rejuvenatedEnterprisesVictorsEmployees[i].name)
            }
            return jumperCabling[row]

        case 2 where segmentedControl.selectedSegmentIndex == 2:
            var jumperCab: [String] = []
            for var i = 0; i < rejuvenatedEnterprisesKellisEmployees.count ; ++i {
                jumperCab.append(rejuvenatedEnterprisesKellisEmployees[i].name)
            }
            return jumperCab[row]

        default:
            return "datsource corrupted"
        }
    }


    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {

        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
                let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
                chosenManagerScrollView.backgroundColor = UIColor.yellowColor()
                for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
                    jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)
                    jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
                }
                chosenManagerScrollView.text = jumperCables[row]
                return chosenManagerScrollView

        case 1 where segmentedControl.selectedSegmentIndex == 1:
                let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
                chosenManagerScrollView.backgroundColor = UIColor.greenColor()
                for var i = 0; i < rejuvenatedEnterprisesVictorsEmployees.count ; ++i {
                    jumperCablez.append(rejuvenatedEnterprisesVictorsEmployees[i].name)
                }
                chosenManagerScrollView.text = jumperCablez[row]
                return chosenManagerScrollView

        case 2 where segmentedControl.selectedSegmentIndex == 2:
            let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
            chosenManagerScrollView.backgroundColor = UIColor.orangeColor()
            for var i = 0; i < rejuvenatedEnterprisesKellisEmployees.count ; ++i {
                jumperCablic.append(rejuvenatedEnterprisesKellisEmployees[i].name)
            }
            chosenManagerScrollView.text = jumperCablic[row]
            return chosenManagerScrollView

        default:
            let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
            chosenManagerScrollView.backgroundColor = UIColor.orangeColor()
            for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
                jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)
            }
            chosenManagerScrollView.text = jumperCables[row]
            return chosenManagerScrollView

        }
    }


    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            employeeNameLabel.text = jumperCables[row]
            //employeeListing.text = (jumperCablesRoles as! [String]).joinWithSeparator(",")
            print((jumperCablesRoles as! [String]).joinWithSeparator(","))

        case 1 where segmentedControl.selectedSegmentIndex == 1:
            employeeNameLabel.text = jumperCablez[row]

        case 2 where segmentedControl.selectedSegmentIndex == 2:
            employeeNameLabel.text = jumperCablic[row]

        default:
            employeeNameLabel.text = jumperCables[row]

        }
    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Solution

  • The problem you're having is in this line of code: jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles).

    As far as I can tell, employeeRoles is an array of Strings in itself ([String]). This means that if you add an employee's employeeRoles to the jumperCablesRoles array, jumperCablesRoles is actually an array which contains an array of Strings. ([[String]]). This is why, when you try to force cast it into [String] it will fail.

    I'm not sure exactly what you want the result to be, so I can't help you with that, but you can try to append each element of employeeRoles separately to jumperCablesRoles if that's what you're going for. If that's what you want, just leave a comment and I'll provide additional code.

    Edit: I just went ahead and found a solution anyways, simply replace jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles) with the following:

    rejuvenatedEnterprisesEmployees[i].employeeRoles.forEach {jumperCablesRoles.append($0)}
    

    This adds every employeeRole (which is a String) to jumperCablesRoles separately.