-1

I am trying to convert JSON array to object for display in table. I am able to capture JSON. However, when I try to turn into object, I get error shown below. There is definitely a property named "name" in object class.

- (void)fetchedData:(NSData *)responseData {
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                                                         options:kNilOptions
                                                           error:&error];
    NSMutableArray* latestItems = nil;
    latestItems = [[NSMutableArray alloc] init];
    latestItems = [json objectForKey:@"items"];
    [self.tableView reloadData];
    for (int i = 0; i < latestItems.count; i++)
    {
        NSDictionary *itemElement = latestItems[i];
        // Create a new l object and set its props to todoElement properties
        IDItemFromServer *newItem = [[IDItemFromServer alloc] init];
//ERROR  NEXT LINE THROWS FOLLOWING ERROR
//'NSUnknownKeyException', reason: '[<IDItemFromServer 0x14e88010> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
       [newItem setValue:@"hi" forKey:@"name"];
       [newItem setValue:itemElement[@"address"] forKey:@"address"];

        // Add this new item  to the array
        [latestItems addObject:newItem];
    }

Would appreciate any suggestions on how to fix error.

Edit:

//Object.h file
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface IDItemFromServer : NSObject
@property (nonatomic, retain) NSNumber * iid;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;

@end
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • 1
    The error indicates there is not a property named `name`. Update your question with details about the interface for the `IDItemFromServer` class. – rmaddy Oct 18 '15 at 16:49
  • If you have a nice public property, why not simply use `newItem.name = @"hi";` instead of using `setValue:forKey:`? – rmaddy Oct 18 '15 at 17:00
  • I was trying this before and it was giving me //[IDItemFromServer setName:]: unrecognized selector sent to instance 0x16d7dfd0 error, however, I will try it again. – user1904273 Oct 18 '15 at 17:04
  • yes it is throwing IDItemFromServer setName:]: unrecognized selector sent to instance 0x15599ed0' that way – user1904273 Oct 18 '15 at 17:06
  • Something is off. The `IDItemFromServer` interface you posted claims there is such a property but at runtime there clearly is no such property. Must be some strange core data issue and I've never used core data. – rmaddy Oct 18 '15 at 17:08
  • In the .m file there was a @dynamic name. I have commented this out and now error seems to be gone. (There is another error but past that line.) Don't know why dynamic would have caused error. – user1904273 Oct 18 '15 at 17:14
  • 1
    You only want `@dynamic` when the property is synthesized at runtime instead of compile time. – rmaddy Oct 18 '15 at 17:15

1 Answers1

1

It says IDItemFromServer has no property name. add the name,address property like bellow code

In IDItemFromServer.h file

#import <Foundation/Foundation.h>
@interface IDItemFromServer : NSObject

@property(nonatomic,strong) NSString name;
@property(nonatomic,strong) NSString address;

@end

In IDItemFromServer.m file

#import "IDItemFromServer.h"

@implementation IDItemFromServer

@synthesize name;
@synthesize address;

@end
Jamil
  • 2,977
  • 1
  • 13
  • 23
  • 1) The question shows the interface for `IDItemFromServer` and it does have a `name` property. 2) Why are you calling `@synthesize`? That hasn't been needed for a few years now. – rmaddy Oct 18 '15 at 17:01
  • Thanks for drawing attention to .m file. – user1904273 Oct 18 '15 at 17:15