Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中的数字格式问题_Ios_Number Formatting - Fatal编程技术网

iOS中的数字格式问题

iOS中的数字格式问题,ios,number-formatting,Ios,Number Formatting,在我的iOS应用程序中,我尝试在第一次执行应用程序时填充核心数据存储。我在AppDelegate.m文件中使用以下代码,我将以创建实体的一个对象为例: //creating new managedObject NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:[self ma

在我的iOS应用程序中,我尝试在第一次执行应用程序时填充核心数据存储。我在AppDelegate.m文件中使用以下代码,我将以创建实体的一个对象为例:

     //creating new managedObject

     NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:[self managedObjectContext]];


    //extracting year,month and day from current date

    todayDate = [NSDate date];        
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components

    //converting components      
    NSNumber *yearBuscado = [NSNumber numberWithLong:[components year]];
    NSNumber *mesBuscado = [NSNumber numberWithLong:[components month]];
    NSNumber *diaBuscado = [NSNumber numberWithLong:[components day]];

    //populating managedObject

    [favoriteThing setValue:@"If done, swipe to right " forKey:@"thingName"];//String
    [favoriteThing setValue:[NSNumber numberWithInt:10] forKey:@"displayOrder"];//Integer32   
    [favoriteThing setValue:[NSNumber numberWithInt:yearBuscado] forKey:@"todoYear"];//Integer32
    [favoriteThing setValue:[NSNumber numberWithInt:mesBuscado] forKey:@"todoMonth"];//Integer32
    [favoriteThing setValue:[NSNumber numberWithInt:diaBuscado] forKey:@"todoDay"];//Integer32
    [favoriteThing setValue:@"Urgent" forKey:@"urgent"];//String
    [favoriteThing setValue:@"Yellow" forKey:@"color"];//String
    [favoriteThing setValue:@"Not deleted" forKey:@"isSemiDeleted"];//String
    [favoriteThing setValue:@"Not done" forKey:@"isDone"];//String
这是首次启动应用程序后存储的内容(摘自SQLite manager):

我需要的是存储今天(2014-01-04)的预期值:

这三个属性来自类型
Integer 32
我做错了什么?
感谢您的时间。

在现代目标C中,使用NSNumber,事情变得很简单。您可以将您的数字定义为:

NSNumber *todoYear = @2014;
NSNumber *todoMonth = @1;
NSNumber *todoDay = @4;
编译器会处理一切。快乐编码:)

查看这些链接了解更多信息。

编辑
使用以下命令:

NSNumber *yearBuscado = @([components year]);

您错误地使用NSNumber对象调用了
numberwhithint
,而直接使用该NSNumber调用了setValue

todayDate = [NSDate date];        
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components

//converting components      
NSNumber *yearBuscado = [NSNumber numberWithInteger:[components year]];
NSNumber *mesBuscado = [NSNumber numberWithInteger:[components month]];
NSNumber *diaBuscado = [NSNumber numberWithInteger:[components day]];

[favoriteThing setValue:yearBuscado forKey:@"todoYear"];//Integer32
[favoriteThing setValue:mesBuscado forKey:@"todoMonth"];//Integer32
[favoriteThing setValue:diaBuscado forKey:@"todoDay"];//Integer32
此外,还可以从CoreData模型生成模型类。因此,您可以通过
属性
将值设置为
NSManagedObject
,而不是进行键值编码


谢谢,但我需要从当前日期获取值,因此我使用的是NSDATE组件…NSNumber*todoYear=@([components year])如何?是的,这肯定是一个有效的答案。这将在将来有所帮助:)@mvasco你在找什么?问题已纠正?尚未检查,但我想您的建议是正确的,谢谢。我会让你知道,因为我检查它…谢谢你,你的第一个建议是工作的预期。非常感谢你。祝你白天/晚上愉快。。。。
todayDate = [NSDate date];        
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components

//converting components      
NSNumber *yearBuscado = [NSNumber numberWithInteger:[components year]];
NSNumber *mesBuscado = [NSNumber numberWithInteger:[components month]];
NSNumber *diaBuscado = [NSNumber numberWithInteger:[components day]];

[favoriteThing setValue:yearBuscado forKey:@"todoYear"];//Integer32
[favoriteThing setValue:mesBuscado forKey:@"todoMonth"];//Integer32
[favoriteThing setValue:diaBuscado forKey:@"todoDay"];//Integer32
FavoriteThing *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:[self managedObjectContext]];
[favoriteThing setBuscado: yearBuscado];