0

We are making a big change to our Core Data model. There are a couple of attributes that we don't want to use anymore. For example, the following values for SomeObject.

However, we don't want to simply remove values from our Core Data yet because we are using it everywhere in our project.

I am wondering whether it is possible to add a deprecation tag to some of the attributes in Core Data, so that we will get some warning whenever we use them.

@interface SomeObject : _SomeObject
// ... 
@end

@interface _SomeObject : NSManagedObject {}
@property (nonatomic, strong) NSNumber* values;
// and a massive amount of auto-generated code by core data
@end

And then I saw this post about how to flag a method as deprecated in objective C. And I tried adding the deprecation tag inside the _SomeObject.h such as:

@interface _SomeObject : NSManagedObject {}
@property (nonatomic, strong) NSNumber* values __attribute__((deprecated));
// and a massive amount of auto-generated code by core data
@end

And it works exactly the way as we want where we have 'values' is deprecated warnings all over the place. So we will be able to focus on all these warnings and fix all of them before our next ship. But one thing that I don't feel quite comfortable about is that we are modifying the auto generated code by Core Data.

So finally, my question is:

Is it possible to add deprecation to methods without touching the _SomeObject.h file?

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234

2 Answers2

2

Not so as directly to generate compiler warnings; manually modifying the interface file is the correct way to do that. You're not introducing any problems beyond someone potentially regenerating the file and losing the attribute.

A possibly preferable rounabout alternative would be to rename the properties, e.g. to valuesDeprecated. Keep the canonical name as values for appropriate migration. Write a category on _SomeObject with the deprecated property values and implement that just to use valuesDeprecated as storage.

Then existing code should get warnings and new code can avoid warnings only by using a property with the word 'deprecated' in it. So the author would need to be deliberately careless.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

Yes there is, as I had a discussion with Apple about this and it's being done in a different manner due to swift being in play.

The proper way to check for depreciations is to have the depreciated objects return NIL as a class.

if ([UIPrintInteractionController class]) {
    // Create an instance of the class and use it.
} else {
    // Alternate code path to follow when the
    // class is not available.
}

[self class] is literally a class method returning class (object etc...). All you need to do is return Nil (as per Apple Docs) The same goes for init methods. Just return

On a system where the UIPrintInteractionController isn’t implemented it will be nil and sending it the class message will return nil. Testing for this will allow you to write code that both runs on older systems and can take advantage of new frameworks & APIs on newer OSes

nil.

This is a direct quote from the Apple Developer tech Support I had a lengthy word with:

Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21