Type 'example' has no member 'sharedInstance' - ios

I created class and I write singleton function called " example "
import UIKit
class example: NSObject {
class example {
static let sharedInstance = example()
var userInfo = (ID: "bobthedev", Password: 01036343984)
// Networking: communicating server
func network() {
// get everything
}
private init() { }
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
example.sharedInstance.userInfo
// (ID "bobthedev", Password 01036343984)
// ViewController One
example.sharedInstance.userInfo.ID // "bobthedev"
}
But I'm getting error * Type 'example' has no member 'sharedInstance' *
https://learnswiftwithbob.com/course/object-oriented-swift/singleton-pattern.html

You created a nested class.
Just skip the inner declaration:
import UIKit
class Example: NSObject {
static let sharedInstance = Example()
var userInfo = (ID: "bobthedev", Password: 01036343984)
// Networking: communicating server
func network() {
// get everything
}
private override init() { }
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Example.sharedInstance.userInfo
// (ID "bobthedev", Password 01036343984)
// ViewController One
Example.sharedInstance.userInfo.ID // "bobthedev"
}
}

You have embedded example class inside example so when you are doing example.sharedInstance it is the outer one, so just delete the outer example class.

Class name should start with capital letter.
No need to declare class inside class you need to declare variable for singleton, please check following code.
class Example {
class var sharedInstance: Example {
struct Singleton {
static let instance = Example()
}
return Singleton.instance
}
var userInfo = (ID: "bobthedev", Password: 01036343984)
// Networking: communicating server
func network() {
// get everything
}
private init() { }
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Example.sharedInstance.userInfo
// (ID "bobthedev", Password 01036343984)
// ViewController One
example.sharedInstance.userInfo.ID // "bobthedev"
}

Related

Add a generic delegate to a base class in Swift

Ideally, I want to create a BaseViewController class that takes in a protocol type (of a delegate) and have a weak variable as the delegate. Something like this:
class BaseViewController<Delegate: AnyObject> {
weak var delegate: Delegate?
init(delegate: Delegate) {
self.delegate = delegate
super.init(...)
}
}
And then inherit from a view controller like so:
protocol MyDelegate: AnyObject {
func funcA()
func funcB()
}
class SomeViewController: BaseViewController<MyDelegate> {
func doSomething() {
delegate?.funcA()
}
}
This doesn't work as the compiler complains:
'BaseViewController' requires that 'MyDelegate' be a class type
How can I work this around to achieve what I need?
Thanks in advance :)
Thats because in swift protocols doesn't confirm to them selves, you can't use "MyProtocol" as concrete type confirming to protocol "MyDelegate"
What you can rather do is
protocol MyDelegate: AnyObject {
func funcA()
func funcB()
}
class BaseViewController<Delegate: MyDelegate> {
weak var delegate: Delegate?
init(delegate: Delegate) {
self.delegate = delegate
super.init(...)
//keeping OPs code as is
}
}
class SomeOtherDelegateClass: MyDelegate {
func funcA() {
//some code here
}
func funcB() {
//some code here
}
}
class SomeViewController: BaseViewController<SomeOtherDelegateClass> {
func doSomething() {
self.delegate?.funcA()
}
}
EDIT 1:
As OP mentioned in comment, he is trying to introduce a generic property in BaseViewController that will simply hold a weak reference to any instance whose class is decided/declared by Child classes of BaseViewController using generics, I am simplifying the above answer a bit
Try this
protocol MyDelegate {
func funcA()
func funcB()
}
class BaseViewController<Delegate> where Delegate: AnyObject {
weak var delegate: Delegate?
init(delegate: Delegate) {
self.delegate = delegate
super.init(...)
//keeping OPs code as is
}
}
class SomeOtherDelegateClass: MyDelegate {
func funcA() {
//some code here
}
func funcB() {
//some code here
}
}
class SomeViewController: BaseViewController<SomeOtherDelegateClass> {
func doSomething() {
self.delegate?.funcA()
}
}
protocol MyDelegate2 {
func funcABCD()
}
class SomeOtherDelegateClass2: MyDelegate2 {
func funcABCD() {
//some code here
}
}
class SomeViewController2: BaseViewController<SomeOtherDelegateClass2> {
func doSomething() {
self.delegate?.funcABCD()
}
}
TBH, I really dont see much of benefit of this design! Probably you need to revisit the code structure and see if you can come up with better code structure :)
You should set your delegate as a constraint for the generic type T in BaseViewController:
protocol MyDelegate: AnyObject {
func funcA()
func funcB()
}
class Delegated1: MyDelegate {
func funcA() { print("A1") }
func funcB() {}
}
class Delegated2: MyDelegate {
func funcA() { print("A2") }
func funcB() {}
}
class BaseViewController<T: MyDelegate>: UIViewController {
var delegate: T?
func doSomething() {
delegate?.funcA()
}
}
class SomeViewController1: BaseViewController<Delegated1> {}
class SomeViewController2: BaseViewController<Delegated2> {}
class TestClass {
let viewController1: SomeViewController1 = {
let viewController = SomeViewController1(nibName: nil, bundle: nil)
viewController.delegate = .init()
return viewController
}()
let viewController2: SomeViewController2 = {
let viewController = SomeViewController2(nibName: nil, bundle: nil)
viewController.delegate = .init()
return viewController
}()
// prints:
// A1
// A2
func myFunc() {
viewController1.doSomething()
viewController2.doSomething()
}
}

