Instance Member Cannot Be Used on Type | Closures

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

Instance member cannot be used on type | Closures

You don't have access to the instance scope when initialising properties, you can only access the static context.

Try this:

var currentWeatherCallback: Weather? -> Void { return
{ (weather: Weather?) in
self.currentTemp.stringValue = weather?.updatedTime
}
}

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 in SwiftUI Preview

It is due to static var preview,

so use either static as well

static var a = DataProvider.DataHeader(title: "a", text: "b")

or construct in place

DetailView(header: DataProvider.DataHeader(title: "a", text: "b"))

Instance member cannot be used on type - error

If you need to use the method without an instance, you need to declare the method as static.

static func checkInputs(_ user: Sign, opponent: Sign) -> String

Note: You do not seem to using the user that you pass. IMO you could skip asking for that parameter and use it as an instance method with playerChoice.

func checkInputs(opponent: Sign) -> String {
// Your logic
}

And then use it like this

playerChoice.checkInputs(opponent: randomSign())

The second error is because you are trying to return an instance of Sign instead of a String. You need to either change the return type to Sign or covert the Sign in outcome to String - outcome.text like @Larme pointed out?

How to fix Instance member cannot be used on type segmentedControl

You seem to have made a custom type named segmentedControl and calling the method on it. You need to remove or rename if there is such a declaration in your code. Better to rename it to MyCustomSegmentedControl from segmentedControl.

class segmentedControl: UISegmentedControl { ... } // rename or remove

You need to call the method on it's instance instead, like this:

let segmentedControl = UISegmentedControl()
segmentedControl.changeUnderlinePosition()

Swift Instance member cannot be used on type

The default value of a method parameter is evaluated on class scope,
not instance scope, as one can see in the following example:

class MyClass {

static var foo = "static foo"
var foo = "instance foo"

func calcSomething(x: String = foo) {
print("x =", x)
}
}

let obj = MyClass()
obj.calcSomething() // x = static foo

and it would not compile without the static var foo.

Applied to your case it means that you have to make the property which is used
as the default value static:

class supClass: UIView {
static let defaultFontSize: CGFloat = 12.0 // <--- add `static` here
}

class subClass: supClass {

private func calcSomething(font: UIFont = UIFont.systemFontOfSize(defaultFontSize)) {
//... Do something
}
}

(Note that it is irrelevant for this problem whether the property is defined in the
same class or in a super class.)

What's wrong here: Instance member cannot be used on type

The problem here is that you are using self before the class is fully initialised. You can either have a getter which will be called every time you access the variable or compute it lazily.

Here is some code:

class TableViewController: UITableViewController {
let mydate = NSDate()
var items : [(Int,Int,Int,String,NSDate)] {
get {
return [
(1, 9, 7, "A", mydate),
(2, 9, 7, "B", mydate),
(3, 9, 7, "C", mydate),
(4, 9, 7, "D", mydate)
]

}
}
}

Lazy computation:

class TableViewController: UITableViewController {
let mydate = NSDate()
lazy var items : [(Int,Int,Int,String,NSDate)] = {

return [
(1, 9, 7, "A", self.mydate),
(2, 9, 7, "B", self.mydate),
(3, 9, 7, "C", self.mydate),
(4, 9, 7, "D", self.mydate)
]

}()
}


Related Topics



Leave a reply



Submit