关于self.view.frame.size.width赋值会出现expression is not assignable 错误
来源:互联网 发布:硬件兼容性检测软件 编辑:程序博客网 时间:2024/06/09 02:07
下面的资料来自于:http://segmentfault.com/q/1010000000177216
self.view.frame.size.height = 100f;
这样写没法通过编译,编译器会报错"expression is not assignable"
原因是,这句话里面的几个点有两种不同的含义。self.view.frame
是Objective-C语法,是读取view属性的frame属性,在Objective-C中使用点来访问属性只是一种语法糖,所以self.view.frame
这句话会被转换成:
[[self view] frame]
也就是说,实际上这是消息传递。
而frame属性是一个CGRect结构,所以frame.size.height
是C语言的语法,就是访问CGRect结构中的size字段,同样,height是CGSize结构的一个字段。所以,你这句话实际上等于:
[[self view] frame].size.height = 100f;
而Objective-C只是对C语言的一个扩展,所以,上面这句话会被转成C语言的函数调用形式,类似于这种形式:
getframe().size.height = 100f;
而在C语言里,函数的返回值是一个R-Value,是不能直接给它赋值的(所谓的R-Value,就是只能出现在等号的右边,你可以理解成是一个常量;而可以被赋值的是L-Value,可以出现在等号的左边,通常是变量)。因此,当你打算直接给函数的返回值赋值的时候,编译器告诉你"这个表达式无法被赋值"。这就是这个错误的出现原因。
所以,解决办法就是,用一个临时变量保存这个函数的返回值,修改这个临时变量,然后再赋给frame:
// 1. 用一个临时变量保存返回值。CGRect temp = self.view.frame;// 2. 给这个变量赋值。因为变量都是L-Value,可以被赋值temp.size.height = 100f;// 3. 修改frame的值self.view.frame = temp;
0 0
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- .frame.size.width赋值会出现expression is not assignable 错误
- Xcode提示“expression is not assignable” self.view.frame.size.height = 100f无法赋值
- 更改frame 时报"expression is not assignable""这个表达式无法被赋值"
- expression is not assignable
- Expression is not assignable
- Expression is not assignable
- self.view.frame.size.height赋值问题
- iOS Xcode 编译错误 “expression is not assignable”
- iOS开发初级错误 Expression is not assignable
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- IOS使用系统默认声音、震动
- PADS Layout 交互布线 无模命令
- 比较两个字符串的大小
- vim 文件操作命令
- Transport (VMDB) error -44: Message. The VMware Authorization Service is not use
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- C++中const的用法
- Win7下的django+python+mysql的安装
- 基于WF4.0流程平台开发过程中的知识积累(三)书签实现子流程
- python 断点跟踪
- 写出我人生
- 将大写字母转换成小写字母
- python *,**
- iOS获取系统键盘的高度