2015-08-21 39 views
1

由於標題暗示我發生在一個單身人士有趣的小問題。這是我的代碼:在Singleton中的func不起作用

import UIKit 

class InterfaceManager: NSObject 
{ 
    class var sharedInstance: InterfaceManager 
    { 
     get 
     { 
      struct Static 
      { 
      static var instance: InterfaceManager? = nil 
      static var token: dispatch_once_t = 0 
      } 
      dispatch_once(&Static.token) { Static.instance = InterfaceManager() 
      } 
      return Static.instance! 
     } 
    } 

    func chooseAttributedString(string: NSString, font: UIFont, color: UIColor) 
    { 
     let string: NSString = string 
     var stringMutable = NSMutableAttributedString() 
     stringMutable = NSMutableAttributedString(string: string as String , attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color]) 
    } 
} 

,但是當我要調用的方法,在類的Xcode給我一個錯誤「外來參數標籤‘字符串’呼叫」。下面我行代碼:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CellSquadreController 
     let squadra = DataManager.sharedInstance.arrayCori[indexPath.row] 

     cell.backgroundColor = UIColor.clearColor() 
     cell.nomeSquadra.attributedText = InterfaceManager.sharedInstance.chooseAttributedString(string: squadra.nome, font: UIFont(name: "Noteworthy-Light", size: 23)!, color: UIColor.whiteColor()) 
     return cell 
    } 

PS:我剛剛修訂了小失誤,但但它仍然沒有工作...

+0

您忘記訪問'sharedInstance'。 'InterfaceManager.sharedInstance.chooseA..' – Jack

+0

此外,你的單例實現是不必要的複雜,請參閱http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift/24024762#24024762 – Jack

+0

@JackWu非常感謝,我將閱讀這篇文章! –

回答

0

你忘了訪問sharedInstance

InterfaceManager.sharedInstance.chooseAttributedString(squadra.nome, font: UIFont(name: "Noteworthy-Light", size: 23)!, color: UIColor.whiteColor())

+0

我試圖在單例中實現其他樂趣,除了我的chooseAttributedString()fun之外,在tableViewController類中出現錯誤「Extraneous argument label'string'in call」...我不知道它有多可能! –

+0

這很有道理,第一個變量名不需要外部參數。我編輯了我的答案。你應該刪除'string:'標籤。 – Jack

+0

好的,那是錯誤的!你解決了我的問題,非常感謝! –