Set label programmatically in Xcode

Discussion in 'Mac Programming' started by ifrit05, Dec 16, 2015.

  1. ifrit05 macrumors regular


    Dec 23, 2013
    Near Detroit, MI. USA
    Heya, was just wondering how to set a label programmatically in Xcode.

    For example, I have a label set as *Placeholder*, but when the app is ran on a machine, I want the label to display the value returned of "sysctl -a | grep machdep.cpu.brand_string" (which outputs something like "
    Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz")

    Also, using awk (or a similar tool), I would like to cut out the (R), (TM), and " @ 2.00GHz".

    Would this be possible?

    Thanks for any help!
  2. any-key macrumors member

    Sep 12, 2012
    Part 1: To set a label's text in your interface you need to create an IBOutlet in your code. Then bind it to the label on the *.xib file with your Interface. Ctrl + drag from the label in the interface to the IBOutlet.

    // the outlet
    @IBOutlet weak var label: NSTextField!
    // set the text
    label.stringValue = "your text"
    This tutorial is more then you need but scroll down to the section "Living in the Past — A Past Tense Verb". The binding stuff is explained right there.

    Part 2: To get the output from a given shell command you need to execute it using an NSTask (tutorial here).
  3. cqexbesd macrumors regular

    Jun 4, 2009
    It's possible but I would highly recommend you don't call out to a shell for this - you should be able to get the same string by calling the sysctl function from your code.
  4. --R0B--, Dec 20, 2015
    Last edited: Dec 20, 2015

    --R0B-- macrumors newbie

    Sep 2, 2010
    Bury St Edmunds, UK
    You could use something like:

    #import <IOKit/IOKitLib.h>
    #import <sys/types.h>
    #import <sys/sysctl.h>
    #import <string.h>
       label.stringValue = self.CPUBrandString;
    + (NSString *)CPUBrandString
    char      buffer [256];
    size_t    sz = sizeof (buffer);
    NSString *str;
    char *p;
       if (sysctlbyname ("machdep.cpu.brand_string", buffer, &sz, NULL, 0) == 0)
          buffer [sizeof (buffer) - 1] = 0;
          // Remove everything from '@' to remove the CPU speed.
          if ((p = strrchr (buffer, '@')))
             *p = 0;
          // Turn buffer into an NSString then remove (R) and (TM) from the string.
          str = [NSStringstringWithCString:buffer encoding:[NSStringdefaultCStringEncoding]];
          str = [str stringByReplacingOccurrencesOfString:@"(R)" withString:@""];
          str = [str stringByReplacingOccurrencesOfString:@"(TM)" withString:@""];
          return (str);
        return (nil);
  5. ifrit05 thread starter macrumors regular


    Dec 23, 2013
    Near Detroit, MI. USA
    Thanks for the pointers guys. Just read over --ROB--'s post and it does make sense what he's saying.
    for the time being I figured I'd learn Swift and then try again once I get better with it.
  6. ArtOfWarfare macrumors G3


    Nov 26, 2007
    There's almost certainly a C library that will get the string for you, no need to call the shell. Probably no need to use sysctl either - I'd imagine there's something higher level than that.

Share This Page