0

I have a class which declares a User and includes these variables:

(User.h file):

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * unid;

(User.m file):

@dynamic name;
@dynamic unid;

I have an array of values by parsing a string. I then want to set the values accordingly:

(ViewController.m file):

[user setName:[returned objectAtIndex:1]];
[user setUnid:[returned objectAtIndex:2]];

When this is run the compiler gives me the following error:

unrecognized selector sent to instance
*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[User setName:]: unrecognized selector sent to instance

When I comment out the setName line it works fine.

I then looked at the classes of the two objects from the returned array and they were both: __NSCFString

I then tried this piece of code:

(ViewController.m file):

[user setName:[returned objectAtIndex:2]];

Again the same error.

Why would the same input fail in one case and succeed in another if they are both expecting the same input?

Thanks.

EDIT:

This error is weird as this part of the app does not interact with any webviews. This returned array is parsed from a string gather from a webpage:

(ViewController.m file):

NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://url_goes_here.com" encoding:NSUTF8StringEncoding error:&error];
NSArray *returned = [returned componentsSeparatedByString:@"#"];

However the unid is also parsed from this array without any problem.

carloabelli
  • 4,289
  • 3
  • 43
  • 70

1 Answers1

3

The @dynmaic keyword means that you'll be providing the accessors yourself. If you want the compiler to create accessors for you, simply delete the @dynamic name declaration. (Because @synthesize is now the default, you don't have to use it explicitly.) Otherwise, you'll need to create the -name, -setName:, -unid, and -setUnid: methods yourself.

it is actually a set up class by xcode using the NSManagedObject subclass for core data

This is an important detail. In the case of managed objects, Core Data will provide the accessors for you and you just need the @dynamic property declaration to let the compiler know that it shouldn't generate accessors itself.

I'm a little confused as to why this error is coming from a web view delegate method. It might help if you could explain a little more about how your Core Data classes are interacting with a web view.

why would this work for the unid, but not the name

The error you're getting is an run time error -- an exception is being thrown. It's likely that the name accessor is simply the first one to be used; the same thing might happen for unid if that property were to be set first.

This error is weird as this part of the app does not interact with any webviews.

Another important clue. At this point, it sounds very much like you've got a bad pointer. You're sending -setName: to an object that's not what you think it is, and in this case it turns out to be a web view delegate. Try turning on NSZombies to help you track this down.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • using synthesize for all of them works, but why would dynamic work for unid, but not name. – carloabelli Jul 09 '13 at 23:32
  • `@dynamic` is different in that it expects a definition somewhere, it doesn't just have to be in the current class. For example, all of NSManagedObject generated from models are `@dynamic` because the compiler generates the accessors for their properties. Think of `@dynamic` as dynamic `extern` – CodaFi Jul 09 '13 at 23:43
  • updated the question. yes the problem with `@synthesize` is that it did not load the variables. – carloabelli Jul 10 '13 at 00:58
  • also about the end to your question: I commented out the setName and it worked perfectly, except for saving the name obviously – carloabelli Jul 10 '13 at 01:19
  • I fixed it by having Xcode automatically recreate the file. For some reason you cannot just add properties. Xcode must do some weird behind the scenes stuff with web views. – carloabelli Jul 11 '13 at 01:54