Ryan
Ryan

Reputation: 28197

Converting Objective C to C# - What is the equivalent to this code?

I am converting some Objective C code to C# for use in a Monotouch iPhone app.

In Objective C, the following equivalence condition is tested:

if ([cell.backgroundView class] != [UIView class])
    ... do something

cell is a UITableViewCell.

In C#, I'd like to test the same condition using (so far) the following:

if ( !(cell.BackgroundView is UIView))
    ... do something

Is the understanding of the Objective C code correct, i.e. it tests the type of cell? What would the equivalent be in C#?

Upvotes: 9

Views: 3462

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243156

The correct way to test for type in Objective-C is like this:

if ([[cell backgroundView] isKindOfClass:[UIView class]]) {
  //the backgroundView is a UIView (or some subclass thereof)
}

If you want to test for explicit membership, you can do:

if ([[cell backgroundView] isMemberOfClass:[UIView class]]) {
  //the backgroundView is a UIView (and not a subclass thereof)
}

Upvotes: 2

Bala R
Bala R

Reputation: 108957

Looks right, unless UITableViewCell inherits from UIView.

in which case you'll need

if (cell.BackgroundView.GetType() !=  typeof(UIView))
    ... do something

Upvotes: 9

Related Questions