Ios 请提供帮助。无法强制转换类型的值
它是tableViewController,具有位置名称列表。 当我使用plist时,无法将类型为“\uu NSCFArray”(0x1039adae0)的值强制转换为“NSString”(0x10301db00)Ios 请提供帮助。无法强制转换类型的值,ios,swift,tableview,Ios,Swift,Tableview,它是tableViewController,具有位置名称列表。 当我使用plist时,无法将类型为“\uu NSCFArray”(0x1039adae0)的值强制转换为“NSString”(0x10301db00) import UIKit class LocationsTableViewController: UITableViewController { let location = LocationsClass() override func viewDidLoad() {
import UIKit
class LocationsTableViewController: UITableViewController {
let location = LocationsClass()
override func viewDidLoad()
{
super.viewDidLoad()
let locationsPath = NSBundle.mainBundle().pathForResource("Location", ofType: "plist")
let locationsArray = NSArray(contentsOfFile:locationsPath!)!
let stationsPath = NSBundle.mainBundle().pathForResource("Station", ofType: "plist")
let stationsDict = NSDictionary(contentsOfFile: stationsPath!)!
for locationArray in locationsArray
{
There is a mistake in the following line
location.name = locationArray["name"] as! String//Could not assign a value of type AnyObject?! to a value of type String
location.image = locationArray["image"] as! String
let stationsArray = stationsDict[location.name as String] as! NSArray
for stationArray in stationsArray {
let dictionaryFromArray = stationArray as! NSDictionary
let workObject = StationsClass(nameStation:dictionaryFromArray["nameStatiom"] as! String, address: dictionaryFromArray["address"] as! String)
location.stations.append(workObject)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
我不知道,有什么回报
(
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return 10
}
您的
locationArray
数组包含一个字典。此词典有一个键“name”,其中包含一个字符串数组
在locationsArray循环中忘记locationArray的,使用[0]
从数组中提取第一个(也是唯一一个)项,并使用将其强制转换为字典,如果让它这样做:
if let dict = locationArray[0] as? [String:AnyObject] {
}
然后在内部可以访问“name”键的值并将其转换为字符串数组:
if let dict = locationArray[0] as? [String:AnyObject] {
if let namesArray = dict["name"] as? [String] {
}
}
最后,您可以获得您的价值观:
if let dict = locationArray[0] as? [String:AnyObject] {
if let namesArray = dict["name"] as? [String] {
for name in namesArray {
print(name)
}
}
}
您的locationArray
数组包含一个字典。此词典有一个键“name”,其中包含一个字符串数组
在locationsArray循环中忘记locationArray的,使用[0]
从数组中提取第一个(也是唯一一个)项,并使用将其强制转换为字典,如果让它这样做:
if let dict = locationArray[0] as? [String:AnyObject] {
}
然后在内部可以访问“name”键的值并将其转换为字符串数组:
if let dict = locationArray[0] as? [String:AnyObject] {
if let namesArray = dict["name"] as? [String] {
}
}
最后,您可以获得您的价值观:
if let dict = locationArray[0] as? [String:AnyObject] {
if let namesArray = dict["name"] as? [String] {
for name in namesArray {
print(name)
}
}
}
如果locationArray
确实是一个数组,那么不要像订阅字典一样订阅它<如果locationArray
是一个字典,那么code>locationArray[“name”]
可以工作,但它是一个数组。从重构关于这个部分的代码开始。@EricD。这是我的Location.plist我不明白code有什么错如果locationArray
确实是一个数组,那么不要像订阅字典一样订阅它<如果locationArray
是一个字典,那么code>locationArray[“name”]
可以工作,但它是一个数组。从重构关于这个部分的代码开始。@EricD。这是我的位置。plist我不明白代码中有什么错误非常感谢,这是工作,你能帮助我,返回什么覆盖函数tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{//返回节中的行数?不客气。:)你的新问题是一个新问题。你应该在新帖子中提问。但在此之前,有一个提示:答案是使用数据源(数组)的.count
。谢谢。我这样做了,只显示名称locationsArray[0],而不显示其他索引(非常感谢您,这是一项工作,您能帮我做些什么吗,返回override func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{//返回节中的行数?不客气。)你的新问题是一个新问题。你应该在一篇新文章中提问。但在此之前,有一个提示:答案是使用数据源(数组)的.count
。谢谢。我只显示了名称locationsArray[0],但不显示其他索引(