0

den Code unten bekomme ich folgende Fehlermeldung:Objective-c-Neuling - UIViewController managedObjectContext Unbekannter Selektor gesendet Instanz

2016-04-25 10:04:51.520 eCoss[1700:620301] -[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x160383a0 
objc[1700]: EXCEPTIONS: throwing 0x14f94500 (object 0x14d07b70, a NSException) 
objc[1700]: EXCEPTIONS: searching through frame [ip=0xffffffff sp=0x26a0b0] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: searching through frame [ip=0x2 sp=0x26b980] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: unwinding through frame [ip=0xffffffff sp=0x26a0b0] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: unwinding through frame [ip=0x2 sp=0x26b980] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: handling exception 0x14f944e0 at 0x231e33b9 
objc[1700]: EXCEPTIONS: rethrowing current exception 
objc[1700]: EXCEPTIONS: searching through frame [ip=0x6 sp=0x26b980] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: terminating 
objc[1700]: EXCEPTIONS: searching through frame [ip=0x1 sp=0x26b820] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: catch(id) 
objc[1700]: EXCEPTIONS: unwinding through frame [ip=0x1 sp=0x26b820] for exception 0x14f944e0 
objc[1700]: EXCEPTIONS: handling exception 0x14f944e0 at 0x22a6f089 
2016-04-25 10:04:51.582 eCoss[1700:620301] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x160383a0' 
*** First throw call stack: 
(0x232d398b 0x22a6ee17 0x232d9325 0x232d6f51 0x232022f8 0x24dc0d9d 0x24dbffb1 0x5d06b 0x48f3d 0x48759 0x80a11 0x24d9b 0x27a70e61 0x27a70be7 0x27a70827 0x279e12fd 0x27a2ed13 0x279e12fd 0x27a2ed13 0x279e12fd 0x27a2ed13 0x27830e55 0x2786fde1 0x2786f593 0x27840055 0x2783e603 0x23295e6f 0x23295a5d 0x23293dcb 0x231e32e9 0x231e30d5 0x247d3ac9 0x278a80b9 0x1a4f1 0x22e8b873) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

CODE

-(UIViewController*) displayViewFromString:(NSString *) identifier 
{ 
    ontracAppDelegate* delegate = [ontracAppDelegate sharedAppDelegate]; 
    NSManagedObjectContext* moc = delegate.managedObjectContext; 

    if ([identifier isEqualToString:@"Notes"]) { 
     viewController = [[ontracPageNotesViewController alloc] init]; 

     NSString *requestURL = [NSString stringWithFormat: @"http://mydomain/%i/true",self.dataObject.dataPack.pack_id ]; 
     NSLog(@"%@", requestURL); 
     //the request is built based on the string 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [[NSURL alloc] initWithString: requestURL]]; 
     [request setHTTPMethod:@"GET"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

     NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
             @"CAKEPHP", NSHTTPCookieName, 
             @"mydomain.co.uk", NSHTTPCookieDomain, 
             self.cookieValue, NSHTTPCookieValue, 
             @"/", NSHTTPCookiePath, 
             nil]; 
     NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; 
     NSArray* cookieArray = [NSArray arrayWithObjects: cookie, nil]; 
     NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray]; 
     [request setAllHTTPHeaderFields:headers]; 

     NSError *err; 
     NSURLResponse *resp; 
     //the return data is formatted into a Json Response 
     NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&resp error:&err]; 
     NSError *error; 
     NSDictionary *json = [NSJSONSerialization 
          JSONObjectWithData:returnData 
          options:kNilOptions 
          error:&error]; 



     NSMutableArray *responseObjects = [json objectForKey:@"response"]; 


     for (DataObject *dataObject in responseObjects) { 
      //if ([dataObject.dataPack.pack_id intValue] == self.dataObject.dataPack.pack_id){ 
      NSArray * value = [responseObjects objectForKey:dataObject]; 
      //NSString *noteString = [[NSString alloc] init] ; 
      NSString *noteCreated = [value objectForKey:@"created"]; 
      NSArray *imageArray = [value objectForKey:@"images"]; 
      NSString *noteString = [value objectForKey:@"note"]; 
      NSString * noteUpdated = [value objectForKey:@"updated"]; 

      NSLog(@"do %@", dataObject); 

      Notes *note = [NSEntityDescription insertNewObjectForEntityForName:@"Notes" inManagedObjectContext:moc]; 


      NSLog(@"%@", note); 

      note.notes = noteString; 
      note.noteID = [NSNumber numberWithInteger: dataObject]; 
      //note.notes = self.textField.text; 
      NSMutableArray * array = [[NSMutableArray alloc] init]; 
      for (NSString *image in imageArray) { 

       NSString *image_created = [image objectForKey:@"created"]; 
       NSString *image_id = [image objectForKey:@"id"]; 
       NSString *image_data = [image objectForKey:@"image_data"]; 
       NSString *image_type = [image objectForKey:@"image_type"]; 

       NSLog(@"%@", image_id); 

       NSData *pngData = [[NSData alloc]initWithBase64EncodedString:image_data options:NSDataBase64DecodingIgnoreUnknownCharacters]; 
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
       NSString *filePath = [documentsPath stringByAppendingPathComponent:[ NSString stringWithFormat:@"%@_%i_%@_notes_image.png", delegate.userName, self.dataObject.dataPack.pack_id, [self genRandStringLength:8]]]; //Add the file name 
       [pngData writeToFile:filePath atomically:YES]; 

       [array addObject:filePath]; 

      } 
      note.dataObject = dataObject; 
      note.imageLocations = [NSKeyedArchiver archivedDataWithRootObject:array]; 
     NSLog(@"note %@", note); 
      [dataObject addNotesObject:note]; 



     //} 

     } 

     //[[[splitView viewControllers] objectAtIndex:1] pushViewController:notesView animated:YES]; 
    } 
} 

