数组无法与Objective-C错误

时间:2016-03-20 03:32:40

标签: ios arrays xcode swift anyobject

在将数组实例化为AnyObject类型并将字符串对象添加到数组后,以下代码编译然后在尝试在文本字段中显示此数组时崩溃。尝试将其打印到调试控制台时程序仍然崩溃,并出现以下错误消息:“致命错误:无法从Objective-C桥接数组”。以下方法与swift不兼容吗?

print((jumperCablesRoles as![String])。joinWithSeparator(“,”))

以下是整个代码库:

    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.
    }


}

1 个答案:

答案 0 :(得分:0)

您遇到的问题出在以下代码行中:jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)

据我所知,employeeRoles本身就是一个String的数组([String])。这意味着,如果您将员工employeeRoles添加到jumperCablesRoles数组,jumperCablesRoles实际上是一个包含String数组的数组。 ([[String]])。这就是为什么当你试图将它强制转换为[String]时,它将会失败。

我不确定您想要的结果是什么,所以我无法帮助您,但您可以尝试将employeeRoles的每个元素分别附加到{{1如果这就是你要去的东西。如果这是你想要的,只需发表评论,我就会提供额外的代码。

修改:我只是继续寻找解决方案,只需将jumperCablesRoles替换为以下内容:

jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)

这会将每个rejuvenatedEnterprisesEmployees[i].employeeRoles.forEach {jumperCablesRoles.append($0)} employeeRole)分别添加到String