Objective-C arrayWithPlist (已经在 NSString 中)

发布于 2024-09-29 19:44:02 字数 270 浏览 5 评论 0原文

我有一个已经包含 pList 的 NSString。

如何将其转换为 NSArray? (无需将其保存到磁盘,只需使用 arrayWithContentsOfFile 重新加载它,然后必须将其删除。)

make arrayWithPlist 或 arrayWithString 方法在哪里? (或者我该如何制作自己的?)

 NSArray *anArray = [NSArray arrayWithPlist:myPlistString];

I have an NSString that already contains a pList.

How do I turn it into an NSArray?
(WITHOUT saving it to disk, only to reload it back with arrayWithContentsOfFile, and then have to delete it.)

Where is the make arrayWithPlist or arrayWithString method?
(Or how would I make my own?)

 NSArray *anArray = [NSArray arrayWithPlist:myPlistString];

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

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

发布评论

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

评论(1

红玫瑰 2024-10-06 19:44:02

您想要使用 NSPropertyListSerialization:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListWithData:plistData
                  options:/*unused*/0
                  format:NULL
                  error:&error];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, error ?: @"(unknown error)");
}

该特定方法是在 iOS 4.0/Mac OS X 10.6 中引入的。在这些版本之前,您将使用:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorText = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListFromData:plistData
                  mutabilityOption:NSPropertyListImmutable
                  format:NULL
                  errorDescription:&errorText];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, errorText ?: @"(unknown error)");

    /* Part of the reason this method was replaced:
     * It is the caller's responsibility to release the error description
     * if any is returned. This is completely counter-intuitive.
     */
    [errorText release], errorText = nil;
}

You want to use NSPropertyListSerialization:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListWithData:plistData
                  options:/*unused*/0
                  format:NULL
                  error:&error];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, error ?: @"(unknown error)");
}

That particular method was introduced with iOS 4.0/Mac OS X 10.6. Prior to those releases, you would use:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorText = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListFromData:plistData
                  mutabilityOption:NSPropertyListImmutable
                  format:NULL
                  errorDescription:&errorText];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, errorText ?: @"(unknown error)");

    /* Part of the reason this method was replaced:
     * It is the caller's responsibility to release the error description
     * if any is returned. This is completely counter-intuitive.
     */
    [errorText release], errorText = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文