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#?
Looks right, unless UITableViewCell
inherits from UIView
.
in which case you'll need
if (cell.BackgroundView.GetType() != typeof(UIView))
... do something
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)
}
精彩评论