1

我的代码中有一个类(我们称之为 myRefLib),它继承自 UIReferenceLibraryViewController(UIKit 框架的一部分),这是一个仅在 iOS 5 及更高版本中可用的类。我想要一个 iOS 3.2 的部署目标。

如果我只是创建 UIReferenceLibraryViewController 的实例,我知道我可以使用 5 的基本 SDK,然后在运行包含它的任何代码之前使用 [UIReferenceLibraryViewController 类] 测试该类是否存在。但是,如果我是从类继承的,我该怎么做呢?

问题是我必须为将使用它的代码部分#include 继承类 myRefLib - 但没有办法在运行时有条件地这样做。或者,也没有运行时有条件地从 myRefLib 中的 UIReferenceLibraryViewController 继承的方法。我还想让 myRefLib 的一个实例成为另一个类的属性 - 同样,我怎样才能有条件地执行该运行时?

非常感谢任何帮助...

D

4

1 回答 1

1

Well, first of all, you have know what you're going to do when running on the earlier OS. Basically, you have to write your code as though you were building against the iOS 3.2 SDK. Only then can you add optional enhancements using the newer features.

Starting with the easiest thing: you don't need a declaration of a class interface in order to declare variables, including instance variables, as pointers to that class. You just need a forward declaration:

@class SomeClass;
SomeClass* foo;

Next, there should be no problem with importing a header which defines your class. That's a compile-time thing, which you seem to think is a problem, but which means it's not affected by the runtime environment. It will compile because you're building against the iOS 5.0 SDK.

The thing you have to be careful of is to not use the class if it's not available. You already know how to check that: test if [SomeClass class] returns Nil. If it does, don't instantiate your custom subclass.

Finally, you mention "runtime conditionally inheriting from UIReferenceLibraryViewController in myRefLib". Why do you think you need to do that? If UIReferenceLibraryViewController is not available, then it doesn't make much sense to want to use myRefLib which subclasses it. If you have some alternative implementation in mind that doesn't depend on UIReferenceLibraryViewController, then make a separate class. Then conditionally pick between myRefLib and this other class depending on availability.

于 2012-05-12T04:27:18.997 回答