1

我有以下代码块:

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];
}

发生的情况是,当我运行代码时,它在初始化时挂起params NSDictionary并说dayNsNumbernil.

Xcode 显示线程 1:EXC_BAD_ACCESS (code=1)。

我该如何解决这个问题?我想添加dayNsNumber到我的params字典中。

此外,还有getCurrentLocalDategetCurrentLocalTime

-(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;
}
4

2 回答 2

5

问题在于关键:"Day Number"应该是@"Day Number".

另请注意,您现在可以对数字、数组和字典使用新语法。例如,这个

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

可以这样重写:

NSNumber* dayNsNumber = @(dayNumber+1);

字典初始化可以这样完成:

NSDictionary *params = @{
    @"Day Number" :   dayNsNumber
,   @"Date"       : [self getCurrentLocalDate]
,   @"Time"       : [self getCurrentLocalTime]
};
于 2013-10-21T02:27:17.080 回答
1

尝试这个

NSDictionary *params = [[NSDictionary alloc]init];
[params setValue:[daysNumber stringValue] forKey:@"Day Number"];
于 2013-10-21T02:28:51.813 回答