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

发布于 2024-10-15 05:32:39 字数 1816 浏览 4 评论 0原文

我有一个 NSMutableDictionary,它包含用户详细信息。我已经使用解析并从 MutableDictionary 获取数据。

feedDictionary 是, {

"city = New York",
"phone = 111111",
"email = [email protected]",
"year = 1986",
"degree = Undergraduate"

}

我已将所有值添加到 FeedArray 中,并且我想为 FeedArray 创建两个字典。因为我已在节表视图中显示数据。因此表部分数据为 - 第 1 部分 - [城市,电话,电子邮件] 和第 2 部分 - [年份,学位]。因为如果数据中的任何一个为零,那么我不应该删除该部分中的特定行。所以我想检查数据,数据是否为零。

编辑:

在添加到 FeedArray 之前,我想检查字符串是否为零。如果字符串为零,那么我不应该添加数组,如果数据不为零,则只添加到 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;

预期输出

是,

我的数组是 5 (

section1{
"city = New York",
"phone = 111111",
"email = [email protected]",
}
 section2
{

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

) 那么我如何为可变数组创建字典。所以请指导我。

谢谢!

I have one NSMutableDictionary and it contains the user details. I have used parsing and get the data from the MutableDictionary.

feedDictionary is,
{

"city = New York",
"phone = 111111",
"email = [email protected]",
"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. so the table section data is - Section-1 -[city,phone,email] and Section-2-[year,degree]. 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. 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.

[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 (

section1{
"city = New York",
"phone = 111111",
"email = [email protected]",
}
 section2
{

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

)
So how can i create the dictionary for the Mutable Array. So Please guide me.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-10-22 05:32:39
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];

你的 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;
}

编辑:我想我误解了你编辑的问题。你的代码乍一看看起来不错。

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:

- (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文