iOS中的数字格式问题

时间:2014-01-05 04:22:48

标签: ios number-formatting

在我的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管理器中提取): enter image description here

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

todoYear = 2014
todoMonth = 1
todoDay = 4

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

2 个答案:

答案 0 :(得分:1)

现代目标C中的NSNumber已经变得容易了。您可以将您的数字定义为:

NSNumber *todoYear = @2014;
NSNumber *todoMonth = @1;
NSNumber *todoDay = @4;

编译器会处理所有事情。快乐编码:)

请查看这些链接以获取更多信息 NSNumber Literals
modern Objective C Basics

修改
使用此:

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

答案 1 :(得分:1)

您错误地使用NSNumber对象调用numberWithInt,而是直接使用该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 Model生成模型类。因此,您可以将值设置为NSManagedObjectProperties,而不是进行键值编码。

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