我正在获得如下的网络服务响应:
(
{
ChatDetailsID = 1;
ChatID = 4;
ChatLine = hI;
Timestamp = "15-Jul-16 04:30";
UserID = 2;
},
{
ChatDetailsID = 2;
ChatID = 4;
ChatLine = "hI!";
Timestamp = "15-Jul-16 04:30";
UserID = 6;
},
{
ChatDetailsID = 9;
ChatID = 4;
ChatLine = "sample string 3";
Timestamp = "18-Jul-16 05:57";
UserID = 2;
},
{
ChatDetailsID = 10;
ChatID = 4;
ChatLine = "sample string 4";
Timestamp = "18-Jul-16 06:03";
UserID = 2;
},
{
ChatDetailsID = 15;
ChatID = 4;
ChatLine = Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg;
Timestamp = "19-Jul-16 01:11";
UserID = 2;
}
)
我希望在表视图中显示此数据,但是时间戳作为表视图部分标题和数据,根据时间戳记在下面。
我正在尝试使用一个键和多个对象准备字典,如下所示:
NSMutableDictionary *temDic = [[NSMutableDictionary alloc] init];
for (int j = 0; j<[arrmDate count]; j++)
{
for (int i =0; i< [arrmMsg count]; i++)
{
ChatContainer *object1 = [arrmMsg objectAtIndex:i];
NSString * strobjdate = object1.strDate;
NSString * strdate = [arrmDate objectAtIndex:j];
if ([strdate isEqualToString:strobjdate])
{
[temDic setValue:object1.strChatLine forKey:strdate];
}
}
}
在上面的代码中,arrmDate是包含不同日期的日期数组,arrmMsg包含容器类的输出,其中我存储了我在Web服务响应中获得的所有值。
但我只得到该特定日期的最后一个值如下:
{
"15-Jul-16" = "hI!";
"18-Jul-16" = "sample string 4";
"19-Jul-16" = Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg;
}
我想要如下:
{
"15-Jul-16" = "hI","hI!";
"18-Jul-16" = "sample string 3","sample string 4";
"19-Jul-16" = Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg;
}
请让我知道我做错了什么。或建议任何其他解决方案。 谢谢。
答案 0 :(得分:0)
在Dictionary中,只能为特定键存储一个值。 TimeStamp 15-Jul-16 04:30发生两次,因此这个timeStamp(用作键)的第二次值被新值替换。
答案 1 :(得分:0)
正如您所看到的,您的第一个字典有let point: CGPoint = CGPoint(x:10,y:10)
let dict:NSMutableDictionary = ["point":NSValue(CGPoint: point)]
let getVal:CGPoint = (dict["point"] as? NSValue)!.CGPointValue()
print(getVal)
作为时间戳,第二个字典也有"15-Jul-16 04:30"
作为时间戳。
因此,第一次编译器遇到"15-Jul-16 04:30"
语句时,它存储了[temDic setValue:object1.strChatLine forKey:strdate]
,因为您的词典中没有包含"hI"
的键
现在,第二次遇到"15-Jul-16 04:30"
语句时,已经有一个[temDic setValue:object1.strChatLine forKey:strdate]
的密钥,所以它只是用"15-Jul-16 04:30"
替换了该密钥
现在,如果您只是将日期部分存储在"hI!"
数组中,它显然无法区分第3和第4个字典,因为arrmdave
是{{1}所以它继续替换相同的密钥。
如果要根据时间戳对词典进行排序,那么首先在striate
数组中,将timeStamps与日期保持一致,然后使用数组来保存已排序的词典。
"18-Jul-16"
请确保您的arrmdave
保留整个日期字符串,例如NSMutableDictionary *temDic;
NSMutableArray *sortedList = [[NSMutableArray alloc]init];
for (int j = 0; j<[arrmDate count]; j++)
{
temDic = [[NSMutableDictionary alloc] init];
NSString *strdate = [arrmDate objectAtIndex:j];
for (int i =0; i< [arrmMsg count]; i++)
{
ChatContainer *object1 = [arrmMsg objectAtIndex:i];
NSString *strobjdate = object1.strDate;
if ([strdate isEqualToString:strobjdate])
{
[temDic setValue:object1.strChatLine forKey:strdate];
[sortedList addObject: temDic];
break;
}
}
}
而不是arrmDate
。
答案 2 :(得分:0)
首先,我很抱歉推迟了答案。昨天晚上8点30分,当我发布回答时是 - Stack Overflow目前处于离线状态 进行维护。这就是原因只是我昨天无法发布答案。
Bhagyashree mahajan谢谢你。我试着让解决方案持续了4个小时。最后我根据你的输出得到了。我创建了你的阵列字典。我的回答让你满意。
#import "ViewController.h"
@interface ViewController ()
{
NSMutableDictionary *dict1,*dict2,*dict3,*dict4,*dict5,*dictResult;
NSMutableArray *arrJsonResponse,*arrTimeStamp,*arrChatLine;
NSMutableArray *arrAppendObject,*arrAppendKey;
NSString *strAppendValue;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dict1 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"1",@"ChatDetailsID",@"4",@"ChatID",@"hi",@"ChatLine",@"15-Jul-16 04:30",@"Timestamp",@"2",@"UserID", nil];
dict2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"2",@"ChatDetailsID",@"4",@"ChatID",@"hi",@"ChatLine",@"15-Jul-16 04:30",@"Timestamp",@"6",@"UserID", nil];
dict3 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"9",@"ChatDetailsID",@"4",@"ChatID",@"sample string 3",@"ChatLine",@"18-Jul-16 05:57",@"Timestamp",@"2",@"UserID", nil];
dict4 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"10",@"ChatDetailsID",@"4",@"ChatID",@"sample string 4",@"ChatLine",@"18-Jul-16 06:03",@"Timestamp",@"2",@"UserID", nil];
dict5 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"15",@"ChatDetailsID",@"4",@"ChatID",@"Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg",@"ChatLine",@"19-Jul-16 01:11",@"Timestamp",@"2",@"UserID", nil];
arrJsonResponse = [[NSMutableArray alloc]initWithObjects:dict1,dict2,dict3,dict4,dict5,nil];
arrTimeStamp = [[NSMutableArray alloc]init];
arrChatLine = [[NSMutableArray alloc]init];
arrAppendObject = [[NSMutableArray alloc]init];
arrAppendKey = [[NSMutableArray alloc]init];
dictResult = [[NSMutableDictionary alloc]init];
for(int i=0;i<arrJsonResponse.count;i++)
{
NSString *strTimeStamp = [NSString stringWithFormat:@"%@",[[arrJsonResponse objectAtIndex:i] objectForKey:@"Timestamp"]];
NSString *strChatLine = [NSString stringWithFormat:@"%@",[[arrJsonResponse objectAtIndex:i] objectForKey:@"ChatLine"]];
NSRange range = [strTimeStamp rangeOfString:@" "];
strTimeStamp = [[strTimeStamp substringToIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"The date is - %@",strTimeStamp);
[arrTimeStamp addObject:strTimeStamp];
[arrChatLine addObject:strChatLine];
}
for(int j=0;j<arrTimeStamp.count;j++)
{
NSString *strDate = [arrTimeStamp objectAtIndex:j];
NSString *strChatLine = [arrChatLine objectAtIndex:j];
int index = (int)[arrTimeStamp indexOfObject:strDate];
if(j != index)
{
[arrAppendKey removeLastObject];
[arrAppendObject removeLastObject];
strAppendValue = [NSString stringWithFormat:@"%@ %@",[arrChatLine objectAtIndex:index],[arrChatLine objectAtIndex:j]];
[arrAppendObject addObject:strAppendValue];
[arrAppendKey addObject:strDate];
}
else
{
[arrAppendObject addObject:strChatLine];
[arrAppendKey addObject:strDate];
}
}
NSLog(@"The objects are - %@",arrAppendObject);
NSLog(@"The keys are - %@",arrAppendKey);
for (int k=0;k<arrAppendKey.count;k++)
{
NSString *strArrKey = [NSString stringWithFormat:@"%@",[arrAppendKey objectAtIndex:k]];
NSString *strArrObject = [NSString stringWithFormat:@"%@",[arrAppendObject objectAtIndex:k]];
[dictResult setObject:strArrObject forKey:strArrKey];
}
NSLog(@"The printed dictionary results are - %@",dictResult);
}
印刷结果
The objects are -
(
"hi hi",
"sample string 3 sample string 4",
Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg
)
The keys are -
(
"15-Jul-16",
"18-Jul-16",
"19-Jul-16"
)
The printed dictionary results are -
{
"15-Jul-16" = "hi hi";
"18-Jul-16" = "sample string 3 sample string 4";
"19-Jul-16" = Dsjfrejfijrihfuirhgvurhuifhueirghiurhgvuirhiuvhriuhfiuerhihfrfreg;
}
以上代码完美无缺。