没有可访问的初始化器,Swift 单例
no accessible initializaers, Swift Singleton
更新
刚刚尝试删除
init(delegate: MPPlayerDelegate) {
self.delegate = delegate
}
并按照
告诉我的去做
var updateLabel: MPPlayerDelegate = MPPlayerSingleton()
在UIVIewcontroller.swift
但还是有问题。
MPPlayerDelegate 没有名为 delegate 的成员
我正在尝试使用委托构建单例,以便它可以使用从 http 流中获取的信息更新 uilabel "Radio"
我遇到了错误
无法构造 MPPlayerDelegate,因为它没有可访问的初始化程序
MPPlayerSingleton.swift
@objc protocol MPPlayerDelegate{
func updateLabel(artist: String, title: String)
}
public class MPPlayerSingleton : NSObject, MPPlayerDelegate {
var delegate: MPPlayerDelegate?
init(delegate: MPPlayerDelegate) {
self.delegate = delegate
}
let songInfo = NSMutableDictionary()
var moviePlayer = MPMoviePlayerController()
var uiView = UIView()
var informationMetaTitle: String!
var informationMetaArtist: String!
class var shared: MPPlayerSingleton {
struct Static {
static var instances: MPPlayerSingleton?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instances = MPPlayerSingleton(delegate: MPPlayerDelegate()) // **This is where the error is**
}
return Static.instances!
}
func updateLabel(artist: String, title: String) {
}
UIViewController.swift
var updateLabel: MPPlayerDelegate = MPPlayerDelegate() // **same error here**
updateLabel.delegate = self // says there is no delegate
我猜你的意思是:
var updateLabel: MPPlayerDelegate = MPPlayerSingleton()
您遇到的错误与单例无关,它们是因为您正在尝试创建协议实例,而这是不可能的。您可以声明一个 属性 并将协议作为其类型,但是在创建要分配给该 属性 的实例时,您需要使用符合给定协议的实际 class。
你的协议声明没问题:
@objc protocol MPPlayerDelegate {
func updateLabel(artist: String, title: String)
}
在您的 MPPlayerSingleton
class 中,您不需要带有委托的初始值设定项,因为这需要由您的客户端代码设置。摆脱它,NSObject
的空 init()
将可用,解决您的大部分问题:
public class MPPlayerSingleton : NSObject, MPPlayerDelegate {
var delegate: MPPlayerDelegate?
// ...
// Cleaned-up singleton
class var shared: MPPlayerSingleton {
struct Static {
static var instance = MPPlayerSingleton()
}
return Static.instance
}
}
现在您可以将 delegate
设置为您的视图控制器之一,在这种情况下您可能需要这样做:
class MyViewController: UIViewController, MPPlayerDelegate {
override func viewDidLoad() {
MPPlayerSingleton.shared.delegate = self
}
// MARK: - MPPlayerDelegate
func updateLabel(artist: String, title: String) {
// ...
}
}
更新
刚刚尝试删除
init(delegate: MPPlayerDelegate) {
self.delegate = delegate
}
并按照
告诉我的去做var updateLabel: MPPlayerDelegate = MPPlayerSingleton()
在UIVIewcontroller.swift
但还是有问题。 MPPlayerDelegate 没有名为 delegate 的成员
我正在尝试使用委托构建单例,以便它可以使用从 http 流中获取的信息更新 uilabel "Radio"
我遇到了错误
无法构造 MPPlayerDelegate,因为它没有可访问的初始化程序
MPPlayerSingleton.swift
@objc protocol MPPlayerDelegate{
func updateLabel(artist: String, title: String)
}
public class MPPlayerSingleton : NSObject, MPPlayerDelegate {
var delegate: MPPlayerDelegate?
init(delegate: MPPlayerDelegate) {
self.delegate = delegate
}
let songInfo = NSMutableDictionary()
var moviePlayer = MPMoviePlayerController()
var uiView = UIView()
var informationMetaTitle: String!
var informationMetaArtist: String!
class var shared: MPPlayerSingleton {
struct Static {
static var instances: MPPlayerSingleton?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instances = MPPlayerSingleton(delegate: MPPlayerDelegate()) // **This is where the error is**
}
return Static.instances!
}
func updateLabel(artist: String, title: String) {
}
UIViewController.swift
var updateLabel: MPPlayerDelegate = MPPlayerDelegate() // **same error here**
updateLabel.delegate = self // says there is no delegate
我猜你的意思是:
var updateLabel: MPPlayerDelegate = MPPlayerSingleton()
您遇到的错误与单例无关,它们是因为您正在尝试创建协议实例,而这是不可能的。您可以声明一个 属性 并将协议作为其类型,但是在创建要分配给该 属性 的实例时,您需要使用符合给定协议的实际 class。
你的协议声明没问题:
@objc protocol MPPlayerDelegate {
func updateLabel(artist: String, title: String)
}
在您的 MPPlayerSingleton
class 中,您不需要带有委托的初始值设定项,因为这需要由您的客户端代码设置。摆脱它,NSObject
的空 init()
将可用,解决您的大部分问题:
public class MPPlayerSingleton : NSObject, MPPlayerDelegate {
var delegate: MPPlayerDelegate?
// ...
// Cleaned-up singleton
class var shared: MPPlayerSingleton {
struct Static {
static var instance = MPPlayerSingleton()
}
return Static.instance
}
}
现在您可以将 delegate
设置为您的视图控制器之一,在这种情况下您可能需要这样做:
class MyViewController: UIViewController, MPPlayerDelegate {
override func viewDidLoad() {
MPPlayerSingleton.shared.delegate = self
}
// MARK: - MPPlayerDelegate
func updateLabel(artist: String, title: String) {
// ...
}
}