-2

I am new in iOS and I am facing problem regarding to show data from web service to calendar.

My code is like this

-(void)funAddEvents:(NSArray *)arrDate
{   
    _eventsByDate = [NSMutableDictionary new];


    for(int i=0;i<arrDate.count;i++)
    {

        NSString *datefromweb= [NSString stringWithFormat: @"%@",[[arrDate objectAtIndex:i] objectForKey:@"Date"]]; //Showing error on this like..
        NSDateFormatter* df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
        NSDate *randomDate = [[NSDate alloc] init];
        randomDate = [df dateFromString:datefromweb];

        NSString *key = [[self dateFormatter] stringFromDate:randomDate];
        //  NSLog(@"event date =%@",randomDate);

        if(!_eventsByDate[key])
        {
            _eventsByDate[key] = [NSMutableArray new];
        }

        [_eventsByDate[key] addObject:randomDate];
        // NSLog(@"afte event date %@",eventsByDate);
    }

}

Thanks in Advance!

Muju
  • 884
  • 20
  • 54
  • `[arrDate objectAtIndex:i]` is a `NSString` object, not a `NSDictionary` object. Guessing? `NSString *datefromweb = [arrDate objectAtIndex:i];`. – Larme Mar 31 '17 at 09:33
  • You are having array of string not array of dictionary – Nirav D Mar 31 '17 at 09:33
  • can you print `arrDate` – Anbu.Karthik Mar 31 '17 at 09:34
  • @Larme Yes it is string object. So I can I show it. – Muju Mar 31 '17 at 09:34
  • @Larme I write code like this NSString *datefromweb = [arrDate objectAtIndex:i]; and it shows error setObjectForKey: key cannot be nil' – Muju Mar 31 '17 at 09:37
  • @Muju Can you show us console log for `arrDate`. – Nirav D Mar 31 '17 at 09:38
  • @NiravD It is String in array. – Muju Mar 31 '17 at 09:38
  • What error? Please, be clear. Give us the value of `arrDate` Or at least `[arrDate objectAtIndex:i]` It's unclear, what is your issue. My guess with your new error? The date format doesn't match? Is it because randomDate is nil? Which line? – Larme Mar 31 '17 at 09:39
  • 1
    @NiravD ArrDate data =( "2017/03/27", "2017/03/27" ) – Muju Mar 31 '17 at 09:39
  • 1
    Now your issue is about the `dateFormat` of your `NSDateFormatter`. In which world `MM/dd/yyyy hh:mm:ss a` matches `2017/03/27`? When you do `[df dateFromString:datefromweb]` that's where lies your issue. Try with `yyyy/MM/dd` instead. – Larme Mar 31 '17 at 09:42

1 Answers1

1

arrDate is Array of String not Array of Dictionary also you are getting nil because of wrong dateFormat. Correct dateFormat for your string is yyyy/MM/dd and currently you are setting dateFormat to MM/dd/yyyy hh:mm:ss a which is wrong, so changing dateFormat will solved your problem.

for(int i=0;i<arrDate.count;i++)
{

    NSString *datefromweb= [NSString stringWithFormat: @"%@",[[arrDate objectAtIndex:i] objectForKey:@"Date"]]; //Showing error on this like..
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy/MM/dd"];
    NSDate *randomDate = [[NSDate alloc] init];
    randomDate = [df dateFromString:datefromweb];

    NSString *key = [[self dateFormatter] stringFromDate:randomDate];
    //  NSLog(@"event date =%@",randomDate);

    if(!_eventsByDate[key])
    {
        _eventsByDate[key] = [NSMutableArray new];
    }

    [_eventsByDate[key] addObject:randomDate];
    // NSLog(@"afte event date %@",eventsByDate);
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183