2016-03-01 96 views
0

的我想知道为什么我不断收到错误消息,未解决的标识符“ChangeInPercent”的使用。我不确定为什么我不断收到有人可以帮助的错误消息,我真的很感激。使用未解决的标识符错误代码

这是代码。

import UIKit 

class StocksTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

//1 
private var stocks: [(String,Double)] = [("AAPL",+1.5),("FB",+2.33),("GOOG",-4.3)] 
@IBOutlet weak var tableView: UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 
    //2 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "stocksUpdated:", name: kNotificationStocksUpdated, object: nil) 
    self.updateStocks() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

//UITableViewDataSource 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return stocks.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cellId") 
    cell.textLabel!.text = stocks[indexPath.row].0 //position 0 of the tuple: The Symbol "AAPL" 
    cell.detailTextLabel!.text = "\(stocks[indexPath.row].1)" + "%" //position 1 of the tuple: The value "1.5" into String 
    return cell 
} 

//UITableViewDelegate 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

} 

//Customize the cell 
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    switch stocks[indexPath.row].1 { 
    case let x where x < 0.0: 
     cell.backgroundColor = UIColor(red: 255.0/255.0, green: 59.0/255.0, blue: 48.0/255.0, alpha: 1.0) 
    case let x where x > 0.0: 
     cell.backgroundColor = UIColor(red: 76.0/255.0, green: 217.0/255.0, blue: 100.0/255.0, alpha: 1.0) 
    case _: 
     cell.backgroundColor = UIColor(red: 44.0/255.0, green: 186.0/255.0, blue: 231.0/255.0, alpha: 1.0) 
    } 

    cell.textLabel!.textColor = UIColor.whiteColor() 
    cell.detailTextLabel!.textColor = UIColor.whiteColor() 
    cell.textLabel!.font = UIFont(name: "HelveticaNeue-CondensedBold", size: 48) 
    cell.detailTextLabel!.font = UIFont(name: "HelveticaNeue-CondensedBold", size: 48) 
    cell.textLabel!.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25) 
    cell.textLabel!.shadowOffset = CGSize(width: 0, height: 1) 
    cell.detailTextLabel!.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25) 
    cell.detailTextLabel!.shadowOffset = CGSize(width: 0, height: 1) 
} 

//Customize the height of the cell 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 120 
} 

//Stock updates 
//3 
func updateStocks() { 
    let stockManager:StockManagerSingleton = StockManagerSingleton.sharedInstance 
    stockManager.updateListOfSymbols(stocks) 

    //Repeat this method after 15 secs. (For simplicity of the tutorial we are not cancelling it never) 
    dispatch_after(
     dispatch_time(
      DISPATCH_TIME_NOW, 
      Int64(15 * Double(NSEC_PER_SEC)) 
     ), 
     dispatch_get_main_queue(), 
     { 
      self.updateStocks() 
     } 
    ) 
} 

//4 
func stocksUpdated(notification: NSNotification) { 
    let values = (notification.userInfo as! Dictionary<String,NSArray>) 
    let stocksReceived:NSArray = values[kNotificationStocksUpdated]! 
    stocks.removeAll(keepCapacity: false) 
    for quote in stocksReceived { 
     let quoteDict:NSDictionary = quote as! NSDictionary 
     var changeInPercentString = quoteDict["ChangeInPercent"] as! String 
     let changeInPercentStringClean: NSString = (changeInPercentString as NSString).substringToIndex(ChangeInPercent(changeInPercentString)-1) 
     stocks.append(quoteDict["symbol"] as! String,changeInPercentStringClean.doubleValue) 
    } 
    tableView.reloadData() 
    NSLog("Symbols Values updated :)") 
} 
} 

的代码有错误的线路,

var changeInPercentString = quoteDict["ChangeInPercent"] as! String 
+1

你确定它的这条线?我认为它的下一行导致错误 – jithin

+1

你在哪里定义了'ChangeInPercent'(函数)? – Thilo

+0

对不起,是的,下一行是造成它。 @jithin –

回答

0
var changeInPercentString = quoteDict["ChangeInPercent"] as! String 
let changeInPercentStringClean: NSString = (changeInPercentString as NSString).substringToIndex((changeInPercentString as NSString).length-1) 
+0

当我输入您的代码,它说“二进制运算符‘ - ’不能适用@Pradeepķ –

+0

更新的代码我假设你正在试图跳过'changeInPercentString'的最后一个字符为如如。 'changeInPercentString'是44%,那么你正在试图获得44'changeInPercentStringClean',对吗? –

+0

是的,完全正确。 –

相关问题