Instance Member Cannot Be Used on Type Class

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 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.

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


}()
}

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

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
}

...
}


Related Topics



Leave a reply



Submit