简体   繁体   English

如何在iPhone中的NSMutableArray中创建NSDictionary?

[英]How to create a NSDictionary in NSMutableArray in iPhone?

I have one NSMutableDictionary and it contains the user details. 我有一个NSMutableDictionary,它包含用户详细信息。 I have used parsing and get the data from the MutableDictionary. 我使用了解析并从MutableDictionary获取数据。

feedDictionary is, { feedDictionary是,{

"city = New York",
"phone = 111111",
"email = aaa@test.com",
"year = 1986",
"degree = Undergraduate"

} }

I have added all the value into the FeedArray and i want to create the two dictionary for the FeedArray.Because i have displayed the data in the section table view. 我已将所有值添加到FeedArray中,并且我想为FeedArray创建两个字典。因为我已在节表视图中显示数据。 so the table section data is - Section-1 -[city,phone,email] and Section-2-[year,degree]. 所以表格部分数据是 - 第1节 - [城市,电话,电子邮件]和第2节[年份,学位]。 Because if the anyone of the data is nil, so i shouldn't remove that particular rows in that section. 因为如果任何数据都为零,那么我不应该删除该部分中的特定行。 so that i want to check the data, whether the data is nil or not. 这样我想检查数据,无论数据是否为零。

Edit: 编辑:

Before i added to the FeedArray, i want to check the string is nil or not. 在添加到FeedArray之前,我想检查字符串是否为nil。 If the string is nil, then i shouldn't add the Array and if the data is not nil, then only added to the FeedArray. 如果字符串为nil,那么我不应该添加数组,如果数据不是nil,则只添加到FeedArray。

[self isEmpty:[feedDictionary valueForKey:@"city"]]?:[feedArray addObject:[feedDictionary valueForKey:@"city"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"phone"]]?:[feedArray addObject:[feedDictionary valueForKey:@"phone"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"email"]]?:[feedArray addObject:[feedDictionary valueForKey:@"email"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"year"]]?:[feedArray addObject:[feedDictionary valueForKey:@"year"]]; //section2

[self isEmpty:[feedDictionary valueForKey:@"degree"]]?:[feedArray addObject:[feedDictionary valueForKey:@"degree"]]; //section2

 -(BOOL) isEmpty :(NSString*)str{
      if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
    return YES;
return NO;

} }

Expected output is, 预期的输出是

My array is 5 ( 我的数组是5(

section1{
"city = New York",
"phone = 111111",
"email = aaa@test.com",
}
 section2
{

"year = 1986",
"degree = Undergraduate"e"
}

) So how can i create the dictionary for the Mutable Array. 那么如何为Mutable Array创建字典呢? So Please guide me. 所以,请指导我。

Thanks! 谢谢!

NSArray *section0Keys = [NSArray arrayWithObjects:@"city", @"phone", @"email", nil];
NSMutableArray *section0 = [[[feedDictionary objectsForKeys:section0Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section0 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
// you don't need this if you don't care for invalid arrays.
[section0 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

NSArray *section1Keys = [NSArray arrayWithObjects:@"year", @"degree", nil];
NSMutableArray *section1 = [[[feedDictionary objectsForKeys:section1Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section1 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
[section1 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

self.dataArray = [NSArray arrayWithObjects:
section0,
section1,
nil];

your UITableView Datasource methods: 您的UITableView数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.dataArray objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /.../
    cell.textLabel.text = [[self.dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

Edit: I guess i misunderstood your edited question. 编辑:我想我误解了你编辑过的问题。 your code looks fine at first sight. 乍一看,您的代码看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM