PerlObjCBridge 从字符串读取 plist 的更好方法(由于不正确的 NSStringEncoding 值 0x0000 编码错误)?

发布于 2024-12-26 10:18:19 字数 1775 浏览 1 评论 0原文

此代码片段是一个示例,适用于任何 Mac OS X 系统。

   #!/usr/bin/perl
use Foundation;
my $dscl = "/usr/bin/dscl";

sub getGeneratedUID(){
  my $name = shift;
  my $command = "$dscl -plist . -read /Users/$name GeneratedUID";
  print "$command\n";
  my $output = `$command`;
  my $plist = NSString->alloc()->initWithCString_encoding_($output,NSStringEncodingASCII);
  my $data = $plist->dataUsingEncoding_(NSStringEncodingASCII);
  my $record = NSPropertyListSerialization->propertyListWithData_options_format_error_($data,NSPropertyListImmutable,0,0);
  my $array = $record->objectForKey_('dsAttrTypeStandard:GeneratedUID');
  return $array->objectAtIndex_(0)->description()->UTF8String();
}
my $logname = $ENV{'LOGNAME'};
my $guid = &getGeneratedUID($logname);
print "My GUID: $guid\n";

因此,我正在为 Mac OS X 目录服务命令行 (dscl) 实用程序编写一个 perl 包装器。由于站点要求,它必须是 perl,并且由于多年来该命令的输出略有改变,我正在使用它的功能将其输出格式化为 Apple Propertly 列表。多年来,我在其他几种语言中使用了 NSPropertyListSerialization->propertyListWithData,一般想法是,如果将字符串转换为数据,这将从字符串中为您创建一个 NSDictionary,因此问题的标题是否有解析 plist 输出的更好方法。到目前为止,这段代码是有效的,尽管它确实给出了以下错误:

2012-01-10 10:57:31.270 perl5.12[1876:1507] Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.

我意识到这是这一行

  my $plist = NSString->alloc()->initWithCString_encoding_($output,NSStringEncodingASCII);

所以如果我能找出编码错误那么我认为这种方式对我来说效果很好,但是如果你有更好的建议解析这些信息我很想听听。然而,我真的很想弄清楚我应该使用什么编码或方法来桥接两个字符串,我希望这是一个简单的问题,因为我发现了类似的问题,但没有任何东西可以使用桥来处理这种错误。

到目前为止,我已经尝试删除分配:

  my $plist = NSString->stringWithCString_($output);

我意识到这个错误不是致命的错误,但在我弄清楚之前我宁愿不使用这种方法。

This code snippet is an Example that should work on any Mac OS X system.

   #!/usr/bin/perl
use Foundation;
my $dscl = "/usr/bin/dscl";

sub getGeneratedUID(){
  my $name = shift;
  my $command = "$dscl -plist . -read /Users/$name GeneratedUID";
  print "$command\n";
  my $output = `$command`;
  my $plist = NSString->alloc()->initWithCString_encoding_($output,NSStringEncodingASCII);
  my $data = $plist->dataUsingEncoding_(NSStringEncodingASCII);
  my $record = NSPropertyListSerialization->propertyListWithData_options_format_error_($data,NSPropertyListImmutable,0,0);
  my $array = $record->objectForKey_('dsAttrTypeStandard:GeneratedUID');
  return $array->objectAtIndex_(0)->description()->UTF8String();
}
my $logname = $ENV{'LOGNAME'};
my $guid = &getGeneratedUID($logname);
print "My GUID: $guid\n";

So I am writing a perl wrapper for the Mac OS X Directory Service Command Line (dscl) utility. It has to be perl due to the site requirements , and due to the fact that this command has changed its output slighly over the years I am using the ability for it to format its output as an Apple Propertly list. I have used the NSPropertyListSerialization->propertyListWithData in a couple other languages over the years, general idea being that if you convert the string to data this will create a NSDictionary for you from the string, thus the title of the question, is there a better way to parse the plist output. So far this code works, though it does give the following error:

2012-01-10 10:57:31.270 perl5.12[1876:1507] Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.

Which Googling around I realize is this line

  my $plist = NSString->alloc()->initWithCString_encoding_($output,NSStringEncodingASCII);

So If I can figure out the encoding error then I think this way will work pretty well for me, but if you have a better suggestion of parsing this info I would love to hear it. However I would really like to figureout what encoding or method I should be using to bridge the two strings which I hope is a simple question as I have found similar questions but nothing that deals with this kind of error using the bridge.

So Far I have tried removing the alloc:

  my $plist = NSString->stringWithCString_($output);

I realize this error is not a fatal one, but I would rather not use this methodlogy until I figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-02 10:18:19

它是 NSASCIIStringEncoding,而不是 NSStringEncodingASCII

It's NSASCIIStringEncoding, not NSStringEncodingASCII.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文