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系统命令缩写
命令缩写:(转) ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户 rpm:redhat package manager 红帽 ...
- 揭开Docker的神秘面纱
Docker 相信在飞速发展的今天已经越来越火,它已成为如今各大企业都争相使用的技术.那么Docker 是什么呢?为什么这么多人开始使用Docker? 本节课我们将一起解开Docker的神秘面纱. 本 ...
- 译: 5. RabbitMQ Spring AMQP 之 Topic 主题
在上一个教程中,我们提高了消息传递的灵活 我们使用direct交换而不是使用仅能够进行虚拟广播的fanout交换, 并且获得了基于路由key 有选择地接收消息的可能性. 虽然使用direct 交换改进 ...
- android studio: 一个Android studio 3.3.2 无法创建新项目的问题
记录一个AS无法创建新项目的问题. 今天想写一个测试Demo,点击上面的“Start a new Android Studio Project” ,填写完包名和项目路径后,点“Finish”, AS无 ...
- idea 导入 android项目
1. 2. 主要是勾选上面选项. next next 导入即可
- oracle 回收表空间的数据文件大小
查看表空间的使用情况: " "used MB",b.bytes "free MB", ,) "percent_used" from ...
- 1. RNN神经网络模型原理
1. RNN神经网络模型原理 2. RNN神经网络模型的不同结构 3. RNN神经网络-LSTM模型结构 1. 前言 循环神经网络(recurrent neural network)源自于1982年由 ...
- 自定义progressdialog,改善用户体验
自定义progressdialog,改善用户体验
- vector、map 内存释放
一.vector void TestVector() { cout << "begin create vector" << endl; int iSize ...
- 19迭代模式Iterator
一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之 一,它把对容器中包含的内部对象的访问委让给 外部类,使用Iterator(遍历)按顺序进行遍历 访问的设计模式. 二.不使用迭代模式 ...