views:

561

answers:

4

I'm programming an iPhone app using Objective-C.

Here's the error Xcode gives me:

error: assignment of read-only variable 'prop.149'

The code:

// Create CATransition object
CATransition *transition = [CATransition animation];
// Animate over 3/4 of a second
transition.duration = 0.75;
// using the ease in/out timing function
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Set transition type
transition.type = kCATransitionPush;
// Next line gives error: assignment of read-only variable 'prop.149'
transition.subtype = (flipsideView.hidden == YES ? kCATransitionFromRight : kCATransitionFromLeft);

What does the error mean, and how do I fix it?

A: 

The error means you're setting a property that is read-only. It looks like the subtype property of CATransition is not a settable property.

Dan Lorenc
But the property shouldn't be read-only. Here's the documentation:subtypeSpecifies an optional subtype that indicates the direction for the predefined motion-based transitions.@property(copy) NSString *subtypeThe ViewTransitions Sample Project (from Apple) has this line, which works fine: transition.subtype = subtypes[random() % 4];
Elliot
A: 

I agree that the documentation says you should be able to assign a value to the subtype property. I wonder if there is an issue with your line of code that is assigning the subtype. Are you getting burned by operator precedence? Try the following:

transition.subtype = flipsideView.hidden ? kCATransitionFromRight : kCATransitionFromLeft;
Jonathan Arbogast
Using the line you posted results in the same error.
Elliot
A: 

This original code fails:

transition.subtype = (flipsideView.hidden == YES ? kCATransitionFromRight : kCATransitionFromLeft);

But this works:

if (flipsideView.hidden == YES) {
 transition.subtype = kCATransitionFromRight;
} else {
 transition.subtype = kCATransitionFromLeft;
}
Elliot
+1  A: 

Not sure exactly why, but the compiler is not able to deduce the evaluated type of the result of the ternary operator. Simply adding an explicit cast seems to work:

transition.subtype = (NSString *)(flipsideView.hidden == YES ? kCATransitionFromRight : kCATransitionFromLeft);

I'd file this as a compiler bug.

cdespinosa
VERY interesting - thanks.
Elliot