我无法将NSNumber数据类型的值插入到我的coredata中

时间:2016-11-26 09:52:17

标签: ios objective-c core-data iphonecoredatarecipes

  

由于未捕获的异常而终止应用   ' NSInvalidArgumentException',原因:'不可接受的值类型   attribute:property =" XXXXX&#34 ;;所需类型= NSNumber;给定类型=   NSTaggedPointerString;值= 0。'

尝试:[在此处输入图片说明] [1]

 [message setValue:localID forKey:@"local_id"];

localID的类型为NSNumber

DataModel image:[1]:https://i.stack.imgur.com/7WTlU.png

 [self insertInLocatDB:print_data entityName:@"Message_data" withPredicate:@"receiver_id" filterWith:@"unique_id"];

使用它插入CoreData:

 -(void)insertInLocatDB :(NSMutableArray *)arr_msg entityName:(NSString*)name withPredicate:(NSString*)predicateText filterWith:(NSString*)filterText
{
    NSEntityDescription *entitydesc=[NSEntityDescription entityForName:name inManagedObjectContext:app.context];
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    [request setEntity:entitydesc];
    NSInteger local_id =[self getMaxLocalId:name];


    for(int i=0; i<[arr_msg count]; i++)
    {
        [fetched_data removeAllObjects];
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"%@ like %@ ",predicateText,[[arr_msg objectAtIndex:i]objectForKey:predicateText]];
        [request setPredicate:predicate];
        NSError *error;
        NSArray *matchingData=[app.context executeFetchRequest:request error:&error];

        local_id=local_id+1;

        for (NSManagedObject *obj in matchingData)
        {
            [fetched_data addObject:[obj valueForKey:filterText]];
        }
        NSString *filter=[[arr_msg objectAtIndex:i] objectForKey:filterText];
        if(![fetched_data containsObject:filter])
        {
            NSManagedObject *message=[[NSManagedObject alloc] initWithEntity:entitydesc insertIntoManagedObjectContext:app.context];
            [arr_keys setArray:[[arr_msg objectAtIndex:i] allKeys]];
            [arr_values setArray:[[arr_msg objectAtIndex:i] allValues]];
            [message setValue:[NSNumber numberWithInteger:local_id] forKey:@"local_id"];
            for (int i=0;i<[arr_keys count];i++)
            {
                if( [[message.entity propertiesByName] objectForKey:[arr_keys objectAtIndex:i]] != nil
                   && ([[[message.entity propertiesByName] objectForKey:[arr_keys objectAtIndex:i]] isKindOfClass:[NSAttributeDescription class]]) )
                {
                    [message setValue:[arr_values objectAtIndex:i] forKey:[arr_keys objectAtIndex:i]];
                }
            }
            [arr_keys removeAllObjects];
            [arr_values removeAllObjects];
        }
    }
}

2 个答案:

答案 0 :(得分:2)

如果您有我的屏幕截图中显示的核心数据属性。

enter image description here

然后,以下代码片段将适合您。

BOOL result;
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newMessage = [NSEntityDescription insertNewObjectForEntityForName:@"message"
                                                              inManagedObjectContext:context];
    [newMessage setValue:[NSNumber numberWithInt:local_id] forKey:@"local_id"];
    [newMovie setValue:movieName forKey:@"message"];

    NSError *error = nil;
    // Save the object to persistent store
    result = YES;
    if (![context save:&error]) {
        result = NO;
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    return  result;

以及获取托管对象上下文的函数

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

编辑Sandhya代码。

-(NSInteger)getMaxLocalId :(NSString*)entitiyName
{
    NSEntityDescription *entitydesc=[NSEntityDescription entityForName:entitiyName inManagedObjectContext:app.context];
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    [request setEntity:entitydesc];
    NSInteger local_id = 0;
    request.fetchLimit = 1;
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"local_id" ascending:NO]];
    NSError *error = nil;
    local_id=[[app.context executeFetchRequest:request error:&error].firstObject integerValue];
    return local_id;
}

然后使用以下内容插入CoreData

NSInteger local_id =[self getMaxLocalId:name];
local_id=local_id+1;
[message setValue:[NSNumber numberWithInteger:local_id] forKey:@"local_id"];

确保你获得local_id。

由于你有NSInteger,要进行转换,你应该使用[NSNumber numberWithInteger:]而不是[NSNumber numberWithInt:]

更新2:

NSString *filter=[[arr_msg objectAtIndex:i] objectForKey:filterText];
if(![fetched_data containsObject:filter])
{
    NSManagedObject *message=[[NSManagedObject alloc] initWithEntity:entitydesc insertIntoManagedObjectContext:app.context];
    [arr_keys setArray:[[arr_msg objectAtIndex:i] allKeys]];
    [arr_values setArray:[[arr_msg objectAtIndex:i] allValues]];
    [message setValue:[NSNumber numberWithInteger:local_id] forKey:@"local_id"];
    for (int i=0;i<[arr_keys count];i++)
    {
        if( [[message.entity propertiesByName] objectForKey:[arr_keys objectAtIndex:i]] != nil
           && ([[[message.entity propertiesByName] objectForKey:[arr_keys objectAtIndex:i]] isKindOfClass:[NSAttributeDescription class]]) )
        {
            if(![[arr_keys objectAtIndex:i] isEqualToString:@"local_id"]){
                [message setValue:[arr_values objectAtIndex:i] forKey:[arr_keys objectAtIndex:i]];
            }
        }
    }
    [arr_keys removeAllObjects];
    [arr_values removeAllObjects];
} 

答案 1 :(得分:0)

-(NSInteger)getMaxLocalId :(NSString*)entitiyName
{
    NSEntityDescription *entitydesc=[NSEntityDescription entityForName:entitiyName inManagedObjectContext:app.context];
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    [request setEntity:entitydesc];
    NSInteger local_id = 0;
    request.fetchLimit = 1;
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"local_id" ascending:NO]];
    NSError *error = nil;
    local_id=[[app.context executeFetchRequest:request error:&error].firstObject integerValue];
    return local_id;
}

然后使用以下内容插入CoreData

NSInteger local_id =[self getMaxLocalId:name];
local_id=local_id+1;
[message setValue:myNumber forKey:@"local_id"];