2016-03-01 50 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

是的,完全正確。 –

相關問題