Reputation: 522
G'day,
I have created a simple Swift app with an NSTextField and a button. The NSTextField is preloaded with some text via the viewDidLoad method.
I want to double click and highlight a single line of text then read that line into a variable when the button is pressed. This should be straight forward but I cannot even get close and I can't find any other questions as simple as this one.
Can someone help?
//
// ViewController.swift
// ReadTextFields
//
// Created by Andrew on 5/04/2015.
// Copyright (c) 2015 Andrew. All rights reserved.
//
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var myTextField: NSTextField!
@IBAction func readTheTextField(sender: AnyObject) {
println("Read button pushed.")
// I need some code in here.
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTextField.stringValue += "To be or not to be.\n"
myTextField.stringValue += "That is the question.\n"
myTextField.stringValue += "Whether tis nobler in the mind,\n"
myTextField.stringValue += "to suffer the slings and arrows of outrageous fortune."
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Upvotes: 0
Views: 272
Reputation: 131418
Take a look at the NSScanner class, and specifically the method scanUpToCharactersFromSet. You'd give it a newline character, and have it scan one line at a time into another string.
Upvotes: 2