2011-01-31 37 views
0

我有一個NSMutableDictionary,它包含用戶的詳細信息。我已經使用解析並從MutableDictionary中獲取數據。如何在iPhone中創建NSMutableArray中的NSDictionary?

feedDictionary是, {

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

}

我已經加入所有的值到FeedArray,我想以創建FeedArray.Because我已經在顯示的數據兩個字典部分表格視圖。所以表格部分的數據是 - 第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" 
} 

) 那麼,如何可以創建字典用於可變數組。所以請指導我。

謝謝!

回答

2
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數據源的方法:


編輯:我想我誤解你的編輯問題。你的代碼看起來很好看。

+0

感謝您的回覆。但請看我最新的問題,並幫助我。謝謝。 – Pugal 2011-01-31 10:45:15

相關問題