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
}
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)])
// ^^^^^^^^
}
}
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
“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()
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
}
}
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
You just have syntax error when saying = {return self.someValue}
. The =
isn't needed.
Use :
var numPages: Int {
get{
return categoriesPerPage.count
}
}
if you want get only you can write
var numPages: Int {
return categoriesPerPage.count
}
with the first way you can also add observers as set
willSet
& didSet
var numPages: Int {
get{
return categoriesPerPage.count
}
set(v){
self.categoriesPerPage = v
}
}
allowing to use = operator
as a setter
myObject.numPages = 5
Related Topics
Saving Exif Data to Jpeg - Swift
Swift Probability of Random Number Being Selected
Swift Threading with Dispatchgroup
Xcode Swift. How to Programmatically Select Cell in View-Based Nstableview
Uicollectionview Scrolltoitematindexpath, Not Loading Visible Cells Until Animation Complete
How to Create Right Angle View in Swift
How to Replace Limited Number of Occurrences in String
Ios/Tvos Playground Fails with "Unable to Find Execution Service for Selected Run Destination"
Cant Change Navigation Bar Height iOS 11
How to Rotate an Arkit 4X4 Matrix Around Y Using Apple's Simd Library
Protocol Having Generic Function and Associatedtype
Swift Build Error_If_Any_Output_Files_Are_Specified_They_All_Must_Be
(Appkit) Tab Insertion Inside of Nstextblock
Conform to Protocol and Keep Property Private