Static member cannot be used on instance of type
You are trying to access Class level variable from the instance of that class. To use that you need to make Class level function: static func (). Try this:
static func sharedDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
Swift Error static member cannot be used on instance of type
Static members can not be accessed with Instance variables like l
and r
.
Static members must be accessed through the type (class
/struct
/enum
) name like:
GlobalSettings.rating
Why do I get Static member '...' cannot be used on instance of type '...' error?
The error message is misleading: static members can be accessed from any piece of code that has proper visibility to them, which includes instance methods.
However, Swift does not provide a short name access to static members from instance methods - a common feature of many other programming languages. This is what is causing the error above.
Swift insists on fully qualifying names of static members, as follows:
public var count : Int {
get {
return Int(RankSet.counts[Int(rankSet)])
// ^^^^^^^^
}
}
“Static member '…' cannot be used on instance of type '…'”
As you are creating and using instances of the classes you have to declare theTask
as instance method and override it (which you have to do in any case)
class BaseClass{
func theTask(){
print("do nothing")
}
}
class SubClassA: BaseClass{
override func theTask(){
print("do class A task")
}
}
class SubClassB: BaseClass{
override func theTask(){
print("do class B task")
}
}
Or the other way round put the type into the variable and call the method on the type
class BaseClass{
class func theTask(){
print("do nothing")
}
}
class SubClassA: BaseClass{
override class func theTask(){
print("do class A task")
}
}
class SubClassB: BaseClass{
override class func theTask(){
print("do class B task")
}
}
...
var theObject: BaseClass.Type = BaseClass.self
...
if mysettings.selectedPort == 1 {
theObject = SubClassA.self
}else{
theObject = SubClassB.self
}
...
let devices = theObject.theTask()
Static member 'load' cannot be used on instance of type 'AppDelegate'
Swift always calls the most specific overloaded version of a function is several could be called with the same parameters.
In your specific case, this means that the instance method will be called rather than the global function, since an instance method is more specific to that type than the global function.
Instance member cannot be used on type?
it should be;
public mutating func setPrivateLogger(imp: ((_: String) -> Void)?) {
logger = imp
}
explanation:
struct is a value type. Therefore, when you change it, you actually get a new value just like Int, String or another value type.
Because it is a value type, you cannot change it in its own instance.
"By default, the properties of a value type cannot be modified from within its instance methods" (Swift documentation)
To be able to do this, you need "mutating" behavior.
- for more info:
https://docs.swift.org/swift-book/LanguageGuide/Methods.html
if you want to set "logger" only in Struct, you can define it as "private(set) var logger" or if you want to access it only in Struct, you can define it as "private var logger"
struct Console {
static let formatter = DateFormatter()
private(set) var logger: ((_: String) -> Void)?
public static func log(_ level: Slog_ClientLog.LogLevel,_ tag: String,_ message: String) {
// other code
}
public mutating func setPrivateLogger(imp: ((_: String) -> Void)?) {
logger = imp
}
}
Instance member cannot be used on type Class?
You cannot access non-static stuff directly in a static method.
The method letKnowPersonDeinitialized
is static because it is modified with the static
modifier:
static func letKnowPersonDeinitialized() {
^
|
here!
}
The name
property of Person
is not static because it is not modified by static
.
Since non-static members belong to each individual instance of that class and static members belong to the class itself, static members have no direct access to non-static members. They can only access non-static members when an instance is present.
To solve your problem, add a parameter to the letKnowPersonDeinitialized
method:
static func letKnowPersonDeinitialized(person: Person) {
print(person.name)
}
And in the deinitializer:
deinit {
Indicator.letKnowPersonDeinitialized(self)
}
VERY IMPORTANT STUFF:
I don't think your code is designed well. This is not how you use inheritance.
Inheritance means "is a kind of". So if Indicator
inherits from Person
, it means that an indicator is a kind of person.
According to common sense, an indicator is not a person. Therefore, it is not suitable to use inheritance here. It makes little sense.
Related Topics
Geofire/Firebase Function Is Executing Handler Multiple Times in Swift
How to Save Re-Ordered Tableview Cells to Core Data
Add Catextlayer on Top of Nsimageview
Pie Chart Entries Outside Slices Have Different Position Offsets
Convert Ble Current Time to Date
How to Get Address in English Language Only Using Gmsgeocoder
Removing Scheduled Local Notification
Why Are Implicitly Unwrapped Variables Now Printing Out as Some(...) in Swift 4.1
Firebase Data Structure Best Practice for User Interaction
How to Pass a String Containing Markdown to Custom View
How to Set Default Clouse Param in View Method
How to Pause an Animation in Swiftui
Compiling for iOS 10.3, But Module 'swiftuicharts' Has a Minimum Deployment Target of iOS 13.0
Swift: Simple Dispatchqueue Does Not Run & Notify Correctly
How to Reconstruct Grayscale Image from Intensity Values