0

I'm trying to pick up a bit of Swift lang and I'm wondering how to convert the following Objective-C into Swift:

- (void)refreshDataOnChange {
    if (![tblSkillMaterials.delegate isKindOfClass:[self class]]) {
        tblSkillMaterials.delegate = self;
    }
    if (![tblSkillMaterials.dataSource isKindOfClass:[self class]]) {
        tblSkillMaterials.dataSource = self;
    }
    //Other Code Logic Here

}

More specifically I need to know how to use isKindOfClass in the new syntax.

func refreshDataOnChange() {
    if tblSkillMaterials.delegate?.isKind(of: ?) {
        tblSkillMaterials.delegate = self
    }
    if tblSkillMaterials.dataSource?.isKind(of: ?) {
        tblSkillMaterials.dataSource = self
    }
    //Other Code Logic Here
}

Fetching Error Like:

Cannot convert value of type 'ViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')

Nishant Bhindi
  • 2,242
  • 8
  • 21
  • Sorry I don't know but No duplicate of Using isKindOfClass with Swift - I want to check which controller assign UITableView Delegate Object – Sudhir Chovatiya Jul 30 '18 at 07:33
  • 1
    What is the purpose of this code? There might be better native Swift solutions for example type constrained protocols – vadian Jul 30 '18 at 07:33

4 Answers4

1

Try the following code, it's working fine for me:

func refreshDataOnChange() {
   if tblSkillMaterials.delegate?.isKind(of: <your view controller name>.self) {
     tblSkillMaterials.delegate = self
   }
   if tblSkillMaterials.dataSource?.isKind(of: <your view controller name>.self) {
     tblSkillMaterials.dataSource = self
   }
   //Other Code Logic Here
 }
bestiosdeveloper
  • 2,339
  • 1
  • 11
  • 28
0

Try following code, I think it works fine:

if !tableView.dataSource?.isKind(of: self) {
    self.tableView.dataSource = self
}

if !tableView.delegate?.isKind(of: self) {
    self.tableView.delegate = self
}
Anup Kanjariya
  • 292
  • 1
  • 2
  • 15
0

Not sure it is a good way or not but may be help you.

func checkDelegate()
{

    if tblVIew.delegate != nil
    {
        if (tblVIew.delegate?.isEqual(self))!
        {
            print("self is  the delegate")
        }
        else{
            print(" else case")
        }
    }
    else{
         print(" delegate is nil")
    }

}
guru
  • 2,727
  • 3
  • 27
  • 39
0

Just Use these two lines instead of above lines

self.tblSkillMaterials.delegate = self
self.tblSkillMaterials.dataSource = self
Sujatha Girijala
  • 1,141
  • 8
  • 20
  • Thank you for answer but, - i want know why i am not checking which controller assign UITableView Delegate Object – Sudhir Chovatiya Jul 30 '18 at 07:44
  • I think this link will help you please check once https://stackoverflow.com/questions/34565570/conforming-to-uitableviewdelegate-and-uitableviewdatasource-in-swift – Sujatha Girijala Jul 30 '18 at 07:56