후 당신은 무엇 :
이 첫째 문제에 대해 생각하고, 다소 여기에 올바른 형식의 데이터를 점점 문제 분석 할 수 있기 때문에 먼저 사전으로 채워진 배열에서 정보가 들어있는 배열이있는 배열 배열로 바꿨습니다 (가장 우아한 해결책은 아니지만 그다지 효과적이지는 않습니다.)
// Here is our array of past dates
NSArray * pastDateDays = @[@"2015-01-27", @"2015-01-26", @"2015-01-25", @"2015-01-24", @"2015-01-23"];
// Here is our array from the request, this is full of dictionaries
NSArray * fetchRequest = @[@{@"daySectionIdentifier" : @"2015-01-24", @"sumValue": @2500}, @{@"daySectionIdentifier" : @"2015-01-25", @"sumValue": @1487}, @{@"daySectionIdentifier" : @"2015-01-27", @"sumValue": @750}];
// Here is a mutable array we will be adding to
NSMutableArray * request = [NSMutableArray arrayWithArray:fetchRequest];
이제 정보를 약간 더 멋진 형식으로 시작할 준비가되었습니다. 그들은 우리의 요구 배열에 일치하는 경우 우리의 날짜 배열의 날짜 변경 가능한 배열을
// This function gets the array in an array of arrays where each array has a date and a value
fetchRequest = [self fetchRequestToArray:fetchRequest];
// Not too complicated just taking it out of one and putting it in another
- (NSArray *)fetchRequestToArray: (NSArray *)array {
NSMutableArray * tempArray = [NSMutableArray new];
for (NSDictionary * dict in array) {
NSArray * temp = @[[dict objectForKey:@"daySectionIdentifier"], [dict objectForKey:@"sumValue"]];
[tempArray addObject:temp];
}
return [NSArray arrayWithArray:tempArray];
}
다음으로 루프 우리는 그들을 제거 :
NSMutableArray * tempDates = [NSMutableArray arrayWithArray:pastDateDays];
for (NSArray * array in fetchRequest) {
NSString * date = array.firstObject;
for (NSString * string in pastDateDays) {
if ([date isEqualToString:string]) {
[tempDates removeObject:string];
}
}
}
을이이 날짜의 배열을 우리에게 남긴다 날짜 배열에 포함되어 있지만 요청한 데이터에는 포함되어 있지 않습니다. 이것들은 우리가 0의 값을 더하는 데 필요한 날짜입니다.
는 다시이 비교적 간단하다 :
이
for (NSString * date in tempDates) {
NSDictionary * dict = [NSDictionary dictionaryWithObjects:@[date, @0]
forKeys:@[@"daySectionIdentifier", @"sumValue"]];
[request addObject:dict];
}
이 원하는 배열로 우리를 반환합니다.
추가해야하는 유일한 것은이 배열이 날짜 순서가 맞지 않는다는 것입니다. 이 방법은 여러 가지 방법으로 쉽게 정렬 할 수 있습니다. 나는 발견하고 몇 초에서 이것을에 추가하지만, 당신이 그것을 필요로하는 경우에 당신은 더 복잡 하나를 선택할 수 있습니다 :
NSSortDescriptor * sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"daySectionIdentifier"
ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortByDate];
NSArray * sortedArray = [request sortedArrayUsingDescriptors:sortDescriptors];
이 뜻을 출력 형식으로 날짜가 :
마지막 배열라는 배열입니다 요청이며 mutableArray입니다.
<__NSArrayI 0x7fade848f480>(
{
daySectionIdentifier = "2015-01-23";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-24";
sumValue = 2500;
},
{
daySectionIdentifier = "2015-01-25";
sumValue = 1487;
},
{
daySectionIdentifier = "2015-01-26";
sumValue = 0;
},
{
daySectionIdentifier = "2015-01-27";
sumValue = 750;
}
)
나는 원하는 출력이라고 생각합니다. 주의 할
것 : - 나는 배열을 많이 사용하고있다 - 값은 우리가있는 NSDictionary
이 가장 우아한 해결책이 아니라에서 정수를 저장할 수 NSNumbers이 아닌 정수 나는 그것이 리팩토링 될 수 있다고 확신한다. 이 코드는 작동하지만 이해를 구축하기 위해 함께 작업 할 수 있습니다.이 코드는 바로 복사 할 때 작동해야하지만 내 X 코드에서 복사 된 긴 대답이므로 조정이 필요한 몇 가지 사항이있을 수 있습니다.
문자열에 정확하게 작동하려면이 형식이어야합니다. 그렇지 않으면이 솔루션을 조정해야합니다. `:
나는이
당신은'2015년 1월 27일 (56 값)의 데이터 구조를 설명해야합니다 도움이되기를 바랍니다. 이 객체는 무엇입니까? 우리는 어떻게이 객체들 중 새로운 객체를 생성합니까? –
감사합니다. Ian. Cora 데이터 객체의 fetchRequest에서 가져온 것입니다. 2 개의 속성이 있습니다. 하나는 NSString YYYY-MM-DD이고 두 번째 필드는 Float입니다. 나는 이것을 다음과 같이 되 찾습니다. NSPropertyDescription * daySectionIdentifierProperty = entity.propertiesByName [@ "daySectionIdentifier"]; fetchRequest.propertiesToFetch = [NSArray arrayWithObjects : daySectionIdentifierProperty, sumExpressionDescription, nil]; fetchRequest.propertiesToGroupBy = @ [daySectionIdentifierProperty]; – d264789
질문을 읽을 수 없으므로이 코드로 질문을 업데이트 할 수 있습니까? – Whirlwind