How to create swift class for category?

拜拜、爱过 提交于 2019-11-27 17:11:50

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.

Suragch

In Objective C they were called categories, but in Swift they are called extensions. The purpose of both of them are to give additional functionality to existing classes without having to create subclasses.

I had read about extensions in the documentation, but I didn't really understand how to use one in my project until I watched this tutorial video (YouTube version, github source).

Here is a summary taken from the video of how to do it.

Add a Swift file to your project

  • Right click in the Project Navigator and choose "New File..."

  • Select "Swift File"

  • The convention is to save the file name as the class name you are extending plus (with a "+" sign) what you are doing to it. For example, "UIImage+Cropping".

Write the code for the extension

Open the new Swift file that you just created.

You should import UIKit (instead of Foundation) if you are extending a UIView. Then use the extension keyword before the class name that you want to extend. You can then add your own new methods to the class. (Note, extensions are for adding new methods, not overriding existing methods--hence the name.)

In the video, the example was to add a method that crops a circle from the image and gives it a border.

import UIKit

extension UIImage {

    func cropToCircleWithBorderColor(color: UIColor, lineWidth: CGFloat) -> UIImage {

        // code to create the cropped circle with colored border

        return newImage
    }
}

See here for the full example.

Use your extension anywhere in your project

Now you can use your new method for that class anywhere in your code, just like it was part of the standard class.

Here is the video's example (on github):

import UIKit
class ViewController: UIViewController {

    @IBOutlet var imageView : UIImageView = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        let img = imageView.image
        imageView.image = img.cropToCircleWithBorderColor(UIColor(red:0.91, green:0.34, blue:0.16, alpha:1.00), lineWidth: 20)
    }
}

The method cropToCircleWithBorderColor is not a standard part of UIImage, but as you can see, it is used just like it were.

For further study:

iSofTom

In Swift it's called Extensions ! Check it out

You can use following code in your existing class

extension name{
func funcName(){
   } 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!