ich einen Zeilenumbruch habe bei Zeile (note.notes = noteString), in der der Fehler erzeugt wird. Zuvor ist das Notizobjekt gut protokolliert. Ich möchte nur die Daten hinzufügen, die ich von der Anfrage bekomme.

DATENSATZ Core Data

Notes.h

@class DataObject; 

@interface Notes : NSManagedObject 

@property (nonatomic, retain) NSData * imageLocations; 
@property (nonatomic, retain) NSString * notes; 
@property (nonatomic, retain) NSNumber * noteID; 
@property (nonatomic, retain) DataObject *dataObject; 

@end 

Notes.m

#import "Notes.h" 
#import "DataObject.h" 


@implementation Notes 

@dynamic imageLocations; 
@dynamic notes; 
@dynamic noteID; 
@dynamic dataObject; 

@end 

managedObjectContext - appDelegate.m

- (NSManagedObjectContext *)managedObjectContext { 

if (managedObjectContext != nil) { 
    return managedObjectContext; 
} 

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
if (coordinator != nil) { 
    managedObjectContext = [NSManagedObjectContext new]; 
    [managedObjectContext setPersistentStoreCoordinator: coordinator]; 
} 

return managedObjectContext; 
} 
+0

zeigen Sie ein Bild Ihres Kerndatenmodells und die Definition der 'Notes'-Entity-Klasse – Wain

+0

@Wain Aktualisiert die Frage, denke, ich habe alles, was Sie gefragt haben, – jampez77

+0

stürzt es auf der ersten Iteration der Schleife, die zweite oder eine andere zufällige Iteration? – Wain

Antwort

0

Es scheint, dass Ihr Problem ist, dass Ihre Datentypen sind alle durcheinander. Ihr JSON-Zeugs ist als NSDictionary aufgelistet, und Sie versuchen, ein NSMutableArray herauszunehmen. Es ist nicht möglich, dass das Array mit dem aktuellen Code veränderbar ist.

Sie iterieren dann dieses Array und versuchen, den Inhalt als DataObject Instanzen zu behandeln, aber sie werden wahrscheinlich NSDictionary Instanzen sein.

Sie verwenden auch responseObjects objectForKey:dataObject, anscheinend auf einem Array, aber dieser Aufruf sollte nur auf ein Wörterbuch funktionieren. Sie verwenden dann weiterhin objectForKey auf was der Code sagt, ist ein Array, aber wieder sollte dies ein Wörterbuch sein.

Also, ich bin irgendwie verwirrt und nicht in der Lage, genau zu sagen, wo dieses Problem ist, aber ich denke, Sie müssen die Klassenarten, mit denen Sie arbeiten, überprüfen und sicherstellen, dass es alles ist, was Sie denken, dass es ist.

0

Zeigen Sie auch den Code für Ihre managedObjectContext-Erstellung an.
Erstellen Sie ManagedObjectContext im Hintergrund Thread?
Aus der ersten Zeile des Absturzes sieht es so aus, als ob der Kontext Ihres verwalteten Objekts kein Zugriffsobjekt mit einer Notiz mit einer Notes-Eigenschaft hat.

[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x160383a0 

Schließlich überprüfen, ob Ihre JSON-Antwort richtig sein wird und nicht Eintrag mit.

+0

Ich habe die Antwort mit der Erstellung von managedObjectContext aktualisiert. Ich kann jedoch Ihre anderen Fragen nicht beantworten, da ich mit xcode Obj-C sehr neu bin und dies ein bestehendes Projekt ist, das nicht von mir selbst erstellt wurde. – jampez77