Xcode工程编译错误:“Cannot assign to 'self' outside of a method in the init family”
#import <Foundation/Foundation.h> @interface EOCRectangle : NSObject<NSCoding>
@property (nonatomic , readonly , assign) float width;
@property (nonatomic , readonly , assign) float height;
-(id)initWithWidth:(float) width andHeight:(float) height;
@end #import "EOCRectangle.h"
/**
* 为对象提供必要信息以便其能完成工作的初始化方法叫做“全能初始化方法”
*/
@implementation EOCRectangle
-(id)initWithWidth:(float) width andHeight:(float) height
{
if ((self = [super init])){
_width = width;
_height = height;
}
return self;
}
/**
* 初始化设置默认的值
*/
//-(id)init
//{
// return [self initWithWidth:10.0 andHeight:10.0];
//}
/**
* 初始化抛出异常
*/
-(id)init{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Must use initWithWidth:(float) width andHeight:(float) height instead"
userInfo:nil]; }
/**
* 初始化NSCoding
*/
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super init])){
_width = [aDecoder decodeFloatForKey:@"width"];
_height = [aDecoder decodeFloatForKey:@"height"];
}
return self;
}
@end
但在我自己写的过程中,忘记将初始化方法名以 init 开头,导致错误:
Cannot assign to 'self' outside of a method in the init family
原因:在ARC有效时,只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他 为准则。
如果此时关闭ARC,会发现刚才的错误提示不见了:
如果将初始化方法名改为 - initialize,同样有错误提示,因为不符合上面的命名规则。
这样的命名规则是为了保证ARC开启时内存管理不出错,同时,init方法必须是实例方法,并且必须返回实例对象,这样要求的原因同上。
Xcode工程编译错误:“Cannot assign to 'self' outside of a method in the init family”的更多相关文章
- xcode工程编译错误:No architectures to compile for
问题 开发环境:xcode6,iPhone6模拟器 xcode工程编译错误:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active ...
- Xcode工程编译错误之iOS开发之Xcode9报错 Compiling IB documents for earlier than iOS7 is no longer supported.
概要: 在我们升级到Xcode9时,最低的编译版本为iOS8,但是在使用一些SDK的时候就会报出Compiling IB documents for earlier than iOS7 is no l ...
- xcode工程编译错误:一般错误总结
1.Apple LLVM 8.0 Error Group /’all-product-headers.yaml’ not found 最近升级了xcode打包后出现了个BUG,记录解决的方法. 现象: ...
- xcode工程编译错误:missing required architecture i386 解决方法
可能原因一:项目内保存了.framework文件,在复制分发到不同计算机的时候可能会引发该错误 解决方法一:来到Targets->Build Settings->Framework Sea ...
- Xcode工程编译错误之iOS开发之The Xcode build system has crashed. Please close and reopen your workspace
解决方法: . 删除DerivedData . 参照上面的链接设置:File -> Workspace Settings -> Build System -> Legacy Buil ...
- xcode工程编译错误:error: Couldn’t materialize
错误信息: error: Couldn't materialize: couldn't get the value of variable amount: variable not available ...
- xcode工程编译错误:The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- Xcode工程编译错误之iOS开发之Sending '__strong typeof (xxx)' (aka 'xxxx *__strong') to parameter of incompatible type 'id<xxx>'
iphone开发出现警告: Sending '__strong typeof (xxx)' (aka 'xxxx *__strong') to parameter of incompatible ty ...
- xcode工程编译错误之iOS解决CUICatalog: Invalid asset name supplied问题
[问题分析]: 这个问题其实是老问题,产生原因就是因为在使用的时候 [UIImage imageNamed:]时,图片不存在或者传入的图片名为nil. [解决方法]: 添加一个系统断点,来判断如果图片 ...
随机推荐
- 每天一个linux命令(8):scp使用
不同的Linux之间copy文件常用有3种方法:1.使用ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy.2.采用samba服 ...
- 浅析tornado 中demo的 blog模块
#!/usr/bin/env python # # Copyright 2009 Facebook # # Licensed under the Apache License, Version 2.0 ...
- SILK 预测模块分析
SILK是一种新结构的基于噪声整形量化算法的编解码框架.不同于类CELP的AMR,EVRC,G729,Speex等标准. 类CELP的结构都是以码本激励为量化框架的编码器.但是这里并不讨论NSQ结构和 ...
- 推荐几个Windows工具软件: Stickies - 桌面贴
主页: http://www.zhornsoftware.co.uk/stickies/index.html Stickies work like Post-it notes for your PC. ...
- zookeeper 入门(二)
上一篇教程中重点讲解了如何部署启动一台zookeeper服务 本章中我们会重点讲解下如何 部署一套zookeeper的集群环境 基于paxos 算法,部署一套集群环境要求 至少 要有3个节点 并且节 ...
- 让ubuntu下的eclipse支持GBK编码
把Windows下工程导入Linux下Eclipse中,由于以前的工程代码,都是GBK编码,而Ubuntu默认不支持GBK编码,所以,我们要让Ubuntu支持GBK,方法如下: 1.修改/var/li ...
- mongodb查询数据库中某个字段中的值包含某个字符串的方法
正则表达式最能解决: 例如: db.getCollection(.*$/}) 这里主要是注意正则表达式要写对,该转义的注意转义,否则报错.
- IE11不能使用F12功能DOM资源管理器无法使用
https://www.ludou.org/win7-ie-11-f12-bug.html 32位win7下载补丁:http://www.microsoft.com/zh-CN/download/de ...
- flume 1.8 安装部署
环境 centos:7.2 JDK:1.8 Flume:1.8 一.Flume 安装 1) 下载 wget http://mirrors.tuna.tsinghua.edu.cn/apa ...
- [UI] 02 - Layout & CSS3
开胃小菜 一.纯CSS布局 UI效果 From: 创建一个没有表格的网页 代码详情 <!DOCTYPE html> <html> <head> <meta c ...