gpt4 book ai didi

objective-c - Cocoa/Objective-C NSProcessInfo 怪异

转载 作者:太空狗 更新时间:2023-10-30 03:25:21 26 4
gpt4 key购买 nike

所以我刚刚开始深入研究 Objective-C、Cocoa 和 iPhone 开发。我遇到了一件我不太明白的奇怪事情。

我正在尝试获取进程信息,进程名称工作正常,但是当我尝试同时获取进程 ID 时,事情变得很奇怪。

这是我的一段代码,它显然不会向控制台输出任何内容:

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

在此代码块之后,所有其他 NSLog 调用也将被忽略。该进程不断运行并说明有关 GDB 的一些信息,我在控制台中看到了这一点:

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)

我在这里可能有点白痴,但是嘿,我以前从来没有搞砸过 Objective-C ......大声笑__无辜地吹口哨__

最佳答案

线

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

应该是

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

因为 processID 只是一个 int 而不是 Objective-C 对象

关于objective-c - Cocoa/Objective-C NSProcessInfo 怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/624325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com