I am working to make an existing iPhone / iPad project backward compatible with iPhoneOS 3.0.
My current test device is the iPod Touch with 3.1.3 on it.
The following bit of code causes problems:
Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer"); if (gestureRecognizer != nil) { UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)]; leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; leftSwipeRecognizer.delegate = self; [self.view addGestureRecognizer:leftSwipeRecognizer]; _leftSwipeRecognizer = leftSwipeRecognizer; }
According to Apple's documentation, UIGestureRecognizer is defined starting with iOS 3.2. Therefore, I expect that the Class gestureReconizer will be nil in the previous version of the OS and that there will be the following for skipping: However, it does not skip. gestureRecognizer not nil , the code inside if starts execution and fails with leftSwipeRecognizer.direction , because:
-[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720
This situation is rather confusing. I think I'm doing everything according to the book. I am trying to check if a class exists before I use it, however a class that should not be there, fools my test, does not meet its expected specifications and crashes.
I could, of course, put a few respondsToSelector checks here and there to get around this crash, but that would not be an elegant way to do this.
Any other suggestions?
source share