0

Customizing an iOS app in objective-c. Not super familiar with the language, and this simple task of setting up a boolean property has taken me too long.

Can anybody offer some advice here?

Video.h

@property (nonatomic, retain) NSNumber * hasCustomThumbnail;

Video.m

@implementation Video
     @dynamic hasCustomThumbnail;
@end

OtherFile referencing the video

// have tried these two an many other things...
video.hasCustomThumbnail = [NSNumber numberWithBool:NO];
video.hasCustomThumbnail = @NO;

The error I get no matter how many dozens of ways I've tried this is:

'NSInvalidArgumentException', reason: '-[Video setHasCustomThumbnail:]: unrecognized selector sent to instance 0x60c0004925c0'

Have tried many suggestions including this: Using a BOOL property

I also know the video is referenced correctly because autocomplete suggests "hasCustomThumbnail" as I start typing.

I simply cannot believe how hard OBJ-c is making this :-)

Any suggestions?

alexr101
  • 588
  • 5
  • 13

2 Answers2

1

The issue is that you have declared it as @dynamic. Just remove this line from the Video.m. Xcode will automatically synthesize the getters and setters.

either of this works.

[obj setHasCustomThumbnail:[NSNumber numberWithBool:true]];
obj.hasCustomThumbnail = [NSNumber numberWithBool:true];

@dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime).

@synthesize will generate getter and setter methods for your property.

So when you define a property with @dynamic, either your superclass or runtime should provide the necessary getter and setter. here no one has provided anything, that is the reason it is saying the unrecognized selector sent to an instance. This error will come only if it couldn't able to find a proper method for the object. Hope this will clear your doubts.

one practical use of @dynamic is that when you inherit your class from NSManagedObject, the core data will provide the setter and getters for the properties.

Kumar Reddy
  • 792
  • 6
  • 15
  • It works, man. I have the project with an example too. The error is pretty straightforward.remove the @dynamic from Video.m. By default, all the properties will be synthesized. https://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences – Kumar Reddy Dec 19 '17 at 12:27
  • updated with more explanation. Mark it if your issue resolved. Otherwise, let me know or comment. – Kumar Reddy Dec 19 '17 at 12:38
  • My bad Kumar, I rushed through the answer this morning, but did not read the synthesize suggestion. Replacing dynamic with synthesize did the trick. Thank you very much you saved me a lot of time! – alexr101 Dec 20 '17 at 03:02
0

This problem is not related to BOOL or NSNumber. It is due to your calling setIsCustomThumbnail on class Video.

Either you are doing video.isCustomThumbnail = or [video setIsCustomThumbnail:] but your class does not declare this isCustomThumbnail. From your code seems you mispelled isCustomThumbnail as hasCustomThumbnail.

So make up your mind and use single name.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • I can see where ur coming with this. But thats wrong. I changed the name a couple of times during all my experimenting but all the names currently match that was just a typo in my question. – alexr101 Dec 19 '17 at 12:24
  • Then correct the typo for the next person trying to answer your question. – GeneCode Dec 19 '17 at 12:25