How to Create Swift Class for Category

How to create swift class for category?

In Swift, you can use Extensions to add new functionality to existing classes, structs and enumeration types.

They differ from Objective-C categories in a few ways, mainly:

  • They aren't named
  • You don't need to import an Extension explicitly. If you define an extension to add new functionality to an existing type, the new functionality will be available on all existing instances of that type, even if they were created before the extension was defined.
  • As stated above, they work not only with classes, but with other types as well.

As it stands today, Extensions can:

  • Add computed properties and computed static properties
  • Define instance methods and type methods
  • Provide new initializers
  • Define subscripts
  • Define and use new nested types
  • Make an existing type conform to a protocol

The basic syntax to declare an extension is as follows:

extension SomeType {
// new functionality to add to SomeType goes here
}

Check Apple's documentation for more information on how to use Extensions in Swift.

Extending a Swift class with Objective-C category

The Bad

i too have been fighting this issue a bunch. unfortunately the documentation pretty explicitly states that this pattern is not allowed:

To avoid cyclical references, don’t import Swift code into an
Objective-C header (.h) file. Instead, you can forward declare a Swift
class or protocol to reference it in an Objective-C interface.

Forward declarations of Swift classes and protocols can only be used
as types for method and property declarations.

also throughout the the linked page you will notice it keeps mentioning to import the generated header specifically into the .m file:

To import Swift code into Objective-C from the same target

Import the Swift code from that target into any Objective-C .m file
within that target

The Good

one solution that may work for you is to create a swift extension that redefines each method you need in the category. it is fragile and ugly, but arguably the cleanest solution.

/**
Add category methods from objc here (since circular references prohibit the ObjC extension file)
*/
extension SomeClass {
@nonobjc func someMethod() {
self.performSelector(Selector("someMethod"))
}
}
  • adding the @noobjc to the front allows the
    same method signature to be used w/o overriding the ObjC implementation
  • now the import "SomeClass+Extension.h" from the bridging
    header can be removed

if support for more than two input params is needed, or tighter type coupling is desired i would recommend using the runtime to call the underlying function. a great description is here.

Swift Create Categories for Items (Classes + Attributes) at runtime

I would create a general CategoryDefinition class/struct and a Category class/struct that looked something like

class CategoryDefinition {
var categoryName: String
var fields: [String: FieldType] //Dictionary with field definitions
}

class Category {
var values: [String: Any?] //Dictionary with field values
}

then two functions to convert from and to Firebase format.

To make it more flexible to work with this I would have a protocol with the properties and functions I mentioned. It would make sense to have a Factory class to generate Category instances from CategoryDefinition.

FieldType above is my made up enum since I assume you have pre-defined set of data types-

How to call below objective c category from swift class

You call it as simply as:

imgView.startScaningRepeatCount(2)

Or

imgView.startScaningRepeatCount(1000, duration: 2)

Or

imgView.startScaningRepeatCount(1000, duration: 2, heightFactor: 0.3)

Etc.

Remember to import your category's header from the project's bridging header.

How do I create a category in Xcode 6 or higher?

They didn't forget. They just moved it without telling anyone.

  1. Click File -> New -> File

  2. Select Objective-C file under Sources in iOS or Mac OS respectively and Click Next

  3. Now under File Type: choose either Category, Protocol, or Extension

PS. Under File Name: whatever you type here will be either the Category, Protocol, or Extension Name.

Swift: How to call a category or class method from Objective-C

There are various cases for categories function and it's calling conventions.

1)In swift class functions in categories can be imported as convenience initializes if they returned same type on which they called like

 +(UIImage *) imageOrPDFNamed:(NSString *)resourceName ;

so it can be called as

    UIImage(orPDFNamed: "abc")

2)However if class functions in categories are not returning same object type(UIImage) on which it calls in this case UIImage like

   +(int) imageOrPDFNamedInt:(NSString *)resourceName ;

it will called as

    UIImage.imageOrPDFNamedInt("abc")

3)If categories function is not class function so it will call as directly on instance of UIImage like

   -(UIImage *) imagePDFNamed:(NSString *)resourceName ;

called as

    UIImage().imagePDFNamed("abc")


Related Topics



Leave a reply



Submit