Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5837889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:24:34+00:00 2026-05-22T11:24:34+00:00

I have four array and they are full of NSNull elements: years (100 elements),

  • 0

I have four array and they are full of NSNull elements: years (100 elements), months (12), days(31) and arrayString. When i choose two date I want put a string inside every array “arrayString” contained inside of every day of the period.

I choose two date and I do dateFormatter and I got first and last day, month an year:
I don’t write date1 and date2 but they are two NSDate

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
int firstDay = [[dateFormatter stringFromDate:data1] intValue];
int lastDay = [[dateFormatter stringFromDate:data2] intValue];
[dateFormatter setDateFormat:@"MM"];
int firstMonth = [[dateFormatter stringFromDate:data1] intValue];
int lastMonth = [[dateFormatter stringFromDate:data2] intValue];
[dateFormatter setDateFormat:@"yyyy"];
int firstYear = [[dateFormatter stringFromDate:data1] intValue]-2011;
int lastYear = [[dateFormatter stringFromDate:data2] intValue]-2011;
NSString *string = @"firstString";

after I want add a NSString inside every array contained in every day of the period, it is possible because every position in the arrays there are NSNull elements that I put inside in viewdidload:

for (int k = firstYear ; k<lastYear + 1; k++){ 

    for (int i = firstMonth; i < lastMonth+1; i++) 
    {
        for (int j = firstDay; j < lastDay+1; j++)  
        {
            [[days objectAtIndex:j] addObject: string];
        }

        [months replaceObjectAtIndex:i withObject:days];
    }

[years replaceObjectAtIndex:k withObject:months];
}   

This code work fine when I choose a period in the same month, because if I choose for example: 15/05/2011 to 25/05/2011 it’s ok, it fill the arrays string inside every day of the period.
But if I choose for example 28/05/2011 to 1/06/2011 there is a problem in the third loop; because “firstday” is 28 and “lastday” is 1, and it don’t entry inside at the loop; how can I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:24:34+00:00Added an answer on May 22, 2026 at 11:24 am

    Given your comments on the question, I assume that you are looking for a way of associating strings (or any object, really) with given days. This association lends itself to an NSDictionary and dates are obviously best represented with NSDate instances. With this in mind, I propose a different data structure.

    Rather than deeply nested arrays, you should consider the following, wherein NSDate objects are the dictionary’s keys and their respective values are NSArray instances containing the string for that specific point in time, not just the day:

    NSDictionary {
        NSDate: NSString,
        NSDate: NSString,
        NSDate: ...,
    }
    

    Now that you have your data stored in a suitable structure, you want to be able to query it for objects associated with a given day. For this, you could have the following method(s):

    - (BOOL)date:(NSDate *)date isSameDayAsReferenceDate:(NSDate *)reference {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSCalendarUnit units = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
        NSDateComponents *compDate = [calendar components:units fromDate:date];
        NSDateComponents *compReference = [calendar components:units fromDate:reference];
    
        return [compDate isEqual:compReference];
    }
    
    - (NSArray *)stringsForDay:(NSDate *)day withDictionary:(NSDictionary *)dictionary {
        NSSet *dates = [dictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
            return [self date:key isSameDayAsReferenceDate:day];
        }];
    
        return [dictionary objectsForKeys:[dates allObjects] notFoundMarker:[NSNull null]];
    }
    

    Now, if you wanted an array of all strings for today, you could simply call [self stringsForDay:[NSDate date] withDictionary:theDictionary] and be done with it.

    This approach is more flexible than your alternative suggested in the question, as changing the granularity to weeks, months, years, etc. is now very simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will have four word list arrays. $array1 = array('red','blue','green');//it can have more elements
I have two arrays they look like $a1 = array( array('num' => 1, 'name'
I have an array $test = array('one', 'two', 'three', 'four', 'five'); I unset two
I have four DIV elements on my page. Each has it's own array which
How can I store arrays in single array? e.g. I have four different arrays,
Hey Guys I have four integer array lists which were created after parsing a
I have put few elements in array (e.g. 5 elements) The first element, index[0]
i have four dates: First two are the beginning and end of a month
I have a four dimensional array for which I have a static set of
I have four <select> element. <div id=select-countries-container> <select name=countryId id=select-countries> <option value=>Choose Country &raquo;</option>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.