数组无法桥接 Objective-C 错误
Array cannot be bridged from Objective-C error
将数组实例化为 AnyObject 类型并将字符串对象添加到数组后,以下代码可以编译,但在尝试在文本字段中显示此数组时会崩溃。尝试将其打印到调试控制台时程序仍然崩溃,并显示以下错误消息:"fatal error: array cannot be bridged from 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.
}
}
您遇到的问题出在这行代码中:jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
。
据我所知,employeeRoles
本身就是一个 String
数组 ([String]
)。这意味着如果将员工的 employeeRoles
添加到 jumperCablesRoles
数组,jumperCablesRoles
实际上是一个包含 String
数组的数组。 ([[String]]
)。这就是为什么当您尝试强制将其转换为 [String]
时它会失败的原因。
我不确定你想要的结果是什么,所以我无法帮助你,但你可以尝试将 employeeRoles
的每个元素分别附加到 jumperCablesRoles
如果那是你想要的。如果那是你想要的,请发表评论,我会提供额外的代码。
编辑:我只是继续前进并找到了解决方案,只需将 jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
替换为以下内容即可:
rejuvenatedEnterprisesEmployees[i].employeeRoles.forEach {jumperCablesRoles.append([=10=])}
这会将每个 employeeRole
(这是一个 String
)分别添加到 jumperCablesRoles
。
将数组实例化为 AnyObject 类型并将字符串对象添加到数组后,以下代码可以编译,但在尝试在文本字段中显示此数组时会崩溃。尝试将其打印到调试控制台时程序仍然崩溃,并显示以下错误消息:"fatal error: array cannot be bridged from 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.
}
}
您遇到的问题出在这行代码中:jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
。
据我所知,employeeRoles
本身就是一个 String
数组 ([String]
)。这意味着如果将员工的 employeeRoles
添加到 jumperCablesRoles
数组,jumperCablesRoles
实际上是一个包含 String
数组的数组。 ([[String]]
)。这就是为什么当您尝试强制将其转换为 [String]
时它会失败的原因。
我不确定你想要的结果是什么,所以我无法帮助你,但你可以尝试将 employeeRoles
的每个元素分别附加到 jumperCablesRoles
如果那是你想要的。如果那是你想要的,请发表评论,我会提供额外的代码。
编辑:我只是继续前进并找到了解决方案,只需将 jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
替换为以下内容即可:
rejuvenatedEnterprisesEmployees[i].employeeRoles.forEach {jumperCablesRoles.append([=10=])}
这会将每个 employeeRole
(这是一个 String
)分别添加到 jumperCablesRoles
。