Error: Instance member cannot be used on type 'ViewController'
You are trying to use an instance (cardButtons
) of ViewController
, which is not initialised. You can use instances of ViewController
, once you view controller is loaded, i mean, inside viewDidLoad()
Look at life cycle of UIViewController
, to understand issue.
Try this and see:
class ViewController: UIViewController {
@IBOutlet var cardButtons: [UIButton]!
var game: Concentration?
//or
//var game: Concentration!
//or
//var game = Concentration()
//or
/*
var game: Concentration {
return Concentration(numberOfPairsOfCards: cardButtons.count / 2)
}
*/
override func viewDidLoad() {
super.viewDidLoad()
game = Concentration(numberOfPairsOfCards: cardButtons.count / 2)
}
}
Instance member cannot be used on type 'ViewController'
If the code you pasted is not defined in a method like viewDidLoad
, you cannot use a variable thats defined at the class level for another variable thats defined at the class level as well. These variables are determined at run time and the order they are determined is not known so fortuneArray may not exist before randomIndex is made (might not really work like this behind the scenes but you can think of it this way at least)
you should compute these variables inside viewDidLoad
or init
or some other function instead
Instance member ...cannot be used on type ViewController
The accepted answer to the link you have provided explains this issue pretty well, IMHO. Basically, in your example, you are using the three "previously" declared UITextField
s in your declaration of ingStackView
. However, these four variables are not necessarily determined in that order at runtime. Xcode gives this error because, for example, ingprice
may be determined after ingStackView
. This is just an example, there is no guarantee in the order.
When you declare each of the variables with lazy var
, the error goes away is because according to Apple's Swift Documentation:
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
This means that for lazy variables, all of them are determined at initialization, so you are guaranteed to have all of the four variable values.
Swift 3: Instance Member cannot be used on type “View Controller”
Here's the solution that works :
import UIKitclass ViewController: UIViewController {
@IBOutlet var MyImage: UIImageView! // Insert Outlet Image : "MyImage"
// Structure ChangeImage
struct ChangeImage {
private var _isChanged: Bool = false
private var _myImage: UIImageView! // Private variable _myImage
init (ImgView: UIImageView) { // Initiate ImgView called since func viewDidLoad()
self._myImage = ImgView // Private variable _myImage = ImgView ( variable MyImage in func viewDidLoad() )
}
mutating func Set_Change(val: Bool) {
if (val) {
_myImage.image = UIImage(named: "image2")
self._isChanged = true
} else {
_myImage.image = UIImage(named: "image1")
self._isChanged = false
}
}
func isChanged()-> Bool {
return self._isChanged
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var MyStruct = ChangeImage(ImgView: MyImage) // Initiate Structure ChangeImage with variable MyImage.
MyStruct.Set_Change(val: true)
print (MyStruct.isChanged())
}
}
;)
Error : Instance member cannot be used on type 'ViewController'
Not really sure, but your code should not even compile as your lazy variable instantiation of menubar says
let mbv = MenuBar(
Which it should be
let mbv = MenuBar()
Final working code would be
let menuBarView : MenuBar = {
let mbv = MenuBar()
mbv.translatesAutoresizingMaskIntoConstraints = false
mbv.backgroundColor = UIColor.green
return mbv
}()
Tested it, it works absolutely fine.
EDIT:
This is how am using it and it is working fine!
//other variable declaration
let menuBarView : MenuBar = {
let mbv = MenuBar()
mbv.translatesAutoresizingMaskIntoConstraints = false
mbv.backgroundColor = UIColor.green
return mbv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(menuBarView)
// Do any additional setup after loading the view, typically from a nib.
}
Error using Swift - Instance member cannot be used on type 'ViewController'
This code returns a new value every time you call it. I expect it what you're looking for
var myInt: Int? {
return Int(percentLabel.text!)
}
instead of
let myString = percentLabel
let myInt: Int? = Int(myString)
Instance Member Cannot Be Used on Type when pushing UIViewController
Step 1: Setup your destination class
In CarbonCalculatorResultsViewController
class, declare a var
to receive data like so:
class CarbonCalculatorResultsViewController: UIViewController {
var foo: String? {
didSet {
// What you'd like to do with the data received
print(foo ?? "")
}
}
ovevride func viewDidLoad() {
//
}
}
Step 2: Prepare data in your source class
let nextViewController = CarbonCalculatorResultsViewController()
// You have access of the variable in CarbonCalculatorResultsViewController
nextViewController.foo = <data_you_want_to_pass>
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
Then, every time the CarbonCalculatorResultsViewController
comes alive, the didSet{}
of foo
would be called.
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?
Related Topics
How to Encrypt and Decrypt a String(Plain Text) with Rsa Public Key in iOS, Swift
Cordova Xcode Build Failed "Permission Denied"
Undefined Symbols for Architecture Armv7 for Cocoapods Libraries
How to Add a Custom Separator to Uitableviewcell
Ble Device Keep Connect with iPhone When Application Terminate
String Replace Phone Number iOS Swift
Uialertcontroller Set Attributed Messages
Is There a Custom Url Scheme for the Built-In Contacts App
How to Detect Fullscreen Mode Using Avplayerviewcontroller in Swift
Installing Openssl Library for Xcode
How to Overlay One Video on Another in iOS
How to Evaluate the String Equation in iOS
Apns Push Notifications Not Working on Production
Swift: How to Draw Shapes and Detect Whether They Intersect
How to Apply Audio Effect to a File and Write to Filesystem - iOS
How to Get All Nsrange of a Particular Character in a Nsstring