Get text in NSScrollView in Swift 4 - swift

This answer shows how to enter a string into an NSScrollView in Swift 4 (when developing Mac apps in Xcode 10):
#IBOutlet weak var myScrollView: NSScrollView!
myScrollView.documentView!.insertText("Hello world")
But how do you read this text (get the text string back into a variable)?
So similar to what stringValue does for NSTextField:
#IBOutlet weak var myTextField: NSTextField!
var myTextString = myTextField.stringValue
I'm struggling to find straightforward documentation. Apple's own documentation seems to be shallow at this point, but they are indicating that this is the right component to use:

let myTextView: NSTextView = myScrollView.documentView! as! NSTextView
let myText:String = myTextView.string

Related

macOS Swift TextField.stringValue not returning updated value until I click on something else

I apologize if this has been asked before but I couldn't find what I was looking for online over the last few hours. I'm still functional a noob with swift.
I am trying to store the stringValue of a TextField when I click a NSButton. If I click anywhere and then click on the NSButton the code works perfect but if I don't click the stringValue is still reporting the previous value.
#IBOutlet weak var NameText: NSTextField!
#IBOutlet weak var SaveChangesAccountButton: NSButton!
var selectedAccountItemNumber = NSInteger()
#IBAction func SaveAccountChanges(_ sender: Any)
{
let AccountName = NameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = AccountName
}
You have to call validateEditing() on the text field.
And please conform to the naming convention that variable and function names start with a lowercase letter and don't use NSInteger in Swift.
#IBOutlet weak var nameText: NSTextField!
#IBOutlet weak var saveChangesAccountButton: NSButton!
var selectedAccountItemNumber = 0
#IBAction func saveAccountChanges(_ sender: Any)
{
nameText.validateEditing()
let accountName = nameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = accountName
}

MacOS Swift Xcode NSTextField! convert integerValue to doubleValue

I would like to have a user input numbers and then have a calculation as the result. I have succeeded with this but I would like the user to be able to input doubles if they want too and also have the result be a double. This is my issue. I have an input TextField box and a Label result. So two Outlets. So the code I have pasted below works but only for whole numbers (ints) Hope this makes sense. Any ideas? Keep in mind I am a newbie to Swift Programming. cheers
using Xcode:
#IBOutlet weak var userInput: NSTextField!
#IBOutlet weak var result: NSTextField!
var value = 100
#IBAction func pushButtonforResult(_ sender: Any) {
result.integerValue = value / userInput.integerValue
}

Nil value when referring to IBOutlets - swift, Cocoa

I am having an issue where my NSTextField IBOutlets are showing up as nil, even though they are connected to the storyboard. In the simplified example below, I have a button that, when pressed, should list the string value of the 3 text labels.
Here is the code for the button:
import Foundation
import Cocoa
class ViewController: NSObject{
#IBAction func pushButton(sender: AnyObject) {
let oneText = Texts()
oneText.listTextFields()
}
}
Here is the code for the NSTextField list:
import Foundation
import Cocoa
class Texts: NSObject{
#IBOutlet weak var l1: NSTextField!
#IBOutlet weak var l2: NSTextField!
#IBOutlet weak var l3: NSTextField!
var textArray = [NSTextField]()
func listTextFields (){
self.textArray = [self.l1,self.l2,self.l3]
for var i = 0; i < textArray.count; i++ {
let text = textArray[i]
print(text.stringValue)
}
}
}
I have verified that the IBOutlets are all connected, but I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" message when I run the program and press the button. Looking at the debugger, it appears that the tree NSTextfields are "nil."
What am I doing wrong?
You're not loading Texts from your storyboard, so it knows nothing about your outlets. Texts() creates a new instance of the object, which you then call the method on.
You presumably have an existing Texts object somewhere in interface builder, ViewController should have an outlet to that.

Textfield and floats in Swift

I have 2 textfields. If they both has a float value bigger than 100, when you click on my button it should allow you to go to another page.
So far so good, however in my code the text field can't have either int or float or doubles...
What can I do?
As Lukas says you need to convert it to a string. If you are capturing the value in the textfield on button click, you need to convert it, like so:
if let doubleValue = Double(textField.text!) {
}
I think based on what you have said, you need to do something like this:
class ViewController: UIViewController {
#IBOutlet weak var box1: UITextField!
#IBOutlet weak var box2: UITextField!
#IBOutlet weak var Check: UIButton!
#IBOutlet weak var Page1: UILabel!
#IBAction func didPressCheckButton(sender: UIButton) {
if let stringValue = box1.text {
if let doubleValue = Double(stringValue) {
if doubleValue > 100 {
print("Navigate to next page")
}
}
}
}
}
You will need to modify the check so that you check if both text boxes have values over 100, but this is a starting point.

How to access text from UITextField Swift 2

I have Swift 2 and I can't access my textfield's text, what should I do?
#IBOutlet weak var CoolField: UITextField!
let texts = Int(CoolField.text?)
There is always the error message that Instance member CoolField cannot be used with type ViewController
You cannot define the following as class variable let texts = Int(CoolField.text?) , it doesn't work like that in Swift :
Keep this defined as it is : #IBOutlet weak var CoolField: UITextField!
Define under it as class variable : var texts : Int!
Then use it in viewDidLoad for example like : texts = Int(CoolField.text!)
Thats should work fine, Good luck !