Search code examples
swiftextension-methods

how to call function in Extension in swift?


I'm making an app but I'm confused about how to call the function in the extension file.

This is the code that I'm trying to call the function from the extension.

import UIKit

class RecipeTableViewCell: UITableViewCell {

    @IBOutlet weak var commnetLabel: UILabel!
    @IBOutlet weak var recipeImageView: UIImageView!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var recipeTypeLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    func configureView(recipe: RecipeData){
    
        commnetLabel.text = recipe.comment
        recipeImageView.setImage(imageUrl: recipe.imageUrl)
    //  dateLabel.text = recipe.recordedAt => get something like 2020-10-01 11:08:42

        dateLabel.text = setTemplate(strDate: recipe.recordedAt)
        recipeTypeLabel.text = recipe.recipeType

    }

}

In the configureView function, I used to write dateLabel.text = recipe.recordedAtto show the date to the app. I'm now trying to change how it is displayed, so I created an extension.

import Foundation

extension DateFormatter {

    func setTemplate (strDate: String) -> String {
    
    
        let dateFormatter = DateFormatter()
    
        dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ydMMM", options: 0, locale: Locale(identifier: "ja_JP"))
    
        let result = dateFormatter.string(from: Date())
    
        return result
    }
}

I thought writing dateLabel.text = setTemplate(strDate: recipe.recordedAt) in configureView function enables to call setTemplate function and change my dateLabel's text. However, currently I'm getting

Use of unresolved identifier 'setTemplate'

and I'm confused about why and how to access to setTemplate function.

Could anyone tell me why I'm getting the error and what I'm doing wrong?


Solution

  • You probably meant to extend RecipeTableViewCell, not DateFormatter.

    extension RecipeTableViewCell {
        func setTemplate(strDate: String) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ydMMM", options: 0, locale: Locale(identifier: "ja_JP"))
            let result = dateFormatter.string(from: Date())
        
            return result
        }
    }
    

    If you extend DateFormatter, the function setTemplate is added to the class DateFormatter -- you can't access it from RecipeTableViewCell. But if you really wanted to extend DateFormatter, you could do it like this:

    extension DateFormatter {
        func setTemplate(strDate: String) -> String {
            self.dateFormat = DateFormatter.dateFormat(fromTemplate: "ydMMM", options: 0, locale: Locale(identifier: "ja_JP"))
            let result = self.string(from: Date())
    
            return result
        }
    }
    
    /// usage:
    let formatter = DateFormatter()
    dateLabel.text = formatter.setTemplate(strDate: recipe.recordedAt)
    

    Extending DateFormatter directly lets you access setTemplate from anywhere (not just RecipeTableViewCell), so you might want to do this.