ios - 在 Objective C 中处理 NSMutableArray 中的集合数据

标签 ios objective-c json nsmutablearray nsmutabledictionary

我从“WebServices”获取“JSON数据”并将它们设置在NSMutableArray中并尝试在页面中显示,我根据“JSON数据”使用“4个键”但是我不知道如何使用“键值”从 NSMutableArray 获取特定数据,我的“JSON`数据”是这样的 -

更多

3/5,641

JSON 数组格式 收件箱 X 阿努吉贾

晚上 9:13(14 小时前)

对我来说,Gaurav,RAjeev

<__NSArrayM 0x7f9e42556c10>(

{

    ansDesc = "Researching a lead that I previously submitted ";

    ansId = 1;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Submitting a new lead ";

    ansId = 2;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Working on a draft lead ";

    ansId = 3;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Daily ";

    ansId = 4;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Several times a week ";

    ansId = 5;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Once a week ";

    ansId = 6;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Every couple of weeks ";

    ansId = 7;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "About once a month ";

    ansId = 8;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

我必须在页面中一起显示“qsId 1”和“ansId 1, 2, 3”的一组问题和相应答案,然后类似地显示“qsId 2”和“ansId 4,5,6,7” ,8”在下一页。请帮我找出最可行的方法。

谢谢

最佳答案

在 Objective C 中,NSMutableArray 不支持键值概念。 NSDictionary 确实支持它。

我认为更好的方法是使用模型类来保存四个属性并使用 NSPredicate 将它们过滤掉。

@interface JSON_Info : NSObject

//id's
@property (nonatomic, assign) int qsId;
@property (nonatomic, assign) int ansId;

//descriptions
@property (nonatomic, strong) NSString *qsDesc;
@property (nonatomic, strong) NSString *ansDesc; 

@end

如果要过滤,请使用NSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"qsId == 1 && ansId == 2"];
NSArray *filteredCollection = [self.JSONInfoCollection filteredArrayUsingPredicate: predicate];

关于ios - 在 Objective C 中处理 NSMutableArray 中的集合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164383/

相关文章:

javascript - WkWebKit - 加载页面上的 javascript 发现 window.webkit 未定义

objective-c - 关于 View 层次结构,需要澄清

iphone - 属性列表 - 读取和写入 (iPhone)

javascript - 具有 API 后端数据预加载的单页应用程序

ios - UICollectionViewCell 的动态高度取决于内容

ios - 使用 Swift SpriteKit 时出现错误 "Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0x0)"

objective-c - RestKit:JSON 映射不起作用

ios - 在 ios 的 tableview 中的选定单元格上设置 imageview 的问题

java - JSON 有模式吗?

javascript - 如何在 Node.js 中的变量中存储 JSON 响应?