-1

For the following code, of the two comment preceded lines below, only the first works.

NSManagedObject * Event = [NSEntityDescription insertNewObjectForEntityForName:str inManagedObjectContext:app.managedObjectContext]; 
//Work but strange
[Event setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:value]; 
 //Error
Event.value= [NSNumber numberWithInt:buf4[v+h]]; 

The second returns and error

request for member 'value' in 'Event', which is of non-class type 'NSManagedObject*'
TechZen
  • 64,370
  • 15
  • 118
  • 145
Andrew H
  • 25
  • 3
  • 1
    What is the error that you're getting? – Kris Markel Oct 11 '10 at 18:51
  • Hi Andrew H. You're new to the site. As a new user, you should understand that questions phrased liked this -- devoid of any detail that would help someone answer your question, and also lacking detail from you of what you tried to do to fix it so far -- will result in very low quality answers, if you get any at all. You should rephrase this question (don't start a new one, just edit this one) and make the problem much more clear. – Shaggy Frog Oct 11 '10 at 21:22
  • 1
    The example code includes a lot of undefined and irrelevant variables that we, the StackOverflow community, have to dig past while analyzing this problem. What is `buf4` and why is it being indexed by the sum `v+h`? In the end, it turns out not to matter and the whole `[NSNumber numberWithInt:buf4[v+h]]` could be replaced with `foo`, which would make the question much easier to read. – John Franklin Oct 11 '10 at 23:09
  • +1 I edited the question to make it clear. The actual question is rather good so others should up vote the edited question. – TechZen Oct 12 '10 at 21:05

2 Answers2

5

Before we begin, it is important to point out that there are conventions that should be followed when writing code in Objective-C. Class names should start with a capital letter: Event, NSManagedObject, MKMapView. Variables should start with a lowercase letter: event, currentUser, myMapView.

Now, to your problem. [Event setValue:foo forKey:value] and Event.value=foo (sic) are not the same, except in the case where the variable value is an NSString containing the string value. (i.e., NSString *value = @"value") Put more clearly,foo.bar=baz is equivalent to [foo setValue:baz forKey:@"bar"] not [foo setValue:baz forKey:bar].

John Franklin
  • 4,962
  • 1
  • 23
  • 27
3

The problem you are seeing is caused by a behavior of the NSManagedObject class called associative storage.

Associative storage basically turns any generic NSManagedObject into a dictionary whose keys are the names of the property of the entity assigned to it. You set and access the values for the keys just like you would for a dictionary or any other key-value compliant class. So when you use a generic managed object like this:

NSManagedObject * Event = [NSEntityDescription insertNewObjectForEntityForName:str inManagedObjectContext:app.managedObjectContext]; 

... you get a generic NSManagedObject instance with the keys of the str entity as defined in the data model. So, when you can use key-value coding to store the value in the generic NSManagedObject instance:

[Event setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:value];

or more clearly something like:

[Event setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:@"anAttributeName"];

However, dot notation is something different. When you call Event.anAttributeName you are calling a method within a custom subclass of NSManagedObject. In order for this to work, you must generate a custom subclass with the name Event and assign it to the Event entity in the data model.

When you use dot notation you are calling a method that looks something like this:

-(void) setAnAttributeName:(NSNumber *) aNumber{
    //some boilerplate
    [self setPrimativeValue:aNumber forKey:@"anAttributeValue"];
    // some more boilerplate
}

You can write the method yourself or use the @dynamic compiler directive to do it for but either way, you must have the method. No method, no dot notation.

When you are just learning Core Data it is best to use generic NSManagedObjects and setValue:forKey: the move on to custom NSManagedObject subclasses.

TechZen
  • 64,370
  • 15
  • 118
  • 145