开发者

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)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