Unable to get rid of compiler warnings for primitiveValue primitives in transient getter impls properties

I implemented the transient property, as shown below, on one of the models of my application. It is declared in model design as a transient property of type undefined.

@property (nonatomic, readonly) NSNumberFormatter *currencyFmt; 

Current (warning) type of this accessory:

 - (NSNumberFormatter *) currencyFmt { [self willAccessValueForKey:@"currencyFmt"]; NSNumberFormatter *fmt = [self primitiveValueForKey:@"currencyFmt"]; [self didAccessValueForKey:@"currencyFmt"]; if (fmt == nil) { fmt = [[[NSNumberFormatter alloc] init] autorelease]; [fmt setNumberStyle:NSNumberFormatterCurrencyStyle]; [fmt setLocale:[self localeObject]]; [self setPrimitiveValue:fmt forKey:@"currencyFmt"]; } return fmt; } 

The call to primitiveValueForKey: is a problem here, since the documentation specifically warns about using this version of primitive search:

You are strongly encouraged to use dynamically generated than using this method directly (for example, primitiveName: instead of primitiveValueForKey: @ "name"). dynamic accessors are much more efficient and allow compile-time validation.

The problem is that if I try to use primitiveCurrencyFmt instead of primitiveValueForKey:@"currencyFmt" , I get a compiler warning saying that the object may not respond to this selector. Everything works fine at runtime if I just ignore this warning, but the warnings are terrible and I don't want to comment on them there.

I tried declaring the property with @dynamic and @synthesize at the top of the file and nothing helps. What do I need to do to use the recommended dynamic accessors without generating these warnings?

Any help is greatly appreciated.

+4
source share
2 answers

Declare methods in a category in a managed object class:

 @interface MyManagedObject : NSManagedObject ... @end @interface MyManagedObject (PrimitiveAccessors) - (NSNumberFormatter*)primitiveCurrencyFmt; - (void)setPrimitiveCurrencyFmt:(NSNumberFormatter*)value; @end 

Apple uses this pattern in several places in the documentation to suppress compiler warnings.

+6
source

With auto- synthesize (new since 2010 when it was asked / answered), you can declare properties instead. Less code, eliminate typos, etc.

 @interface MyManagedObject (PrimitiveAccessors) @property (nonatomic) NSNumberFormatter *primitiveCurrencyFmt; @end 

Apple example .

0
source

Source: https://habr.com/ru/post/1333827/


All Articles