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 of custom class
What you need there is a read only computed property:
var stringArray: [String] {
return whiskyArray.map{$0.whiskyName!}
}
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
}
...
}
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.
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 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
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
How to Compare Enum With Associated Values by Ignoring Its Associated Value in Swift
Real Time Nstask Output to Nstextview With Swift
What Does It Mean That String and Character Comparisons in Swift Are Not Locale-Sensitive
Nsurlsession Concurrent Requests With Alamofire
Photopicker Discovery Error: Error Domain=Pluginkit Code=13
The "++" and "--" Operators Have Been Deprecated Xcode 7.3
Send Data from Tableview to Detailview Swift
Why Optional Constant Does Not Automatically Have a Default Value of Nil
What Is the 'Open' Keyword in Swift
Non-'@Objc' Method Does Not Satisfy Optional Requirement of '@Objc' Protocol
Initialize @Stateobject With a Parameter in Swiftui
Deleting List Elements from Swiftui'S List
How to Generate a Random Number in a Range (10...20) Using Swift
What Is the Point of Having Two Different Names For the Same Parameter