简体   繁体   中英

iOS/objective-c: Spooky error setting value of object

I have copied some code that works fine for saving an api to one entity in core data over to save a similar api to another entity. Although everything is largely the same, I cannot get rid of exception error.

Here is the code that throws the exception:

- (NSMutableArray *) convertFeedtoObject:(NSMutableArray*)feed {
    NSMutableArray * _newitems = [[NSMutableArray alloc] init];
    for (int i = 0; i < feed.count; i++) {
        NSDictionary *feedElement = feed[i];

        ItemOnServer *newItem = [[ItemOnServer alloc] init];
        newItem.itemtitle = feedElement[@"itemtitle"]; //throws exception
        newItem.item = feedElement[@"item"];//also throws an exception if above commented out
         newItem.descript = @"some item"; //if above lines commented out, littoral throws exception too.
//same goes for any other values I try to set.  The first one throws exception.

Here is the error:

2015-11-20 05:26:03.281 [20610:60b] -[ItemOnServer setItem:]: unrecognized selector sent to instance 0x175721b0

No matter what values I try to set, I get a similar error even though I know the values are there.

One item of the feed looks like this:

 {
        lastviewed = "2015-11-17 15:21:45";
        itemtitle = "New";
        id = 944;
        item = "cotton shirt";

    }
)

Thanks for any suggestions.

Edit:

Code from ItemOnServer.h

//.m file is largely empty
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class ItemsVC;
@class Vendors;

@interface ItemOnServer : NSObject
@property (nonatomic, retain) NSString * item;
@property (nonatomic, retain) NSNumber * id;//this is permanent id shared with server
@property (nonatomic, retain) NSNumber * localid; //this is temp id if first created locally
@property (nonatomic, retain) NSString * itemtitle;
@property (nonatomic, retain) NSDate * lastviewed;

//this is relationship with vendors
@property (nonatomic, retain) Vendors *vendor;

@end

It looks like your class ItemOnServer doesn't have implementations for the methods setItem:, setItemtitle and so on. I don't know how you managed to do this, but you should look at the interface and implementation of ItemOnServer.

Look at what the error message says:

 -[ItemOnServer setItem:]: unrecognized selector

That means a setItem: message is sent to an object, that object is an ItemOnServer object, and it doesn't implement setItem: The first two of these three points are exactly what you expect to happen when you write newItem.item = ...

Do you have another class named ItemOnServer in your application, maybe linked through some library? You can easily check by renaming ItemOnServer with ItemOnServer2 and checking what happens.

Instead of using an NSDictionary declare feedElement as an NSMutableArray , so something like:

- (NSMutableArray *) convertFeedtoObject:(NSMutableArray*)feed {
    NSMutableArray * _newitems = [[NSMutableArray alloc] init];
    NSMutableArray *feedElement = [[NSMutableArray alloc] init];
    for (int i = 0; i < feed.count; i++) {
        feedElement = feed[i];

        ItemOnServer *newItem = [[ItemOnServer alloc] init];
        newItem.itemtitle = feedElement[@"itemtitle"];
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM