gpt4 book ai didi

Objective-C 序列化复杂对象列表

转载 作者:行者123 更新时间:2023-12-04 05:44:32 26 4
gpt4 key购买 nike

我有一个如下所示的类列表:

@interface AISlideItem: NSObject
{
NSString* PlaceHolderName;
NSUInteger PlaceHolderID;
}
@property (nonatomic, strong) NSString* PlaceHolderName;
@property (nonatomic) NSUInteger PlaceHolderID;

@end

@interface AITitleSlideItem : AISlideItem
{
NSString* Title;
}
@property (nonatomic, strong) NSString* Title;
@end

@interface AIParagraphSlideItem : AISlideItem
{
NSMutableArray* Paragraphs;
}
@property (nonatomic, strong) NSMutableArray* Paragraphs;
@end

@interface AITextSlideItem : AISlideItem
{
NSString* Text;
}
@property (nonatomic, strong) NSString* Text;
@end

@interface AIPictureSlideItem : AISlideItem
{
NSMutableData* Picture;
}
@property (nonatomic, strong) NSMutableData* Picture;
@end

@interface AISlideContent : NSObject
{
NSString* LayoutName;
NSMutableArray* Items;
}
@property (nonatomic,strong) NSString* LayoutName;
@property (nonatomic,strong) NSMutableArray* Items;
@end

@interface ContentParaghraph : NSObject
{
NSInteger Level;
NSString* Text;
}
@property (nonatomic) NSInteger Level;
@property (nonatomic, strong) NSString* Text;
@end

我想将一个包含 AISlideContent 对象的 NSMutableArray 序列化为 json。 json 中的每个项目的名称都应该与变量的名称相同。

我怎样才能做到这一点?

这是一个示例 JSON:
{
d: [
{
Items: [
{
placeHolderName: "1",
Title: "Title Slide"
},
{
placeHolderName: "2",
Paragraphs: [
{
Level: 0,
Text: "Title content"
}
]
}
],
LayoutName: "Title"
},
{
Items: [
{
placeHolderName: "1",
Title: "Slide 2"
},
{
placeHolderName: "2",
Paragraphs: [
{
Level: 0,
Text: "Line 1"
},
{
Level: 0,
Text: "Line 2"
},
{
Level: 1,
Text: "Line 2 indented"
},
{
Level: 2,
Text: "Line 3 more indented"
},
{
Level: 0,
Text: "Line 4"
}
]
}
],
LayoutName: "Title and Content"
},
{
Items: [
{
placeHolderName: "1",
Title: "Slide 3"
},
{
placeHolderName: "3",
Picture: [

]
}
],
LayoutName: "Picture above Caption"
}
]
}

p.s.我启用了 ARC

最佳答案

你必须关注 NSCoding 协议(protocol)和覆盖encodeObjectdecodeObject方法。

Here是一个样本。

关于Objective-C 序列化复杂对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847865/

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