Static Func not calling variable

Hi developers, im doing a framework with this function


public func LoginGenerateToken(userName: String, password: String) {
       
       
        let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
        LoginSingleton.shared.mailUserLogin = userName
        networkManager1 = URL_Session()
        networkManager1?.delegate = self
        networkManager1?.LoginGenerateTokenPOST(parameters: parameters)

       
    }


so when the user install the framework and call this function it should ask for 'userName' and 'password'


but it doesn't because is not an static function

the function get call like this


LoginWS.LoginGenerateToken()


insted of


LoginWS.LoginGenerateToken(userName: String, password: String)


i can't set static because im calling a delegate outside the func LoginGenerateToken


networkManager1?.delegate = self


how could i get the function to work without using static?

this is the full code


//
//  LoginWS.swift
//  IntegradorFrameWork
//
//  Created by BlumonPay on 11/27/19.
//  Copyright © 2019 BlumonPay. All rights reserved.
//

import Foundation
import CommonCrypto

public class LoginWS: URL_SessionDelegate{
    var networkManager1: URL_Session?
   
     func connectionFinishWithError(session: URL_Session, error: Error) {
        print(error)
    }
     func connectionFinishSuccessfull(session: URL_Session, response: NSDictionary) {
       
        print(response)
        let dictionaryValue = response as? NSDictionary
        let responseError = dictionaryValue?["error"] as? String
        let errorDescription = dictionaryValue?["error_description"] as? String
       
        if responseError?.count == nil {
           
            let dictionaryValue = response as? NSDictionary
            let tokenAcceso = dictionaryValue?["access_token"] as! String
            print("Access Token: \(tokenAcceso)")
           
        }else{
            let responseError = dictionaryValue?["error"] as! String
            let errorDescription = dictionaryValue?["error_description"] as! String
            print("ERROR: \(errorDescription)")
        }
  
    }
   

    public  func LoginGenerateToken(userName: String, password: String) {
       
       
        let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
        LoginSingleton.shared.mailUserLogin = userName
        networkManager1 = URL_Session()
        networkManager1?.delegate = self
        networkManager1?.LoginGenerateTokenPOST(parameters: parameters)

       
    }
   


    }


extension String {
   
    func sha256() -> String{
        if let stringData = self.data(using: String.Encoding.utf8) {
            return hexStringFromData(input: digest(input: stringData as NSData))
        }
        return ""
    }
   
    private func digest(input : NSData) -> NSData {
        let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
        var hash = [UInt8](repeating: 0, count: digestLength)
        CC_SHA256(input.bytes, UInt32(input.length), &hash)
        return NSData(bytes: hash, length: digestLength)
    }
   
    private  func hexStringFromData(input: NSData) -> String {
        var bytes = [UInt8](repeating: 0, count: input.length)
        input.getBytes(&bytes, length: input.length)
       
        var hexString = ""
        for byte in bytes {
            hexString += String(format:"%02x", UInt8(byte))
        }
       
        return hexString
    }
   
}



delegate method


@objcprotocol URL_SessionDelegate {
    func connectionFinishSuccessfull(session: URL_Session, response: NSDictionary) //Response del mismotipo que devuelve el JSON o utilizar any para cachar cualquiera de los 2
    func connectionFinishWithError(session: URL_Session, error: Error)
    @objc optional func connectionFinishSuccessfull2(session: URL_Session, response: NSDictionary)
    @objc optional func connectionFinishSuccessfull3(session: URL_Session, response: NSDictionary)
    @objc optional func connectionFinishSuccessfull4(session: URL_Session, response: NSDictionary)
}

  class URL_Session: NSObject, URLSessionDelegate, URLSessionDataDelegate {
   
    var dataTask: URLSessionDataTask? //Descarga los bytes, evalua la respuesta del servidor
    var responseData: Data = Data() //Respuesta del lado del servidor
    var httpResponse: HTTPURLResponse?
    static var delegate: URL_SessionDelegate?
    var delegat2: URL_SessionDelegate?
    var delegat3: URL_SessionDelegate?
var delegat4: URL_SessionDelegate?

Why don't you just declare


static func LoginGenerateToken(userName: String, password: String) {

it gives an error


static func LoginGenerateToken(userName: String, password: String) {
       
       
        let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
        LoginSingleton.shared.mailUserLogin = userName
        networkManager1 = URL_Session()
        networkManager1?.delegate = self
        networkManager1?.LoginGenerateTokenPOST(parameters: parameters)

       
    }


because networkManager1

it's outside the function i should declare like this

publicstaticfunc LoginGenerateToken(userName: String, password: String) {
        var networkManager1: URL_Session?
       
        let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
        LoginSingleton.shared.mailUserLogin = userName
        networkManager1 = URL_Session()
        networkManager1?.delegate = self
        networkManager1?.LoginGenerateTokenPOST(parameters: parameters)

       
    }

but i cant use the delegate method if i do it this way

it gives an error

What error exactly ?


but i cant use the delegate method if i do it this way

Do you get an error message or just the delegate not being set (which is logical).


var just creates a local var but will not be visible outside the func (in fact it is deleted when you exit the func).


So you could declare var networkManager1: URL_Session? at the global scope, or in a singleton for the app.


for declaration of LoginGenerateToken

- public should not be needed, it is public by default

- names of func should start with lowercase login not Login

- Finally, you have an editor problem with publicstaticfunc all attached. Does that mean you did not copy the exact code but retyped it ? You should avoid this, that's error prone and may lead to wrong analysis of issues.

Static Func not calling variable
 
 
Q