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
}
}
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)
]
}()
}
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()
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?
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
How to Use Afnetworking or Sthttprequest to Make a Request of a Soap Web Service
Creating an Image Format with an Unknown Type Is an Error Objective-C Xcode 8
Swift Put Multiple Iboutlets in an Array
How to Control Avassetwriter to Write at the Correct Fps
Default Value of Maxconcurrentoperationcount for Nsoperationqueue
Uitableviewcell Without Using Deprecated Method Initwithframe:Reuseidentifier
Usage of String.Range in Swift 3.0
Passing Data Between Two Viewcontrollers (Delegate) - Swift
Uitableview Pagination - Bottom Refresh to Load New Data in Swift
iOS Uitableview with Dynamic Text and Images Rendered Together (Nsattributedstring + Images)
Ios: Usage of Self and Underscore(_) with Variable
Error: Uitableview Jump to Top with Uitableviewautomaticdimension
Itms-90809: Deprecated API Usage - Existing App That Use Uiwebview Are No Longer Accepted
Pull Notification Locally on Jailbroken Device
Using Sysctlbyname() from Swift
Choosing Units with Measurementformatter
How to Convert String to Unicode(Utf-8) String in Swift
Gamecenter iOS 9 Gamecenter Gklocalplayerlistener Methods Not Called