如何使用parse.com服务器iOS保存对象数组? [英] How to save array of objects using parse.com server iOS?

查看:72
本文介绍了如何使用parse.com服务器iOS保存对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用parse.com服务器发送和检索我的iOS应用程序数据.我想保存歌曲列表,每首歌曲具有以下属性(标题,艺术家,专辑).我的代码段在这里;

Iam using parse.com server to send and retrieve my iOS app data. I want to save list of songs and each song have the following properties(title, artist, album). My code snippet is here;

        -(IBAction)saveSongsData:(id)sender {

        PFObject *newPlayer = [PFObject objectWithClassName:@"Players"];

        /*[newPlayer addObjectsFromArray:self.songsArrray forKey:@"songs"];
        here i got the exception, if i uncomment it*/

        [newPlayer setObject:self.txtPlayerName.text forKey:@"playerName"];
        [newPlayer setObject:self.txtPlayerDesc.text forKey:@"playerDescription"];
        [newPlayer setObject:self.txtPlayerPass.text forKey:@"playerPassword"];

        NSString *objectId = [newPlayer objectId];
        [[NSUserDefaults standardUserDefaults]setObject:objectId forKey:@"id"];

        [newPlayer saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
            }
            else {
            }
        }];
        }

其中self.songsArray是具有以下属性的歌曲对象数组;

Where self.songsArray is an array of songs objects having following properties;

  • 标题, 艺术家, 相册.

  • title, artist, album.

但是当我尝试使用此行代码保存我的歌曲数据时.

But when i try to save my songs data by using this line of code.

    [newPlayer addObjectsFromArray:self.songsArrray forKey:@"songs"];

我有一个例外,这是消息:-

I got an exception and this is the message:-

由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"PFObject值必须可序列化为JSON"

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values must be serializable to JSON'

请帮助我,如何将我的歌曲数据以JSON格式发送到服务器. 谢谢.

Please help me, how to send my songs data to server in JSON format. Thanks.

推荐答案

使用Parse时,您的数组和字典只能包含可以序列化为JSON的对象.看起来self.songsArray包含无法自动转换为JSON的对象.您有两个选择-使用与Parse兼容的对象,或者按照注释中的建议,将您的歌曲制作为PFObjects,然后通过某种关系将它们与播放器关联.

When working with Parse, your arrays and dictionaries can only contain objects that can be serialized to JSON. It looks as if self.songsArray contains objects that cannot be automatically converted to JSON. You have two options - use objects that are compatible with Parse or, as someone has suggested in the comments, make your songs PFObjects and then associate them with the player via a relationship.

这篇关于如何使用parse.com服务器iOS保存对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