由於標題暗示我發生在一個單身人士有趣的小問題。這是我的代碼:在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:我剛剛修訂了小失誤,但但它仍然沒有工作...
您忘記訪問'sharedInstance'。 'InterfaceManager.sharedInstance.chooseA..' – Jack
此外,你的單例實現是不必要的複雜,請參閱http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift/24024762#24024762 – Jack
@JackWu非常感謝,我將閱讀這篇文章! –