Question

I have the following code block:

NSNumber* dayNsNumber = [[NSNumber alloc] initWithInt:dayNumber+1];
NSLog(@"dayNsNumber: %d", [dayNsNumber intValue]);

if(boolVar){

    UIAlertView* success = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"Day %d!", dayNumber+1] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Star", nil];
    [success show];
    NSLog(@"dayNsNumber: %d", [dayNsNumber intValue]);
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
            dayNsNumber, "Day Number",
            [self getCurrentLocalDate], @"Date",
            [self getCurrentLocalTime], @"Time",
         nil];
}

What happens is that when I run the code, it hangs upon initialization of the params NSDictionary and says that dayNsNumber is nil.

Xcode displays a Thread 1: EXC_BAD_ACCESS (code=1).

How do I solve thie issue? I would like to add the dayNsNumber to my params dictionary.

Additionally, here are getCurrentLocalDate and getCurrentLocalTime:

-(NSString*)getCurrentLocalDate;{ 
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"dd-MM-yyyy" options:0 locale:[NSLocale currentLocale]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatString];

    NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"todayString: \"%@\"", todayString);
    return todayString;

}

-(NSString*)getCurrentLocalTime;{
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm:ss zzz" options:0 locale:[NSLocale currentLocale]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatString];

    NSString *timeString = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"timeString: \"%@\"", timeString);
    return timeString;
}
Was it helpful?

Solution

The problem is with the key: "Day Number" should be @"Day Number".

Also note that you can now use the new syntax for your numbers, arrays, and dictionaries. For example, this

NSNumber* dayNsNumber = [[NSNumber alloc] initWithInt:dayNumber+1];

can be rewritten like this:

NSNumber* dayNsNumber = @(dayNumber+1);

and the dictionary initialization can be done like this:

NSDictionary *params = @{
    @"Day Number" :   dayNsNumber
,   @"Date"       : [self getCurrentLocalDate]
,   @"Time"       : [self getCurrentLocalTime]
};

OTHER TIPS

try this

NSDictionary *params = [[NSDictionary alloc]init];
[params setValue:[daysNumber stringValue] forKey:@"Day Number"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top