Here's my Protocol:
protocol RequestLocationAuthorizationPresenterProtocol {
mutating func handleLoad(for view: RequestLocationAuthorizationViewProtocol)
func handleGivePermissionAction()
}
Here's my protocol implementation:
struct RequestLocationAuthorizationPresenter: RequestLocationAuthorizationPresenterProtocol {
private let interactor: RequestLocationAuthorizationInteractorProtocol
private let router: RequestLocationAuthorizationRouterProtocol
private weak var view: RequestLocationAuthorizationViewProtocol!
init(with interactor: RequestLocationAuthorizationInteractorProtocol,
router: RequestLocationAuthorizationRouterProtocol) {
self.interactor = interactor
self.router = router
}
mutating func handleLoad(for view: RequestLocationAuthorizationViewProtocol) {
self.view = view
}
func handleGivePermissionAction() {
self.interactor.requestAuthorization { result in
switch result {
case .success:
self.router.goToWeatherView()
case .error:
self.view.presentAlert(with: "Error", message: "The app needs your location in order to work.")
}
}
}
}
When I call the function 'handleLoad' on my View class, it compiles perfectly. But, when I call it from a mock struct it gives me this error: "Value of type 'RequestLocationAuthorizationPresenterProtocol?' has no member 'handleLoad'"
Here's my view class:
class RequestLocationAuthorizationView: UIViewController {
private let presenter: RequestLocationAuthorizationPresenterProtocol
init(with presenter: RequestLocationAuthorizationPresenterProtocol) {
self.presenter = presenter
super.init(nibName: "RequestLocationAuthorizationView", bundle: .main)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func presentAlert(with title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
}
#IBAction func givePermissionButtonPressed(_ sender: Any) {
self.presenter.handleGivePermissionAction()
}
}
And here's my mock struct:
class RequestLocationAuthorizationViewMock: RequestLocationAuthorizationViewProtocol {
private let expectation: XCTestExpectation!
private let expectedTitle: String!
private let expectedMessage: String!
private let presenter: RequestLocationAuthorizationPresenterProtocol!
init(with expectation: XCTestExpectation? = nil,
expectedTitle: String? = nil,
expectedMessage: String? = nil,
presenter: RequestLocationAuthorizationPresenterProtocol? = nil) {
self.expectation = expectation
self.expectedTitle = expectedTitle
self.expectedMessage = expectedMessage
self.presenter = presenter
}
func callPresenterHandleLoad() {
self.presenter.handleLoad(for: self)
}
func callPresenterHandleGivePermissionAction() {
self.presenter.handleGivePermissionAction()
}
func presentAlert(with title: String, message: String) {
if title == self.expectedTitle && message == self.expectedMessage {
self.expectation.fulfill()
}
}
}
When I change my implementation to be a class instead of a struct and remove the mutating word, it also works perfectly. I've tried to look for similar errors, but I had no luck. I'm on Xcode 10.1 and using Swift Compiler 4.2.
Any thoughts about this issue are welcome.
After looking more closely to the issue, I realize that I'm trying to call a mutating function a constant value (let) and that's why it was not working. The problem here was the compiler giving me a non-sense error. I changed my property from let to var and now it works.
presenter is an optional (even an implicit unwrapped optional is an optional), you have to add a question mark for optional chaining
func callPresenterHandleLoad() {
self.presenter?.handleLoad(for: self)
}
But the error is misleading, now you get the real error
Cannot use mutating member on immutable value: 'presenter' is a 'let' constant
so you have to declare presenter as variable and – highly recommended – as regular optional
private var presenter: RequestLocationAuthorizationPresenterProtocol?
Related
I am trying to understand more protocols. I could not figure out myProtocolFunction never call and print or passing the variable as below.
I think my problem is about initialization. When I try with two viewcontroller everything is okay but MyStruct protocol instance is nil. Thanks for your help.
class ViewController: UIViewController, MyProtocol {
var myProtocolValue: String = ""
var globalInstance = "a"
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func click(_ sender: Any) {
var myStruct = MyStruct()
myStruct.myProtocol = self
}
func myProtocolFunction(passing: String) {
globalInstance = passing
print("never print")
}
}
protocol MyProtocol {
func myProtocolFunction(passing: String)
}
struct MyStruct {
var myProtocol: MyProtocol?
init() {
makeSomething()
}
func makeSomething() {
myProtocol?.myProtocolFunction(passing: "abc")
}
}
1- You need to make
var myStruct = MyStruct() // an instance variable
#IBAction func click(_ sender: Any) {
myStruct.myProtocol = self
}
2- Also you call init before even you set myProtocol as its called when you do var myStruct = MyStruct() and at that time myProtocol is nil as its not yet set
init() {
makeSomething()
}
Check full working edit
class ViewController: UIViewController, MyProtocol {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func myProtocolFunction(passing: String) {
print(passing)
}
#IBAction func click(_ sender: Any) {
var myStruct = MyStruct(self)
}
}
protocol MyProtocol {
func myProtocolFunction(passing: String)
}
struct MyStruct {
var myProtocol: MyProtocol?
init(_ mine:MyProtocol) {
myProtocol = mine
makeSomething()
}
func makeSomething() {
myProtocol?.myProtocolFunction(passing: "abc")
}
}
Think about protocols as a type, in your struct you are basically saying
that this struct have a variable of this type ( your protocol )
and on init you are trying to call its function.
What you're missing is setting an instance of type ( your protocol )
so it can get called.
for instance :
MyStruct = MyStruct(myProtocol: ViewController())
Now your nullable protocol have a value and can call it's function in another word, protocols can't have implementation directly into them they're more like a signature contract
Read more about protocols here.
You need to create MyStruct() object out of the click action else when the scope is over your object is deinitialize.
Also, You have to call .makeSomething() function on the button click as the time of object creation delegate is nil.
var myStruct = MyStruct() // Create an instance here
#IBAction func click(_ sender: Any) {
myStruct.myProtocol = self
// Add your action
myStruct.makeSomething()
}
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.
I simply have such protocols:
protocol Containerable {
var containerView: UIView { get }
var containerController: UIViewController { get }
var oldViewController: UIViewController? { get set }
}
protocol ContainerRoutable: class {
func load(controller: UIViewController, into context: inout Containerable)
}
extension ContainerRoutable {
func load(controller: UIViewController, into context: inout Containerable) {
context.oldViewController?.willMove(toParent: nil)
context.oldViewController?.view.removeFromSuperview()
context.oldViewController?.removeFromParent()
controller.view.frame = context.containerView.bounds
context.containerController.addChild(controller)
context.containerView.addSubview(controller.view)
context.oldViewController = controller
controller.didMove(toParent: context.containerController)
}
func loadDashboard(into context: inout Containerable) {
let controller = assembler.resolve(DashboardViewController.self)!
load(controller: controller, into: &context)
}
}
and now on the tap action I need to use it:
mainView.dashboardButton.rx.tap.bind { [weak self] in
self?.mainView.activateDashboardMenuItem()
if var a = self as? Containerable { //warning: Conditional downcast from 'TabBarController?' to 'Containerable' is equivalent to an implicit conversion to an optional 'Containerable'
self?.router.loadDashboard(into: &a)
}
}.disposed(by: bag)
What is self?
class TabBarController: UIViewController, Containerable {
private let mainView: TabBarView
let router: TabBarRoutable
private let bag = DisposeBag()
var oldViewController: UIViewController?
var containerController: UIViewController {
return self
}
var containerView: UIView {
return mainView.containerView
}
}
How to remove the following warning?
Conditional downcast from 'TabBarController?' to 'Containerable' is equivalent to an implicit conversion to an optional 'Containerable'
Update the if-condition as,
if var a: Containerable = self {
self?.router.loadDashboard(into: &a)
}
I want to call the label from ViewController and set it at different points in the code for example there are 2 different functions and both of them will set the label to lets say "hi" and 2nd function will set it to "hello"
I used Swift3 protocol way just like this: Access label.text from separate class in swift
I have another init in the class so I'm not sure why it would not set the label text to new values but is nil.
I hit the connect button first.
Here is my code:
protocol HiResponder: class {
func hi()
func hello()
}
class ViewController: UIViewController, HiResponder {
#IBOutlet weak var status: UILabel!
var test: Test!
override func viewDidLoad() {
super.viewDidLoad()
test.responder = self //gives me nil fatal error: unexpectedly found nil while unwrapping an Optional value
}
#IBAction func connectBtn(_ sender: Any) {
self.test = Test(
p1: "hey",
p2: "there"
)
self.test.connect(p1: "hey", p2: "there")
}
func hi() {
status.text = "hi"
}
hello() {
status.text = "hello"
}
}
This is the class that sets the value of these responders:
class Test {
var test:TestQQ?
var p1:String?
var p2: String?
weak var responder: HiResponder?
init(p1: String, p2:String) {
self.p1 = p1
self.p2 = p2
}
init(responder: TestResponder) {
self.responder = responder
}
// Connect
func connect(p1:String, p2:String) {
//code
}
func setHello(){
responder?.hello()
}
func setHi(){
responder?.hi()
}
}
I tried to generalize my code but thats the idea. My connect function is being called in my viewDidload of viewController.
You didn't instantiate test. Since viewdidload will run first it is of course nil. Therefore, you cannot acces anything from it. What you need to do is find a spot to instantiate it first before you use it. Maybe in connectBTN you can set the responder after you create it instead lf in viewdidload
The solution to deal with the unwrapped nil Optional value is to set test as an Optional Test object.
There is another problem though. You never actually use either of the functions from the protocol. hi() or hello()
Below I used both functions from the protocol but not from another class. In fact I didn't use Test at all. Not sure what your goal was with using a different class to set the Label text.
protocol HiResponder: class {
func hi()
func hello()
}
class ViewController: UIViewController, HiResponder {
#IBOutlet weak var status: UILabel!
var num = 1
var test: Test?
override func viewDidLoad() {
super.viewDidLoad()
test?.responder = self //gives me nil fatal error: unexpectedly found nil while unwrapping an Optional value
}
#IBAction func connectBtn(_ sender: Any) {
if num % 2 == 0 {
hi()
} else {
hello()
}
num += 1
}
func hi() {
status.text = "hi"
}
func hello() {
status.text = "hello"
}
}
class Test {
var test: Test?
var p1: String?
var p2: String?
weak var responder: HiResponder?
init(p1: String, p2:String) {
self.p1 = p1
self.p2 = p2
}
init(responder: HiResponder) {
self.responder = responder
}
// Connect
func connect(p1:String, p2:String) {
//code
}
func setHello(){
responder?.hello()
}
func setHi(){
responder?.hi()
}
}
So basically I want to enable Sinch Video in iOS application.
For testing purposes I've created SinchManaevger which is singleton and I instatiate it in AppDelegate:
class SinchManager: NSObject, SINClientDelegate, SINCallClientDelegate {
static let sharedInstance = SinchManager()
var client: SINClient?
func initSinchClientWithUserId(id: String) {
if client == nil {
if case .Authenticated(let currentUser, _) = SessionManager.sharedInstance.state.value {
self.client = Sinch.clientWithApplicationKey("xyz", applicationSecret: "xyz", environmentHost: "sandbox.sinch.com", userId: currentUser.username)
print("sinchClient")
print(client!)
self.client!.delegate = self
self.client!.setSupportCalling(true)
self.client!.enableManagedPushNotifications()
self.client!.start()
self.client!.startListeningOnActiveConnection()
}
}
}
func clientDidStart(client: SINClient!) {
print("clientDidStart")
self.client!.callClient().delegate = self
}
func clientDidStop(client: SINClient!) {
print("clientDidStop")
}
func clientDidFail(client: SINClient!, error: NSError!) {
print("clientDidFail")
}
func client(client: SINCallClient!, didReceiveIncomingCall call: SINCall!) {
print("didReceiveIncomingCall")
let sinchVC = SinchVC(username: currentUser.username)
let sinchNC = DNMMainNC(rootViewController: sinchVC)
sinchVC.call = call
}
}
And I've created Sinch ViewController which is initialized with username which will be called:
class SinchVC: UIViewController, SINCallDelegate {
private let videoController = SinchManager.sharedInstance.client!.videoController()
private let audioController = SinchManager.sharedInstance.client!.audioController()
private let callClient: SINCallClient
private var call: SINCall!
let username: String
private var mainView: SinchView { return view as! SinchView }
override func loadView() {
view = SinchView()
}
init(username: String) {
self.username = username
self.callClient = SinchManager.sharedInstance.client!.callClient()
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
call.delegate = self
self.mainView.videoView.addSubview(self.videoController.localView())
self.videoController.localView().contentMode = .ScaleToFill
if self.call.direction == SINCallDirection.Incoming {
self.audioController.startPlayingSoundFile(self.pathForSound("incoming.wav") as String, loop: true)
}
if self.call.details.videoOffered {
print("video offered")
self.mainView.videoView.addSubview(self.videoController.localView())
self.videoController.localView().contentMode = .ScaleToFill
}
mainView.videoView.addSubview(self.videoController.localView())
mainView.answerButton.addTarget(self, action: #selector(answer), forControlEvents: .TouchUpInside)
mainView.declineButton.addTarget(self, action: #selector(decline), forControlEvents: .TouchUpInside)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.audioController.enableSpeaker()
}
func pathForSound(string: String) -> NSString {
let nsSt = NSBundle.mainBundle().resourcePath! as NSString
return nsSt.stringByAppendingPathComponent(string)
}
func answer() {
call.answer()
}
func decline() {
call.hangup()
}
func callDidEstablish(call: SINCall!) {
print("callDidEstablish")
}
func callDidEnd(call: SINCall!) {
print("callDidEnd")
}
func callDidProgress(call: SINCall!) {
print("callDidProgress")
self.audioController.startPlayingSoundFile(self.pathForSound("ringback.wav") as String, loop: true)
}
func callDidAddVideoTrack(call: SINCall!) {
print("callDidAddVideoTrack")
mainView.videoView.addSubview(self.videoController.remoteView())
}
}
Problem is when I try to call from my app to other phone with my app nothing happens (didReceiveIncomingCall delegate method doesn't get called at all)
If I try to call from my app to SinchVideo sample app then video call gets initiated normal. But when i call from SinchVideo app to my app nothing happens in my app. So probably i've forgot to add some notification or something to tell my app when the call is incoming. If you could help I would be very grateful. Thanks
EDIT: I managed to make didReceiveIncomingCall work but now call.answer isnt working. (nothing happens when call.answer is called and i see that my phone is ringing)
I am not sure what DNMMainNC does in your did recieve incoming call,
let sinchNC = DNMMainNC(rootViewController: sinchVC) What does DNMMainNC do?
sinchVC.call = call // private var?
But its looks kind of weird to set a private var call from your code, should that not be public or have a constructor like your init but with a call