1

I want to add warnings to some methods that have legacy code in both objective C and Swift , whenever these methods are used somewhere by other developers, I want them to be warned that they are using legacy code, it is not required to be shown as deprecated or obsolete I only need to warn them about it.

Eg. Let this be the function definition

func someLegacyMethod() {
#warning this is legacy code
// some function
}

When a user calls this method I want him to be warned. I thought of using swiftlint but couldn't find a way to do this because the definition is elsewhere and the calling place is elsewhere and this needs to be future proof because these methods can be used in many places in the future. I am open to exploring other ways to implement this too.

someLegacyMethod() //warning should appear here

Edit1: I found this: https://blog.twitter.com/engineering/en_us/a/2014/attribute-directives-in-objective-c and tried using attribute((warning(…))) but XCode is complaining : unknown attribute 'warning' ignored.

KP1
  • 69
  • 7
  • 1
    You can use [@available to deprecate old APIs](https://www.hackingwithswift.com/example-code/language/how-to-use-available-to-deprecate-old-apis). – Dávid Pásztor Aug 11 '22 at 08:35
  • I need custom @available for objective C code that is called from swift also but thanks for this it solves my swift issue – KP1 Aug 11 '22 at 08:42
  • 1
    You can also [mark Obj-C methods as deprecated](https://stackoverflow.com/a/22907726/4667835) and the deprecation warning will appear in Swift as well. – Dávid Pásztor Aug 11 '22 at 08:54
  • I checked this but is it possible to just warn them without showing this as deprecated? (Edited the question) I just checked all the available variants for attribute but couldn't find any. – KP1 Aug 11 '22 at 09:33
  • This is the thing I want but for Objective C https://stackoverflow.com/questions/57642989/a-more-standard-attribute-warningmsg – KP1 Aug 11 '22 at 11:12
  • If you want a runtime warning, wouldn't `NSLog()` suffice? – Gerd K Aug 12 '22 at 06:29
  • Not runtime warning more like when anyone tries to use the method I must warn them. – KP1 Aug 12 '22 at 07:53

0 Answers0