Delegate function does not get called

I have two ViewControllers and I'm trying to set one as the other's delegate. This is what I have:
ViewController One:
protocol storeChosenDelegate {
func getPopularProductsFor(store id: String)
}
class PopularStoresVC: UIViewController {
//MARK: - Properties
var delegate: storeChosenDelegate?
private let storesView = PopularStoresView()
private let STORE_CELL = "storeCell"
fileprivate var currentStore: Int = 0 {
didSet {
delegate?.getPopularProductsFor(store: "THIS IS WORKING NOW.")
}
}
}
And this is what I have in ViewController Two:
//MARK: - Properties
private let PRODUCT_CELL = "productCell"
private var popularStores = PopularStoresVC()
//MARK: - Initializers
override func viewDidLoad() {
super.viewDidLoad()
popularStores.delegate = self
setupProductsCollection()
}
extension PopularProductsVC: storeChosenDelegate {
func getPopularProductsFor(store id: String) {
//TODO: Show all popular products for the store's id we got.
print("Got store \(id)")
}
}
It seems that the didSet is getting called, and I do set the Second VC as the delegate, but the function just does not getting called. I have no errors or warnings related to that so I don't really understand why this is not working.

Protocol implementation method not calling in Swift

I am using Xcode 10.3. I have protocol which method is not calling. What is wrong?
My first view controller with protocol:
protocol MyProtocol: class {
func doGetUpdateInfo(valu1:String,value2:String);
}
class Class_A : UIViewController{
weak var myprotocolDelegate:MyProtocol? = nil;
override func viewDidLoad() {
super.viewDidLoad()
myprotocolDelegate?.doGetUpdateInfo(account_no:value1, account_title: value2)
}
}
My second view controller
class Class_B: UIViewController,UpdateBeneficiaryProtocol {
var class_a = Class_A()
override func viewDidLoad() {
super.viewDidLoad()
class_a.myprotocolDelegate = self;
}
func doGetUpdateInfo(value1: String, value2: String) {
print("not calling****")
}
}
What is the wrong with it?
Please see the below example. you are creating a new class for A but so it will not be called. you need to provide a reference for the current class
protocol MyProtocol: class {
func doGetUpdateInfo(valu1:String,value2:String);
}
class Class_A : UIViewController{
weak var myprotocolDelegate:MyProtocol? = nil;
override func viewDidLoad() {
super.viewDidLoad()
myprotocolDelegate?.doGetUpdateInfo(account_no:value1, account_title: value2)
}
func navigateToClassB() {
let classb = Class_B()
classb.class_a = self
self.navigationController?.pushViewcontroller(classb, animated:true)
}
}
And class b should be
class Class_B: UIViewController,UpdateBeneficiaryProtocol {
var class_a : Class_A?
override func viewDidLoad() {
super.viewDidLoad()
class_a.myprotocolDelegate = self;
}
func doGetUpdateInfo(value1: String, value2: String) {
print("not calling****")
}
}
Push controller Class_b as per display in method navigateToClassB.
If you face still issue comment here I will assist you.

Get values of variables defined by protocol from any VC

protocol(myProtocol):-
protocol myProtocol {
var type:String { get set }
var sub:String { get }
var msg:String? { get set }
}
Class(myVC):-
class myVC: UIViewController, myProtocol {
//Protocol Declarations
var sub = myTypes.type.rawValue
var type = myTypes.type.getType()
var msg :String?
.... }
Extension:-
extension UIViewController
{
func getData() {
if self is myProtocol {
let msg = self.msg
} }
}
Getting error at 'self.msg' saying Value of type UIViewController has no member 'sub'
How do i go about it?
Any help is appreciated.
All you need to do is write another line of code in your extension class. I just tested it in my end and it worked for me. Here is the code I wrote -
extension UIViewController
{
func getData() {
if self is myProtocol {
let x = self as! myProtocol
let msg = x.msg
print(x.msg)
}
}
}
According to your example, you need to change your extension definition
extension UIViewController
as
extension myVC

I can't use delegate in my app from my framework

First I make a SDK about Bluetooth
ImyBluetoothManager.swift
public protocol ImyBluetoothManager: NSObjectProtocol {
var delegate: myBluetoothDelegate?{get set}
}
public class myBluetooth {
public static func getManager() -> ImyBluetoothManager? {
return myBluetoothManager.sharedInstance()
}
}
myBluetoothManager.swift
class myBluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate, myBluetoothManager {
static var instance: myBluetoothManager!
var delegate: myBluetoothDelegate? {
get {
return delegate
}
set {
delegate = newValue
}
}
private override init() {}
static func sharedInstance() ->myBluetoothManager {
if self.instance == nil {
self.instance = myBluetoothManager()
self.instance.setup()
self.instance.scanBLE()
}
return self.instance
}
Then I run it and get myBluetooth.framework,and I new a project 'ExampleApp'
ExampleApp
ViewController.swift
import myBluetooth
class ViewController: UIViewController,myBluetoothDelegate {
override func viewDidLoad() {
let test = SiltaBluetooth.getManager()
test?.delegate = self
super.viewDidLoad()
}
}
Then I run ExampleApp,and warning me.
warning: could not load any Objective-C class information. This will
significantly reduce the quality of type information available.

Resources