将对象转换为字典以另存为 plist:属性列表格式无效

发布于 2024-12-10 20:14:56 字数 4356 浏览 0 评论 0原文

我想将对象数组作为 plist 文件保存到磁盘。

首先,我将对象数组转换为等效字典数组。对于某些属性来说没有问题,但有些属性会出现错误。现在我正在尝试获取 url 属性错误。 (注释行也有问题,但我现在不工作。

这是 YOEvento 类的接口。

#import <MapKit/MapKit.h>

@interface YOEvento : NSObject

{
    NSURL *url; // <url>
    NSInteger identificador; // <identificador>
    NSString *nombre; // stores the <name> tag
    NSDate *diaDeInicio; // stores the tag <dia-de-inicio>
    NSDate *diaDeFin; // stores the tag <dia-de-fin>
    NSString *entradilla; // stores the tag <entradilla>
    NSURL *foto; // <foto>
    CLLocationCoordinate2D localizacion; // <localizacion>
    BOOL isFavourite;
}

@property (nonatomic, retain) NSURL *url;
@property NSInteger identificador;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSDate *diaDeInicio;
@property (nonatomic, retain) NSDate *diaDeFin;
@property (nonatomic, retain) NSString *entradilla;
@property (nonatomic, retain) NSURL *foto;
@property CLLocationCoordinate2D localizacion;
@property BOOL isFavourite;

@end

转换数组的代码

        // 1. Convert array of YoEvento in array of Dictionary to be able to save as plist

    NSMutableArray *tempArray = [NSMutableArray array];
    for (YOEvento *evento in self.eventosFavouritesAppDel) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                  evento.url,
//                                                                  evento.identificador,
                                                                  evento.nombre,
                                                                  evento.diaDeInicio,
                                                                  evento.diaDeFin,
                                                                  evento.entradilla,
//                                                                  evento.foto,
//                                                                  evento.localizacion,
                                                                  nil]
                                                         forKeys:[NSArray arrayWithObjects:
                                                                  kURLElement,
//                                                                  kIdentificadorElement,
                                                                  kNombreElement,
                                                                  kDiaDeInicioElement,
                                                                  kDiaDeFinElement,
                                                                  kEntradillaElement,
//                                                                  kFotoElement,
//                                                                  kLocalizacionElement,
                                                                  nil]];
        [tempArray addObject:dict];
    }
        // 2. Convert the array to NSData
    NSString *errorDesc;
    NSData *data = [NSPropertyListSerialization dataFromPropertyList:tempArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];

在最后一条指令之后,我在 errorDesc 中收到错误“属性列表无效格式”。 字典里的内容看来是有效的。这是运行时内容的日志:

