I have not been able to convert hoursWorked to a double inside a UITextField property I keep getting a "Cannot invoke initializer for type 'Double' with an argument list of type '(UITextField!)'"
#IBOutlet weak var hoursWorked: UITextField!
#IBAction func onEditingChange(sender: UITextField) {
var hoursWorkedAmount = Double(hoursWorked)
var salary = hoursWorkedAmount! * 25
print(salary)
}
I've tried to test everything before hand on playgrounds to get a better understanding of what I'm doing wrong but I keep getting the error.
You need to get the text field's text, not the text field itself.
Change:
var hoursWorkedAmount = Double(hoursWorked)
to:
var hoursWorkedAmount = Double(hoursWorked.text)
Better yet, change it to:
var hoursWorkedAmount = Double(sender.text)
This way your method acts on the actual text field that sent the event.
Note: I'm not fluent in Swift. You might need a ! after text.
Related
I have a question that has been answer multiple times, but none seem to fit my problem
Just trying to set the text of a label when the view loads when I get the error
import UIKit
class ProblemsViewController: UIViewController {
#IBOutlet weak var respiratoryText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
respiratoryText.text("e ")
}
}
Any help would be appreciated. Thanks!
You have
respiratoryText.text("e ")
The correct syntax is
respiratoryText.text = "e "
Calling text with parentheses and a string inside of the parentheses is like invoking a function called text that takes a parameter of type String. Such a function does not exist on UILabel.
Text is a mutable property, not a function, so you can just assign String values to it without any function calls.
#IBOutlet var firstName:UITextField?
#IBOutlet var lastName:UITextField?
let string = firstName!.text
print(string)
The output is like as below:
Optional("ohh")
How can I get the data without optional text and double quotes?
Your issue is that the text attribute of a UITextField is an Optional - this means it must be unwrapped. To do that, you add a ! to the end, which produces a String instead of a String?.
You can also conditionally unwrap an optional using the syntax if let, so here it would be
if let string = firstName!.text{
print(string) //outputs if text exists
}else{
//text didn't exist
}
All I'm trying to do is get the text input from a UITextField object on my main.storyboard. Both #IBOutlet objects show that they are linked to the correct text field objects. However, when I try to make the constant meenieOneText take on the value of the .text property of the meenieOne UITextField, I get the error message "Instance member 'meenieOne' cannot be used on type 'ViewController'.
I'm really new at this so I could be doing something simple completely wrong, but I'm at my wit's end with this because it looks right to me.
Any help is much appreciated.
Thanks in advance!
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var meenieOne: UITextField!
#IBOutlet weak var meenieTwo: UITextField!
let meenieOneText: String = meenieOne.text
Try this instead:
var meenieOneText: String = ""
override func viewDidLoad() {
super.viewDidLoad()
meenieOneText = meenieOne.text!
}
You have a problem, because in iOS the Storyboard elements, while accessible, may not have been initialised (eg. the user cannot see them) as you try to access their properties. The easiest way to ensure the elements are initialised, you would access them after viewDidLoad(). There are also other phases such as 'viewDidDisappear' and so forth. See Apple documentation on ViewControllers here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/
You can not use properties of UITextField directly in class. Write same code in any function. Its work properly.
In my app, I have a string of code that is lrs24 = (30 * Int(weight)! + 70) * Int(factor)!;. It seems like it's all OK, but the compiler says that it "cannot invoke initializer for type 'Int' with an argument list of type 'UITextField' ". What does this mean, and how can I fix it? Thanks!
PS- Here is all my code-
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var animalNum: UITextField!
#IBOutlet weak var logLabel: UILabel!
#IBOutlet weak var resetButton: UIButton!
#IBOutlet weak var weight: UITextField!
#IBOutlet weak var deh: UITextField!
#IBOutlet weak var losses: UITextField!
#IBOutlet var factor: [UITextField]!
#IBOutlet weak var lrs24: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
animalNum.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
logLabel.text = textField.text
}
// MARK: Actions
#IBAction func setDefaultLabelText(sender: UIButton) {
logLabel.text = "Default Text"
}
#IBAction func textFieldsDidEndEditing(sender: AnyObject) {
lrs24 = (30 * Int(weight)! + 70) * Int(factor)!;
//+ Int(weight)! * Int(deh)! * 10 + Int(losses)!
//(30 * &weight + 70) * &factor + &weight * &deh * 10 + &losses
}
}
The error is exactly what it says: You cannot use a UITextField as the argument when initializing an Int
Here's the code you're using in your textFieldsDidEndEditing function
lrs24 = (30 * Int(weight)! + 70) * Int(factor)!
^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | |
Defined as a UITextField! | | | |
Defined as a [UITextField]!
You're trying to tell Swift to create a new integer, Int(). To create the integer, you're giving Swift a text field, weight and factor. Where is Swift supposed to get the integer value from? A UITextField is not an integer.
To put that into perspective, it's like trying to turn an apple into an orange - it's impossible to do (unless you have dark magic, but that's a different subject). The same sort of idea applies to this - you can't turn a field where a user can enter text into a number. It just doesn't work!
You can however turn a string, for example, into an integer because it contains data that the integer can be initialized with. For example, it's quite simple to turn the string "7901" into a number using Int("7901") because you're giving Swift data that it can turn into an integer.
If you would like to get the text that was entered in the text field, you have to use the UITextField.text variable. For example, to get the number entered in the weight field, you could use
//This should only be used if you are 100% sure that the text will be a number
//If it isn't, using this code, the app will crash
var weightInteger: Int = Int(weight.text)!
//If you aren't 100% sure the input will be a number, you should use
var weightInteger: Int = Int(weight.text) ?? 0
You'll be able to do this because of the above Int initialization with a String
I would also assume that the factor variable shouldn't be a [UITextField]!, which is an array (or list) of UITextFields. Instead, it should probably be a UITextField! (of course, I might be wrong, and you may actually be storing a list of UITextFields in that variable, in which case you would have to use a for loop to get the values in the list.
If you want to set the text of a label, you have to use UILabel.text - you can't just set the UILabel to a string.
So, in the end, assuming that factor is supposed to be a UITextField! and not a [UITextField]!, you should use
let value: Int = (30 * (Int(weight.text) ?? 0) + 70) * (Int(factor.text) ?? 0)
//You may want to change the last bit
//(Int(factor.text) ?? 0) to (Int(factor.text) ?? 1)
//Which would set the factor to "1" instead of "0"
//if a non-integer or nothing is inputed
lrs24.text = "\(value)"
//this sets the text of the label to
//the value above. It has to be in the format
//"\(x)" because we have to turn x into a String.
//If you prefer, you could also use String(x)
//which would be String(value) in this example
//that's personal preference, though
Also, although this has nothing to do with the original question, here are some things you should do to your code to make it better
You don't need a ; at the end of the line - Swift automatically ends lines for you
You should avoid forcing unwrapping (the ! operator) unless you're really sure the value won't be nil. Instead of using Int(x)!, you should use Int(x) ?? 0, which will instead of crashing give a value of "0" if the initialized integer is nil.
I am passing in an array via segue which works fine. I then want to set the text of a label with the array but the resulting screen comes up blank even though the print command is showing the array. Here is the code.
class MessageViewController: UIViewController, UIViewControllerTransitioningDelegate {
var passArrayForSegue = [String]()
#IBOutlet weak var arrayforSegue: UILabel!
override func viewDidLoad() {
print(passArrayForSegue)
arrayforSegue.text = passArrayForSegue as? String
}
You are trying to set the text of arrayforSegue which is a UILabel. The .text property has type String while your passArrayForSegue is of type Array<String> (you used the alternate way of declaring an array which is [String]). An easy way to set the .text property would be to iterate over the objects of your array and append them to a string.
UPDATE
You can see how to concatenate your array into a string in this post
You can't directly set a labels text to an array when it's expecting a string. If you want to set the text to the contents of the array, you are going to need to iterate through it and get the strings from the array.