请帮忙。不能施放类型的值

时间:2015-10-09 09:24:12

标签: ios swift tableview

它的tableViewController包含位置名称列表。 无法投射类型' __ NSCFArray' (0x1039adae0)到' NSString' (0x10301db00)。,当我使用plist

 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

        }

1 个答案:

答案 0 :(得分:0)

您的locationArray数组包含一个字典。这个字典有一个键“name”,它包含一个字符串数组。

忘记你的for locationArray in locationsArray循环并使用[0]从数组中提取第一个(也是唯一的)项目并将其转换为if let的字典,如下所示:

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