Instance Member Cannot Be Used on Type

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?

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"))

backup

Instance member '....' cannot be used on type 'CustomTextField'; did you mean to use a value of this type instead?

I distilled the problem down a bit and recreated it, so you'll want to add back the code I removed. This code produces the same error:

class CustomTextField {
var leftIcon: String = ""

private let leftIconView: UIImageView = {
let icon = UIImage(systemName: leftIcon) // <--- Error here
let imageView = UIImageView(image: icon)
return imageView
}()
}

And tracked down the problem.

The problem is that initializing leftIconView is actually happening in a static context before the initializer is called, so you can't refer to instance properties. I found two easy solutions, the first is make leftIconView a lazy var instead of a let:

class CustomTextField {
var leftIcon: String = ""

private lazy var leftIconView: UIImageView = {
let icon = UIImage(systemName: leftIcon)
let imageView = UIImageView(image: icon)
return imageView
}()
}

The second solution is to just initialize leftIconView in the initializer.

class CustomTextField {
var leftIcon: String = ""

private let leftIconView: UIImageView

override init()
{
let icon = UIImage(systemName: leftIcon)
self.leftIconView = UIImageView(image: icon)
}
}

This second solution will probably require that you also implement some other initializers for UIView once you add back that inheritance, and of course you'll need to call super.init at the end of the initializer.

Addendum

The above solution did indeed fix the compile error, but actually making the icon appear in the CustomTextField required using a property observer on leftIcon

class CustomTextField: UITextField, UITextFieldDelegate {
//"person"
private var showPassword: Bool = false
var leftIcon: String = ""
{
didSet
{
leftIconView = makeImageView(for: leftIcon)

// leftView is the view that is actually displayed. It's set
// once in the initializer, so now that we're updating
// leftIconView, we have to update leftView as well
leftView = leftIconView

// Maybe also do this
leftView?.setNeedsDisplay()
}
}

private lazy var leftIconView: UIImageView = {
makeImageView(for: leftIcon)
}()

private func makeImageView(for imageName: String) -> UIImageView {
let icon = UIImage(systemName: imageName )
let imageView = UIImageView(image: icon)
imageView.contentMode = .scaleAspectFit
imageView.tintColor = .white
return imageView
}

...
}

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?

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)
]


}()
}

On awakeFromNib() - Error Instance member 'button' cannot be used on type 'CustomView'

When typing awakeFromNib, I used the autocomplete provided by XCode to complete the function, which resulted in the below code:

override class func awakeFromNib() {
}

Notice the class in the func declaration. This was causing the error:
I removed it and the code worked fine. Thought this would help someone.



Related Topics



Leave a reply



Submit