xcode 创建 XML 文件以保存应用程序数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10321136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:05:48  来源:igfitidea点击:

Create an XML file to save app data

iosxcode

提问by Alessandro

I am creating an app where there is a page which enables users to create a small project, I mean painting with the brush, adding labels, text fields, and adding UIImageViews and placing an image in them using the iOS library. then a screenshot is taken (for now) and it is uploaded on dropbox. from a tableview the users will be able to see all the uploaded documents. but the point is that it is only a screenshot. I wanted to upload things in a way that the textfields could be scrolled, and when I add the video feature, see the video. then add comments. I thought of uploading all the images, photos, textviews separately, and then save all the position of the pieces in an XML, so that the projects can be viewed from the table view: when a row is selected the app opens the XML and in base of that composes all the pieces like a puzzle. I decided to use Google library, (data), but I can't seem to find where to download the sample project with the library. so I put it at a side. I then tried to use NSXML parser, but I only see tutorials that enable asccess to an xml file, not actually create one according to each project!! Help!! How can I proceed? Any suggestions or tutorials? May be were to give me the link to data project, and please not to the google developer page or trunk, because it is a mess!!

我正在创建一个应用程序,其中有一个页面可以让用户创建一个小项目,我的意思是用画笔绘画、添加标签、文本字段、添加 UIImageViews 并使用 iOS 库在其中放置图像。然后(暂时)截取屏幕截图并将其上传到保管箱。从 tableview 用户将能够看到所有上传的文档。但关键是它只是一个截图。我想以一种可以滚动文本字段的方式上传内容,当我添加视频功能时,请查看视频。然后添加评论。I thought of uploading all the images, photos, textviews separately, and then save all the position of the pieces in an XML, so that the projects can be viewed from the table view: when a row is selected the app opens the XML and in它的基础像拼图一样组成了所有的碎片。我决定使用谷歌库,(数据),但我似乎无法找到在哪里下载带有库的示例项目。所以我把它放在一边。然后我尝试使用 NSXML 解析器,但我只看到启用访问 xml 文件的教程,实际上并没有根据每个项目创建一个!!帮助!!我该如何继续?有什么建议或教程吗?可能是给我数据项目的链接,请不要到谷歌开发者页面或主干,因为它是一团糟!!

thanks for the help in advance

我在这里先向您的帮助表示感谢

回答by Matt Hudson

There is a very good tutorial by Ray Wenderlich's web site (a great iOS dev and tutorial resource) on how to create XML with GDataXML here:

Ray Wenderlich 的网站(一个很棒的 iOS 开发和教程资源)提供了一个关于如何使用 GDataXML 创建 XML 的非常好的教程:

http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml

http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml

Here is the GitHub repo: https://github.com/neonichu/GDataXML

这是 GitHub 存储库:https: //github.com/neonichu/GDataXML

回答by Lloyd Sargent

OR you could use the very simple, very easy to use standardNSPropertyListSerialization:dataWithPropertyList:format:options:error and this is how you would do it:

或者你可以使用非常简单、非常容易使用的标准NSPropertyListSerialization:dataWithPropertyList:format:options:error ,这就是你的做法:

Populate it with NSArray's and NSDictionary's (this is the part that requires the most code - but it isn't difficult to do). The objects in your dictionary are the children and sub children (which may be NSArray's of NSDictionaries). Then use the following to write it out:

用 NSArray 和 NSDictionary 填充它(这是需要最多代码的部分 - 但它并不难做到)。字典中的对象是子对象和子子对象(可能是 NSDictionaries 的 NSArray)。然后用下面的写出来:

NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList: resultLists
                                                             format: NSPropertyListXMLFormat_v1_0
                                                            options: 0
                                                              error: &error];

//----- DO ERROR CHECKING (left out to simplify)

BOOL result = [xmlData writeToFile:arrayFileName atomically:YES];

Boom. An XML file.

繁荣。一个 XML 文件。

I'm backing up a core data file and it is relatively trivial to do.

我正在备份一个核心数据文件,而且做起来相对简单。

I normally like Ray Wenderlich's web site, but in this case he makes something that is very easy to do very complex. I've done it that way and honestly, the Apple way is much, much easier.

我通常喜欢 Ray Wenderlich 的网站,但在这种情况下,他制作了一些很容易做到很复杂的东西。我已经这样做了,老实说,Apple 的方式要容易得多。