2013-04-18 17 views
5

Errore di dati di base che non riesco a capire come risolvere.tipo desiderato NSNumero tipo di tipo __NSCFString error

Fondamentalmente sto estraendo tutti i dati di un oggetto in un dizionario, mostrando i dati a un modulo e alcuni campi consentono la modifica, quindi cercando di memorizzare i dati sull'oggetto al momento dell'invio.

Tuttavia, l'impostazione di tutti i nuovi valori/aggiornati ottengo l'errore

Unacceptable type of value for attribute: property = "totalLocations"; desired type = NSNumber; given type = __NSCFString; value = 7.

Ecco il codice che gestisce questa struttura particolare ...

//grab the value from the property 

    if (myObject.totalLocations) 
    [data setObject:myObject.totalLocations forKey:@"totalLocations"]; 

    // store it back to the object 
    _myObject.totalLocations = [data objectForKey:@"totalLocations"]; 

Oltre a questi due linee non c'è troppo uso della proprietà. può essere modificato, ma non dall'utente su questo particolare schermo

risposta

6

Il tipo di totalLocations nell'entità dati principale Integer e myObject.totalLocations a NSString? Se sì è necessario impostare i dati di base di questo tipo:

[data setValue:[NSNumber numberWithInteger:[myObject.totalLocations integerValue]] forKey:@"totalLocations"]; 

Il modo in cui ho impostato i miei oggetti gestiti è come questo:

- (void)insertNewPromo:(NSDictionary *)promoJson 
{ 
    NSManagedObjectContext *context = [self.promoFetchedResultsController managedObjectContext]; 
    NSEntityDescription *entity = [[self.promoFetchedResultsController fetchRequest] entity]; 
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context]; 

    // Checking if inappropriate data are in the JSON to avoid some crashes. 
    if ([[promoJson objectForKey:@"id"] isKindOfClass:[NSNull class]]) 
     [newManagedObject setValue:nil forKey:@"id"]; 
    else 
     [newManagedObject setValue:[NSNumber numberWithInteger:[[promoJson objectForKey:@"id"] integerValue]] forKey:@"id"]; 
    ... 
    ... 
    NSError *error = nil; 
    if (![context save:&error]) 
    { 
     if (DEBUG_ON == 1) 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
} 

l'oggetto della promoJsonid è un NSString

+0

rappresentazione più concisa è '@ (myObject.totalLocations.integerValue)'. –

+0

myObject.totalLocations è un NSNumber – JMD

+0

qual è il tipo di '[chamberData objectForKey: @" totalLocations "]'? – Jeremy