使用目标c中的核心数据获取数据

时间:2015-03-26 10:13:16

标签: ios objective-c core-data fetch

我正在制作一个应用程序来保存字段。当我点击按钮时,我收到错误。我需要获取数据并在保存后更新数据。

这是我的代码!

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObjectContext *serverIP;
serverIP = [NSEntityDescription insertNewObjectForEntityForName:@"IP" inManagedObjectContext:context];
[serverIP setValue:self.txtServerAdress.text forKey:@"ipaddress"];
[serverIP setValue:self.txtServerPort.text forKey:@"serverport"];

这是我得到的错误......

2015-03-26 12:12:06.914 SaphiraCrm[12306:3527354] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
2015-03-26 12:12:08.307 SaphiraCrm[12306:3527354] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fb4cac58470 H:[UITextField:0x7fb4cac6a2e0(243)]>",
    "<NSLayoutConstraint:0x7fb4cac6cdb0 H:[UITextField:0x7fb4cac6a2e0]-(38)-|   (Names: '|':UIView:0x7fb4cac67c50 )>",
    "<NSLayoutConstraint:0x7fb4cac6ce00 H:|-(39)-[UITextField:0x7fb4cac6a2e0]   (Names: '|':UIView:0x7fb4cac67c50 )>",
    "<NSLayoutConstraint:0x7fb4cac717b0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb4cac67c50(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb4cac58470 H:[UITextField:0x7fb4cac6a2e0(243)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-03-26 12:12:08.309 SaphiraCrm[12306:3527354] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fb4cac51670 H:[UITextField:0x7fb4cac6b3c0(243)]>",
    "<NSLayoutConstraint:0x7fb4cac6cef0 H:[UITextField:0x7fb4cac6b3c0]-(38)-|   (Names: '|':UIView:0x7fb4cac67c50 )>",
    "<NSLayoutConstraint:0x7fb4cac717b0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb4cac67c50(375)]>",
    "<NSLayoutConstraint:0x7fb4cac6cf40 H:|-(39)-[UITextField:0x7fb4cac6b3c0]   (Names: '|':UIView:0x7fb4cac67c50 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb4cac51670 H:[UITextField:0x7fb4cac6b3c0(243)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-03-26 12:12:12.387 SaphiraCrm[12306:3527354] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'IP''
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101968a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101601bb7 objc_exception_throw + 45
    2   CoreData                            0x000000010063cf07 +[NSEntityDescription entityForName:inManagedObjectContext:] + 263
    3   CoreData                            0x000000010067896e +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 30
    4   SaphiraCrm                          0x0000000100518c4b -[SettingsViewController btnKaydet:] + 187
    5   UIKit                               0x0000000102168a22 -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x000000010226fe50 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x000000010226f21f -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x00000001021aeb68 -[UIWindow _sendTouchesForEvent:] + 735
    9   UIKit                               0x00000001021af493 -[UIWindow sendEvent:] + 683
    10  UIKit                               0x000000010217bfb1 -[UIApplication sendEvent:] + 246
    11  UIKit                               0x0000000102189227 _UIApplicationHandleEventFromQueueEvent + 17700
    12  UIKit                               0x000000010216423c _UIApplicationHandleEventQueue + 2066
    13  CoreFoundation                      0x000000010189dc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x0000000101893b5d __CFRunLoopDoSources0 + 269
    15  CoreFoundation                      0x0000000101893194 __CFRunLoopRun + 868
    16  CoreFoundation                      0x0000000101892bc6 CFRunLoopRunSpecific + 470
    17  GraphicsServices                    0x0000000105abda58 GSEventRunModal + 161
    18  UIKit                               0x0000000102167580 UIApplicationMain + 1282
    19  SaphiraCrm                          0x000000010051bed3 main + 115
    20  libdyld.dylib                       0x0000000103d92145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

3 个答案:

答案 0 :(得分:0)

您的[appDelegate managedObjectContext]似乎返回nil个对象。调试并查看它为nil的原因。修复它,你的代码应该可以正常工作。

答案 1 :(得分:0)

您必须像这样保存数据:

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObjectContext *serverIP = [NSEntityDescription insertNewObjectForEntityForName:@"IP" inManagedObjectContext:context];

serverIP.ipaddress = [key objectForKey:@"ipaddress"];
...

if (![context save:&error]) {
    NSLog(@"Error: %@", [error localizedDescription]);
}

你也应该注意你的约束问题。

答案 2 :(得分:0)

您需要更改以下行:

NSManagedObjectContext *serverIP;
serverIP = [NSEntityDescription insertNewObjectForEntityForName:@"IP" inManagedObjectContext:context];

变化:

NSManagedObject *serverIP;
serverIP = [NSEntityDescription insertNewObjectForEntityForName:@"IP" inManagedObjectContext:context];