I want to use this open-source component:
https://github.com/CEWendel/SWTableViewCell
On iOS 7 ++ works fine, but on iOS 6 it crashes.
I need to know if there is any built-in macro that will allow me to do something like this:
#if __CURRENT_IPHONE_OS_VERSION >= __IPHONE_7_0
#import "SWTableViewCell.h"
@interface PPTimerCell : SWTableViewCell
#else
@interface PPTimerCell : UITableViewCell
#endif
Thanks in advance!
As noted in comments, #if
is a compile-time condition, not something that applies at run time. If you want to continue with your direction, you'll need a way to vary behavior at run time.
Also as noted in comments... is it really worth the extra development, maintenance, and support effort for this issue for the small number of users not on iOS 7.0 or later? Apple's latest usage statistics say that number is 4% (and most likely shrinking). You might have an easier time just cutting 6.0 support.
Note also that the swipe-for-actions functionality provided by SWTableViewCell
is built-in on iOS 8 . You could reduce your dependency on third-party code by just doing swipe actions on iOS 8 with the built-in API. (Not saying you should, just that it's worth considering how you spend your development/QA resources.)
The general case of varying a superclass at run time is hard — see further down if you must. But you're doing this for table cells — a table view can easily have the class of each cell set at run time . You could refactor your code so that you instantiate a UITableViewCell
subclass on one OS version and a SWTableViewCell
subclass on another — and instead of duplicating the code that'd be shared by those classes, put that code in a third class, an instance of which is referenced by each of the other two.
So you're looking for a general solution to the conditional inheritance problem? Well, that gets into dark places real fast.
If you were working with a class that'd be present (ie linked) on one version of iOS and not on another, the compiler's @compatiblity_alias
feature might help you. This declares one class name as a substitute for another — a couple of years ago it was handy for using third-party libraries like PSTCollectionView
as a backward-compatible replacement for UICollectionView
, but that was introduced in iOS 6 and nobody's seriously trying to go back to iOS 5 anymore. Anyhow, since @compatibility_alias
depends on symbol linkage, to use it in your case you'd need to make the linker bring in your library on iOS 7+ but not on iOS 6, and I don't think that can be done.
If you really want to create a class whose parentage can be different at run time, you'll have to create that class at run time using the ObjC runtime API. Call objc_allocateClassPair
to create the class. Beware that working with the ObjC runtime is not for the faint of heart, though there are some good writeups on it out there. Again, though, you probably don't want to do this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.