Printing description of dict:
<CFBasicHash 0x5b99170 [0x1298400]>{type = immutable dict, count = 5,
entries =>
    0 : <CFString 0xfd00 [0x1298400]>{contents = "entradilla"} = <CFString 0x5b99b10 [0x1298400]>{contents = "Festival Internacional de Cine de Gij\u00f3n  M\u00e1s informaci\u00f3n en  www.gijonfilmfestival.com "}
    2 : <CFString 0xfcd0 [0x1298400]>{contents = "nombre"} = <CFString 0x5b99590 [0x1298400]>{contents = "49 FICXix\u00f3n - Festival Internacional de Cine de Gij\u00f3n"}
    4 : <CFString 0xfcb0 [0x1298400]>{contents = "url"} = <CFURL 0x5b992f0 [0x1298400]>{type = 15, string = http://www.gijon.es/eventos/show/18753-49-ficxixon-festival-internacional-de-cine-de-gijon, encoding = 134217984, base = (null)}
    5 : <CFString 0xfcf0 [0x1298400]>{contents = "dia-de-fin"} = 2011-11-26 22:59:00 +0000
    6 : <CFString 0xfce0 [0x1298400]>{contents = "dia-de-inicio"} = 2011-11-18 22:59:00 +0000
}

I want to save to disk an array of objects as plist file .

First I convert the array of objects into an array of equivalent dictionaries. With some properties there is no problem but some of then give errors. Now I'm trying to get the url property error. (The commented lines are also problematic but I'm not working on then now.

HEre is the interface of YOEvento class.

#import <MapKit/MapKit.h>

@interface YOEvento : NSObject

{
    NSURL *url; // <url>
    NSInteger identificador; // <identificador>
    NSString *nombre; // stores the <name> tag
    NSDate *diaDeInicio; // stores the tag <dia-de-inicio>
    NSDate *diaDeFin; // stores the tag <dia-de-fin>
    NSString *entradilla; // stores the tag <entradilla>
    NSURL *foto; // <foto>
    CLLocationCoordinate2D localizacion; // <localizacion>
    BOOL isFavourite;
}

@property (nonatomic, retain) NSURL *url;
@property NSInteger identificador;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSDate *diaDeInicio;
@property (nonatomic, retain) NSDate *diaDeFin;
@property (nonatomic, retain) NSString *entradilla;
@property (nonatomic, retain) NSURL *foto;
@property CLLocationCoordinate2D localizacion;
@property BOOL isFavourite;

@end

The code to convert the array

        // 1. Convert array of YoEvento in array of Dictionary to be able to save as plist

    NSMutableArray *tempArray = [NSMutableArray array];
    for (YOEvento *evento in self.eventosFavouritesAppDel) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                  evento.url,
//                                                                  evento.identificador,
                                                                  evento.nombre,
                                                                  evento.diaDeInicio,
                                                                  evento.diaDeFin,
                                                                  evento.entradilla,
//                                                                  evento.foto,
//                                                                  evento.localizacion,
                                                                  nil]
                                                         forKeys:[NSArray arrayWithObjects:
                                                                  kURLElement,
//                                                                  kIdentificadorElement,
                                                                  kNombreElement,
                                                                  kDiaDeInicioElement,
                                                                  kDiaDeFinElement,
                                                                  kEntradillaElement,
//                                                                  kFotoElement,
//                                                                  kLocalizacionElement,
                                                                  nil]];
        [tempArray addObject:dict];
    }
        // 2. Convert the array to NSData
    NSString *errorDesc;
    NSData *data = [NSPropertyListSerialization dataFromPropertyList:tempArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];

After the last instructions I get in errorDesc the error "property list invalid format".
The contents of the dictionary seem to be valid. Here is a log of the contents in runtime:

Printing description of dict:
<CFBasicHash 0x5b99170 [0x1298400]>{type = immutable dict, count = 5,
entries =>
    0 : <CFString 0xfd00 [0x1298400]>{contents = "entradilla"} = <CFString 0x5b99b10 [0x1298400]>{contents = "Festival Internacional de Cine de Gij\u00f3n  M\u00e1s informaci\u00f3n en  www.gijonfilmfestival.com "}
    2 : <CFString 0xfcd0 [0x1298400]>{contents = "nombre"} = <CFString 0x5b99590 [0x1298400]>{contents = "49 FICXix\u00f3n - Festival Internacional de Cine de Gij\u00f3n"}
    4 : <CFString 0xfcb0 [0x1298400]>{contents = "url"} = <CFURL 0x5b992f0 [0x1298400]>{type = 15, string = http://www.gijon.es/eventos/show/18753-49-ficxixon-festival-internacional-de-cine-de-gijon, encoding = 134217984, base = (null)}
    5 : <CFString 0xfcf0 [0x1298400]>{contents = "dia-de-fin"} = 2011-11-26 22:59:00 +0000
    6 : <CFString 0xfce0 [0x1298400]>{contents = "dia-de-inicio"} = 2011-11-18 22:59:00 +0000
}

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

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

发布评论

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

评论(1

活泼老夫 2024-12-17 20:14:56

属性列表只能包含少量类的对象:即 NSStringNSDateNSNumberNSArrayNSDictionaryNSData。如果您想在属性列表中表示其他类型(例如 NSURL),则必须首先将其转换为允许的类型之一。

Property lists can only contain objects of a small number of classes: namely, NSString, NSDate, NSNumber, NSArray, NSDictionary, NSData. If you want to represent other types (such as NSURL) in a property list, you have to convert it to one of the permitted types first.

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