iOS应用程序: incompatible pointer types

标签 ios pointers core-data

在我的 iOS 应用程序中,我正在尝试执行以下操作:在 ViewController 中,有几个 TextFields 和一个 Save 按钮。当用户点击保存按钮时,yearTextField 的文本必须存储在核心数据属性 (todoYear:int32) 中。 这是我的保存方法的代码:

- (IBAction)SaveButtonAction:(id)sender {


    AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;

    NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:context];
    NSString *todoText = ToDoTextField.text;
    NSNumber *todoYear = [yearTextField.text intValue];
    NSNumber *todoMonth = [monthTextField.text intValue];
    NSNumber *todoDay = [dayTextField.text intValue];
    NSNumber *todoHour = [hourTextField.text intValue];
    NSNumber *todoMinute = [minuteTextField.text intValue];        
    NSString *todoUrgent = urgentTextField.text;
    NSString *todoColor = colorTextField.text;


    [favoriteThing setValue:todoText forKey:@"thingName"];
    [favoriteThing setValue:[NSNumber numberWithInt:0] forKey:@"displayOrder"];
    [favoriteThing setValue:todoYear forKey:@"todoYear"];
    [favoriteThing setValue:todoMonth forKey:@"todoMonth"];
    [favoriteThing setValue:todoDay forKey:@"todoDay"];
    [favoriteThing setValue:todoHour forKey:@"todoHour"];
    [favoriteThing setValue:todoMinute forKey:@"todoMinute"];
    [favoriteThing setValue:todoUrgent forKey:@"urgent"];
    [favoriteThing setValue:todoColor forKey:@"color"];             

    NSError *error;
    if(! [context save:&error])
    {
        NSLog(@"Whoopw,couldn't save:%@", [error localizedDescription]);
    }
}

这是个异常(exception):

self    AddToDoViewController * 0x16578680  0x16578680
favoriteThing   FavoriteThing_FavoriteThing_ *  0x1650da90  0x1650da90
todoText    __NSCFString *  @"h"    0x1659f2b0
todoColor   __NSCFString *  @"Black"    0x16542070
todoUrgent  __NSCFString *  @"Not urgent"   0x165139e0
todoYear    NSNumber *  0x7dd   0x000007dd
todoMinute  NSNumber *  0x25    0x00000025
urgentTextField UITextField *   0x165bc2d0  0x165bc2d0
colorTextField  UITextField *   0x165b2de0  0x165b2de0
minuteTextField UITextField *   0x165b8380  0x165b8380

'todoText'、'todoColor'、'todourgent'的属性是正确的,读取'todoYear'属性的值时出现错误。 我不知道如何以正确的方式放置“todoYear”值才能被正确接受。提前谢谢你。

最佳答案

使用NSNumbernumberWithInt方法:

NSNumber *todoYear = [NSNumber numberWithInt: [yearTextField.text intValue]];
NSNumber *todoMonth = [NSNumber numberWithInt: [monthTextField.text intValue]];
NSNumber *todoDay = [NSNumber numberWithInt: [dayTextField.text intValue]];
NSNumber *todoHour = [NSNumber numberWithInt: [hourTextField.text intValue]];
NSNumber *todoMinute = [NSNumber numberWithInt: [minuteTextField.text intValue]];

或者将这一切组合成一个 NSDate...

NSString *dateString = [NSString stringWithFormat: @"%@-%@-%@ %@:%@:00 +0000",
    yearTextField.text, monthTextField.text, dayTextField.text,
    hourTextField.text, minuteTextField.text];
NSDate *todoDate = [NSDate dateWithString:dateString];

关于iOS应用程序: incompatible pointer types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20579456/

相关文章:

iphone - 获取的属性 v 关系(核心数据 - iPhone)

ios - 如何在 UITableView 静态单元格中动态更改单元格高度

ios - 在 subview Controller 之间切换

ios - 通用链接不会在 iOS 上打开应用程序

C 程序 - 在指针变量中存储值

iphone - UITableView - 延迟加载联系人图像

ios - Collection View 中的节数

c++ - c++中指向引用的指针

c - 为什么这不适用于 C 中的哈希表(指针)

iphone - 迁移核心数据——如何安全地迁移?