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. [解决方法]: 添加一个系统断点,来判断如果图片 ...
随机推荐
- 用pigz代替gzip -- 并行压缩软件
用pigz代替gzip By yejr on 03 十二月 2012 pig是个啥东东?官网:http://zlib.net/pigz一句话简介: A parallel implementation ...
- IOS开发 文件路径
1.开发平台路径: /Developer/Platforms 此路径下一般有三个目录,分别是mac电脑.模拟器.iphone真机 MacOSX.platform iPhoneSimulator.pla ...
- Android中获取应用程序(包)的信息-----PackageManager的使用
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagename ...
- Android 测试入门之---Monkey test
这周重点学习的也是Android monkey test 的一些相关知识,也对其进行了初步的操作和试验.讲学习资料整理如下 : Monkey是一个命令行工具 ,可以运行在模拟器里或实际设备中.它向系统 ...
- Java知多少(14)数组
如果希望保存一组有相同类型的数据,可以使用数组. 数组的定义和内存分配 Java 中定义数组的语法有两种: type arrayName[]; type[] arrayName; type 为Java ...
- Path expected for join!错误处理
在Hibernate中,进行连接查询时,如果使用join语句,就容易产生标题所示的错误. 比如from Content o left join Mcp mcp where o.mcp.id=mcp.i ...
- linux选择sdb sdb4 fat32 还是sda分区
fat32是怎么混到它们中的sda,sdb,sdc是你的第一块,第二块,第三块硬盘sda1,sda2,sda5是你第一块硬盘中的第一块分区,2块,5块分区fat32,ext2,ext3,ext4是你的 ...
- Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页
1.安装selenium pip install selenium Collecting selenium Downloading selenium-3.4.1-py2.py3-none-any.wh ...
- Mac获取公网ip
终端输入 curl http://members.3322.org/dyndns/getip way 2: ➜ ~ wget http://ipecho.net/plain -O - -q ; ech ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V3 -- 远程方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V3——RPC -- 远程方法调用 及 null的传输 + Spring 服务提供商: 1. 配置 rpc03_server.xml 注入 服务提供 ...