Reputation: 9945
I have a User Class which holds the details of that particular user, authentication status and a flag that tells me wether or not my user's data has been retrieved:
// Users class
class user_Global {
static let shared_Instance = user_Global()
var is_Retrieved : Bool = false
var is_Authenticated : Bool = false
var user : users_Status = users_Status.init(name: "Global user", is_Master: false, EY_ID : "N/A", ey_Email : "N/A", UUID : "N/A")
}
// Structure
struct users_Status{
var user_Name : String!
var is_User_Master : Bool!
var user_EY_Email : String!
var user_UUID : String!
var user_EY_ID : String!
init(name : String!, is_Master : Bool!, EY_ID : String! ,ey_Email : String!, UUID : String!){
self.user_Name = name
self.is_User_Master = is_Master
self.user_EY_Email = ey_Email
self.user_UUID = UUID
self.user_EY_ID = EY_ID
}
}
Going further in my app, i have few functionalities that i only allow for the authenticated user, The problem is that this status can change any time. How do i add a listener to that variable i.e is_Authenticated
in my user_Global.shared_Instance..
to perform an operation on my subsequent classes.
I did think of using protocol-delegate method but couldn't get it operational. Somehow my protocol conforming classes are not calling the function of the protocol.
EDIT 1
class user_Global {
var refresh_Delegate : user_Data_Refresh!
static let shared_Instance = user_Global()
var is_Retrieved : Bool = false
var is_Authenticated : Bool = false{
didSet{
print(oldValue)
print(is_Authenticated)
}
}
var user : users_Status = users_Status.init(name: "Global user", is_Master: false, EY_ID : "N/A", ey_Email : "N/A", UUID : "N/A"){
didSet{
if user.is_User_Master == true{
print("The user has MASTER Permissions")
user_Data_Refresh.refresh_User_State() // This is the line where is get an error
}else{
print("Master Auth was terminated!")
}
}
}
}
// Protocol :
protocol user_Data_Refresh {
func refresh_User_State()
}
PS: user
is updated once i retrieve the value from my Database. To confirm that data is already retrieved is_Retrieved
flag.
Upvotes: 0
Views: 518
Reputation: 4159
Use property observers for this:
class user_Global {
static let shared_Instance = user_Global()
var is_Retrieved : Bool = false
var is_Authenticated : Bool = false {
didSet {
// Your code here
}
}
var user : users_Status = users_Status.init(name: "Global user", is_Master: false, EY_ID : "N/A", ey_Email : "N/A", UUID : "N/A")
}
Update
You have a protocol definition there and user_Global
defines a delegate which conforms to this protocol var refresh_Delegate : user_Data_Refresh!
(PS: Delegates usually are optionals).
The problem in the mentioned line:
user_Data_Refresh.refresh_User_State()
is that you do not use the delegate object to call the function, but instead you use its type, which is wrong. Replace it with
refresh_Delegate.refresh_User_State()
and it should work.
Note: It looks like you are moving to Swift from backend development or C/C++ or something like that. Please take your time to revise Swift API design guidelines to get in touch more with naming conventions. Your code is hardly readable.
Upvotes: 1