无法转换类型' __ NSDictionaryM' (0x2819d60)到' NSString' (0x2e85588)

时间:2015-07-04 13:28:52

标签: ios iphone swift uitableview

我在swift中有tableview,一切正常,但当我尝试在搜索栏中过滤某些内容时出现此错误:

我在swift中有tableview,一切正常,但当我尝试在搜索栏中过滤某些内容时出现此错误:

Could not cast value of type '__NSDictionaryM' (0x2819d60) to 'NSString' (0x2e85588). 

这是我的代码

var info = NSDictionary();    
var searchActive : Bool = false
var alertController: BPCompatibleAlertController?
var alert1: UIAlertView = UIAlertView()
var loadingIndicator1: UIActivityIndicatorView = UIActivityIndicatorView()
@IBOutlet weak var searchBar: UISearchBar!

var cuenta:String!
var user:String!
var password:String!
var data = [String]()    

var is_searching:Bool!
var filtered:NSMutableArray = NSMutableArray() 
var posts1:NSMutableArray = NSMutableArray()   
var myData:NSMutableArray = NSMutableArray()

var searchingDataArray:NSMutableArray = NSMutableArray()

override func viewDidLoad()
{
    super.viewDidLoad()

     is_searching = false
    self.beginParsing()
    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self

}

// this method does all the search trick
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
    if searchBar.text.isEmpty{
        is_searching = false
        self.tableView.reloadData()
    } else {
        println(" search text %@ ",searchBar.text as NSString)
        is_searching = true
        searchingDataArray.removeAllObjects()
        for var index = 0; index < self.posts1.count; index++
        {
            var currentString = self.posts1.objectAtIndex(index) as! String
            if currentString.lowercaseString.rangeOfString(searchText.lowercaseString)  != nil {
                searchingDataArray.addObject(currentString)

            }
        }
        self.tableView.reloadData()
    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if is_searching == true{
        return self.searchingDataArray.count
    }
    else{
        return self.posts1.count  //Currently Giving default Value
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell;

    if is_searching == true {   

        println("Active")
        cell!.textLabel?.text = searchingDataArray[indexPath.row] as! NSString as String

    }
    else {

        var speedKPH_Default = posts1.objectAtIndex(indexPath.row).valueForKey("speedKPH") as! NSString
        let  speedKPH_NSSTring = NSString(string: speedKPH_Default).intValue
        var Speed_Normal = String(speedKPH_NSSTring)
        var fecha_default = posts1.objectAtIndex(indexPath.row).valueForKey("timestamp") as! NSString
        var now = (NSDate(timeIntervalSince1970:(fecha_default as NSString).doubleValue))
        var fecha_Normal = NSDateFormatter()
        fecha_Normal.dateFormat = "dd-MM-yyyy' 'HH:mm' '"
        fecha_Normal.timeZone = NSTimeZone(forSecondsFromGMT: 0)
        var union = "Veloc. : " + Speed_Normal+" KM/H  /  Ult. Act. : " + fecha_Normal.stringFromDate(now)

        cell!.textLabel?.text = (posts1.objectAtIndex(indexPath.row).valueForKey("des") as! NSString as String) + "\n" + union

        cell!.detailTextLabel?.text = posts1.objectAtIndex(indexPath.row).valueForKey("dev") as! NSString as String
    }
    spinneroff()
    return cell!;

}

这里我把调试中的所有信息都放在

searchBar   UISearchBar 0x7873fa00  0x7873fa00
searchText  String  "F" 
_core   _StringCore     
_baseAddress    COpaquePointer  0x0000000000000000  0x0000000000000000
_countAndFlags  UWord   3221225473  3221225473
_owner  AnyObject?  Some    Some
self    AppOctagonoGPS.mtTableTableViewController   0x7a053c00  0x7a053c00
index   Int 0   0
currentString   String  "\b`\xf7\xbf\x89\xfb\f" 
_core   _StringCore     
_baseAddress    COpaquePointer  0x00000000bff75fc8  0x00000000bff75fc8
_countAndFlags  UWord   74571669    74571669
_owner  AnyObject?  Some    Some
instance_type   Builtin.RawPointer  0x78677910  0x78677910 -> 0x0284f7ac (void *)0x0284f784: __NSCFString

1 个答案:

答案 0 :(得分:2)

您说错误发生在以下行:

    var currentString = self.posts1.objectAtIndex(index) as! String

但是,您有另一行显示posts1不包含字符串:

    var speedKPH_Default = posts1.objectAtIndex(indexPath.row).valueForKey("speedKPH") as! NSString

这里你在posts1的数组元素上调用“valueForKey”方法,所以它显然不是字符串数组。