单例中的函数不起作用
A func in a Singleton does not works
正如标题所暗示的那样,我在单身人士中遇到了一个有趣的小问题。这是我的代码:
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])
}
}
但是当我要调用 class 中的方法时 Xcode 给我一个错误 "Extraneous argument label 'string' in call"。按照我的代码行:
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.chooseAttributedString(squadra.nome, font: UIFont(name: "Noteworthy-Light", size: 23)!, color: UIColor.whiteColor())
正如标题所暗示的那样,我在单身人士中遇到了一个有趣的小问题。这是我的代码:
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])
}
}
但是当我要调用 class 中的方法时 Xcode 给我一个错误 "Extraneous argument label 'string' in call"。按照我的代码行:
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.chooseAttributedString(squadra.nome, font: UIFont(name: "Noteworthy-Light", size: 23)!, color: UIColor.whiteColor())