iosswiftobjective-c-categoryswift-extensions

Is it safe to extend an Objective-C class in Swift with non-prefixed methods?


In Objective-C, one should always prefix category methods, e.g. if extending UIView with the method descendants, you'd add zzz_ and make it zzz_descendants to avoid naming conflicts. Is that necessary for a function in extension UIView { ... } in Swift?


Solution

  • It's not necessary in order to compile. However, it's a good idea to prefix extension methods to avoid possible conflicts later on, in case Apple should introduce a method by the same name. An even more important reason, in my opinion, is to make clear that the extension method is custom code and not part of the standard API.