Since, I have started using struct
, I am wondering which is best among Class
and Struct
for creating Shared Instances.
Class
Example:
class Helper{
func isNetworkReachable(){
return reachability.isReachable
}
}
Usage:
//Below taking as a Global Instance
let helperInstance = Helper()
print(helperInstance.isNetworkReachable())
In Class
, as we can see that shared instance is created and it stays is memory till application terminate.
struct
Example:
struct Helper{
static func isNetworkReachable(){
return reachability.isReachable
}
}
Usage:
print(Helper.isNetworkReachable())
In struct
, the static
keyword plays the main role as it also specifies that the instance will stays in memory till application terminates.
In short, I want to know which is best, Class
or struct
and why?