如何从NSMutableArray中获取存储在Objective C中的CoreData中的NSString的值

时间:2016-12-08 10:30:02

标签: objective-c xcode core-data

我是iOS新手,我遇到了在nsstring中获取coredata mutablevalue的问题。 我的代码就像这样

在索引路径的行中的单元格

       static NSString *MyIdentifer =@"MyReuseIdentifer";
        UITableViewCell *cell=[tableView dequeueReusableHeaderFooterViewWithIdentifier:MyIdentifer];
        if (cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifer ];
        }
        // Configure the cell...
        NSManagedObject *device2 = [devices objectAtIndex:indexPath.row];
        NSLog(@"Devices =%@",devices);
        [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device2 valueForKey:@"Key"]]];
        NSLog(@"Cell All Text =%@",cell.textLabel.text);
         NSString *string = [devices componentsJoinedByString:@","];
        NSLog(@"String value =%@",string);

        return cell;

MutableArray中的数据就像这样

{
    Key = 46;
}),<NSManagedObject: 0x7f988612f560> (entity: Device; id: 0xd000000000080000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p2> ; data: <fault>),<NSManagedObject: 0x7f988612f5e0> (entity: Device; id: 0xd0000000000c0000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p3> ; data: <fault>),<NSManagedObject: 0x7f988612f660> (entity: Device; id: 0xd000000000100000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p4> ; data: <fault>),<NSManagedObject: 0x7f988612f6e0> (entity: Device; id: 0xd000000000140000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p5> ; data: <fault>),<NSManagedObject: 0x7f988612f760> (entity: Device; id: 0xd000000000180000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p6> ; data: <fault>),<NSManagedObject: 0x7f988612f7e0> (entity: Device; id: 0xd0000000001c0000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p7> ; data: <fault>),<NSManagedObject: 0x7f988612f860> (entity: Device; id: 0xd000000000200000 <x-coredata://7845A868-643C-4CFE-8FC8-283C1C7D0A37/Device/p8> ; data: <fault>)

我在TableView中获得正确的值

如何在NSString中获取MutableArray的值。注意,Devices是一个NSMutableArray。

获取请求

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
        [context deleteObject:[self.devices objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

        // Remove device from table view
        [self.devices removeObjectAtIndex:indexPath.row];
        [Audittable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

提前致谢!

0 个答案:

没有答案