0

配列を AnyObject 型としてインスタンス化し、文字列オブジェクトを配列に追加した後、次のコードはコンパイルされますが、この配列をテキストフィールドに表示しようとするとクラッシュします。デバッグ コンソールに出力しようとすると、プログラムは依然としてクラッシュし、次のエラー メッセージが表示されます: 「致命的なエラー: アレイは Objective-C からブリッジできません」。次の方法はswiftに対応していませんか?

print((jumperCablesRoles as! [文字列]).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.
    }


}
4

1 に答える 1

0

あなたが抱えている問題は、このコード行にあります: jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles).

私が知る限り、それ自体が s のemployeeRoles配列です( )。これは、配列に従業員を追加すると、実際には の配列を含む配列になることを意味します。( )。これが、強制的にキャストしようとすると失敗する理由です。String[String]employeeRolesjumperCablesRolesjumperCablesRolesString[[String]][String]

結果をどうしたいのか正確にはわからないので、それについてはお手伝いできませんが、それが目的である場合は、の各要素をemployeeRoles個別に追加してみてjumperCablesRolesください。それが必要な場合は、コメントを残してください。追加のコードを提供します。

編集:とにかく先に進んで解決策を見つけました。単にjumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)次のものに置き換えてください:

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

employeeRoleこれは、すべて(これはString) をjumperCablesRoles個別に追加します。

于 2016-03-20T07:28:59.740 に答える