iPhone Obj-C错误:在Xcode中分配只读变量'prop.149'

时间:2009-07-22 16:22:20

标签: iphone objective-c xcode variable-assignment

我正在使用Objective-C编写iPhone应用程序。

这是Xcode给我的错误:

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

代码:

// 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);

错误意味着什么,我该如何解决?

4 个答案:

答案 0 :(得分:7)

不确定原因,但编译器无法推断出三元运算符结果的计算类型。简单地添加一个显式的强制转换似乎有效:

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

我将此作为编译器错误提交。

答案 1 :(得分:0)

错误意味着您正在设置只读属性。看起来CATransition的subtype属性不是可设置的属性。

答案 2 :(得分:0)

我同意文档说您应该能够为子类型属性赋值。我想知道你的代码行是否存在分配子类型的问题。您是否因运营商优先权被烧毁?请尝试以下方法:

transition.subtype = flipsideView.hidden ? kCATransitionFromRight : kCATransitionFromLeft;

答案 3 :(得分:0)

此原始代码失败:

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

但这有效:

if (flipsideView.hidden == YES) {
    transition.subtype = kCATransitionFromRight;
} else {
    transition.subtype = kCATransitionFromLeft;
}