views:

2017

answers:

5

So I've just started digging into Objective-C, Cocoa, and iPhone development. I've ran into a little weird thing which I don't really get tho.

I'm trying to get process information, process name works fine, but things get weird when I try to also get the process ID.

Here's the piece of code I have, which evidently doesn't output anything to the console:

NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSString *processName = [processInfo processName];
int processID = [processInfo processIdentifier];
NSLog(@"Process Name: '%@' Process ID:'%@'", processName, processID);

After this block of code, all other NSLog calls are also ignored. And the process keeps running stating something about GDB, and I get this in the console:

Loading program into debugger… GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-apple-darwin".Program loaded. sharedlibrary apply-load-rules all Attaching to program: '/Users/jim/Documents/Xcode Projects/CS 193P/Assignment1B (WhatATool)/WhatATool/build/Debug/WhatATool', process 28700. (gdb)

I'm probably a bit of an idiot here, but hey, I've never messed with Objective-C before... lol _*whistles innocently*_

+15  A: 

The line

NSLog(@"Process Name: '%@' Process ID:'%@'", processName, processID);

should be

NSLog(@"Process Name: '%@' Process ID:'%d'", processName, processID);
/*                          change here ^                          */

as processID is just a int and no Objective-C object

epatel
Thanks, I just tested it :DI'd almost forgotten how it feels to be a n00b, it's kinda fun, and a little embarrassing... lol
jimeh
you also could have used stringWithFormat, that's how I did the assignment.
Jack Marchetti
A: 

Maybe somthing like this without using the NSLOG and just use printf: You may have to clean up the output but it should compile and work. Not sure if it is the way it should be done, but seems to work from my testing.

void PrintProcessInfo() {

int pid = [[NSProcessInfo processInfo] processIdentifier];
printf("The ProcessInfo is: %d \n", pid);

}

A: 

"int" is not Objective-C, so delete the * after "int" and it should be fine.

You may want to note that you are actually commenting on Fredrick's non-answer, not answering the original question yourself. Also, Sören Kuklau already pointed out his problem in a comment on that non-answer.
Peter Hosey
A: 

I ran into the same problem, though I was attempting to solve it by figuring out how to put the value of an int into an NSString. Still haven't figured out how to do that.

A: 

"do %i" for integers.