Xcode提示“expression is not assignable”
来源:互联网 发布:知乎 电动牙刷 编辑:程序博客网 时间:2024/06/05 00:58
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;
我知道这样写看起来有点笨,也许有一天,Objective-C的编译器会变的智能一点,自动完成这种转换。
转:http://segmentfault.com/q/1010000000177216 0 0
- 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”
- Xcode提示“expression is not assignable”
- Xcode提示“expression is not assignable”
- expression is not assignable
- Expression is not assignable
- Expression is not assignable
- Xcode提示“expression is not assignable” self.view.frame.size.height = 100f无法赋值
- iOS Xcode 编译错误 “expression is not assignable”
- iOS开发初级错误 Expression is not assignable
- is not assignable to
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- 关于self.view.frame.size.width赋值会出现expression is not assignable 错误
- 更改frame 时报"expression is not assignable""这个表达式无法被赋值"
- 转: 一致性hash算法 - consistent hashing
- 类的sizeof
- Referenced file contains errors
- 你想手机内存突破100G吗,你想直接在手机上播放硬盘上的视频吗?
- QTP 之 下拉列表/单选框/订单链接的参数化
- Xcode提示“expression is not assignable”
- tableView
- 2013年工作总结
- win7 + IIS7.0 HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面
- iOS开发之工具篇-20个可以帮你简化移动app开发流程的工具
- SHAREPOINT - CAML列表查询
- OpenCV中GPU模块使用
- 利用CSS让元素垂直居中的两种实现方法
- Apache转发配置