Swift:从NSTextField获取一行

时间:2015-04-05 10:40:31

标签: swift xcode6

G'天,

我创建了一个带有NSTextField和按钮的简单Swift应用程序。 NSTextField通过viewDidLoad方法预加载了一些文本。

我想双击并突出显示单行文本,然后在按下按钮时将该行读入变量。这应该是直截了当的,但我甚至无法接近,我找不到像这一样简单的任何其他问题。

有人可以帮忙吗?

//
//  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.
        }
    }


}

1 个答案:

答案 0 :(得分:2)

查看NSScanner类,特别是scanUpToCharactersFromSet方法。你给它一个换行符,并让它一次扫描一行到另一个字符串。