- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序使用 NSTask 启动 Python 脚本,然后该脚本通过 NSPipe 返回一个数组。我读取数据,将其粘贴在字符串中,然后显示它:
NSMutableData *data = [[NSMutableData alloc] init];
NSData *readData;
while ((readData = [readHandle availableData])&& [readData length]) {
[data appendData: readData];
}
NSString *string = [[NSString alloc]
initWithData: data
encoding: NSUTF8StringEncoding];
这一切都很好,但我意识到我确实需要将其保留为数组 - 而不是字符串。我找不到从数据(从 NSPipe 返回的数据)启动数组的方法。我怎样才能做到这一点?我发现的最接近的东西可能是使用:
[NSPropertyListSerialization dataWithPropertyList:format:options:error:]
...但我本身不需要“属性列表”。我必须先将数据转换为 plist 吗?
编辑:我刚刚意识到它比我想象的更复杂。 Python返回一个字典数组,字典中有字符串。这些字符串可以有逗号和其他字符,所以我认为我不能使用“,”分隔符来分隔它。
在Python中:
msg_set = []
msg_set = [
dict(mts="t,s1", mfrom="f@ro,m1", msbj="msb,j1", mbody="bod,y1", mid="i,d1"),
dict(mts="ts2", mfrom="from2", msbj="msb,j2", mbody="body2", mid="id2")
]
print msg_set # <- this is what python returns
最佳答案
你可以先将数据转换为JSON(我看到了dasblinkenlight答案的评论,但我已经输入了我的答案),然后将它们传递给Cocoa。像这样的事情:
Python端
import json
#...
json.dumps(msg_set) # <- return this one instead
Objective-C 方面
NSString *myPythonJson = @""; // <- Whatever you got from python
NSError *error = nil;
id myObjectsFromJson = [NSJSONSerialization JSONObjectWithData:[myPythonJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
关于python - NSPipe - 如何将返回的数据转换为 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12057620/
最近我一直在开发一个日志框架,我允许用户提供一个 NSPipe 来让日志下降,如果他们不希望日志消息转到控制台。 Logger 可以启用或禁用,禁用时,不应将任何数据发送到管道。 启用记录器后,很容易
是否需要释放NSPipe创建的写法 NSPipe *pipe = [NSPipe 管道]; ?实际上我没有调用 alloc 所以理论上我不应该调用release...我对吗? 最佳答案 是的,你说得对
我的应用程序使用 NSTask 启动 Python 脚本,然后该脚本通过 NSPipe 返回一个数组。我读取数据,将其粘贴在字符串中,然后显示它: NSMutableData *data = [[NS
我正在我的 OS X 应用程序中启动一些交互式进程,我希望能够从管道读取和写入。 例如:启动进程将等待用户输入命令。当用户完成时(又名按下回车键),进程将返回一些内容,然后再次等待用户。 目前,我使用
我正在为命令行应用程序编写一个非常简单的 GUI 工具。它只有 2 个按钮。 连接并退出。 在 applicationDidFinishLaunching 中,我运行以下命令 NSPipe *pipe
所以我遇到了这个问题,一旦这个 NSFileHandle/NSPipe 被触发......我的 CPU 使用和内存开始变得疯狂。问题是我发现很难找到这个泄漏。任何建议或帮助表示赞赏。干杯。 .h NS
我有一个用 Objective-C 编写的应用程序,并且正在尝试从用 C 编写的应用程序接收消息。看来我有两个选择...XPC 服务和管道。关于 NSPipe - 我正在尝试确定如何打开一个管道来监视
我需要将此部分添加到我的应用程序中,用户将在其中输入终端命令,例如“killall SpringBoard”,当用户按下按钮时,它将运行该命令。我知道如果您使用 System("") 函数,您可以运行
我正在通过网络接收编码音频流,但是混入了一堆数据,所以我需要接收数据包,去除音频然后播放音频。 AVAudioPlayer,它可能是也可能不是最好的工具,但它是我目前正在追逐的路径,需要来自 NSDa
我正在编写一个 Cocoa 应用程序,它需要执行一个 UNIX 程序并在生成时逐行读取其输出。我这样设置了 NSTask 和 NSPipe: task = [[NSTask alloc] init];
这是我的代码: task = [[NSTask alloc] init]; [task setCurrentDirectoryPath:@"/applications/jarvis/brain/"];
我正在 NSTask 对象中运行 Perl 脚本,其输出进入 NSPipe。我使用通知来定期接收其输出并更新 GUI。 Perl 脚本实际上生成了其他进程,其输出似乎没有进入此管道,但确实出现在调试器
我有一个 NSTask,它处理从 stdin 传递的参数。参数通过 NSFileHandle 传递。 它工作正常,但我们的命令行工具从 stdin 读取输入,直到收到 EOF 符号(通过按 Ctrl+
有人可以展示一个简单的例子来说明如何结合使用 NSTask 和 NSPipe 来做到这一点: Charlie AI - 通过终端运行与 AI 通信 我想使用 xcode 和 objective c 为
我正在尝试使用 NSTask 运行一个简单的 bash 脚本并将输出定向到 TextView 。执行任务后,我的应用程序的 CPU 使用率是 100%,即使它是一个简单的 echo(目前)。 我创建了
我正在尝试终止通过 NSTask 生成的 openvpn 任务。 我的问题: 我应该将 ctrl+c (SIGINT) 发送到我的 NSTask 的输入 NSPipe 吗? inputPipe = [
我是一名优秀的程序员,十分优秀!