Swift Question: Label Text

Discussion in 'Mac Programming' started by macphreak2011, Sep 22, 2014.

  1. macphreak2011, Sep 22, 2014
    Last edited: Sep 22, 2014

    macphreak2011 macrumors newbie

    Oct 7, 2011
    I am having a strange problem and I was wondering if you could help me out with it. I have this code:

    import Cocoa
    class AppDelegate: NSObject, NSApplicationDelegate {
        @IBOutlet weak var window: NSWindow!
        @IBOutlet weak var lblLeadsCount: NSTextField!
        @IBOutlet weak var lblLastChecked: NSTextField!
        func applicationDidFinishLaunching(aNotification: NSNotification?) {
            // Insert code here to initialize your application
        func applicationWillTerminate(aNotification: NSNotification?) {
            // Insert code here to tear down your application
        @IBAction func checkLeads(sender: AnyObject) {
            var url = NSURL(string: "http://*********************")
            let theRequest = NSURLRequest(URL: url!)
            NSURLConnection.sendAsynchronousRequest(theRequest, queue: nil, completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                if data.length > 0 && error == nil {
                    let response : AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
                        options: NSJSONReadingOptions.MutableContainers, error: nil)
                    lblLeadsCount.text = response
    However, when it gets to the line that says:
    lblLeadsCount.text = response
    I am getting this error:
    "'NSTextField' does not have a member named 'text'"

    Any ideas why I can't set a simple Label? Thanks!
  2. chown33 macrumors 604

    Aug 9, 2009
    on the Western Slopes, with E. A. Poe
    Maybe NSTextField doesn't have a member named 'text'.

    Maybe you're thinking of UITextField, which DOES have a member named text.

    In general, when you think the compiler is mistaken in cases like this, the first step is to check the reference documentation for the class in question. Also be sure to check superclasses.

    Here's an example with Swift code and an NSTextField:
  3. macphreak2011 thread starter macrumors newbie

    Oct 7, 2011
    For someone else looking for this answer, I found mine:

    NSTextField is correct. However, the post chown33 linked to, helped me find the correct answer (thanks chown33!).

    You need to have this line of code:
    self.lblLeadsCount.stringValue = "test";
    To get past the compiler errors. This worked for me.

    Thanks again chown33!

Share This Page