Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ifrit05

macrumors 6502a
Original poster
Dec 23, 2013
548
385
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!
 
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!

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.

Code:
// 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).
 
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".

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.
 
You could use something like:

Code:
#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);
}
 
Last edited:
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.