#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”的更多相关文章

  1. xcode工程编译错误:No architectures to compile for

    问题 开发环境:xcode6,iPhone6模拟器 xcode工程编译错误:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active ...

  2. 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 ...

  3. xcode工程编译错误:一般错误总结

    1.Apple LLVM 8.0 Error Group /’all-product-headers.yaml’ not found 最近升级了xcode打包后出现了个BUG,记录解决的方法. 现象: ...

  4. xcode工程编译错误:missing required architecture i386 解决方法

    可能原因一:项目内保存了.framework文件,在复制分发到不同计算机的时候可能会引发该错误 解决方法一:来到Targets->Build Settings->Framework Sea ...

  5. Xcode工程编译错误之iOS开发之The Xcode build system has crashed. Please close and reopen your workspace

    解决方法: . 删除DerivedData . 参照上面的链接设置:File -> Workspace Settings -> Build System -> Legacy Buil ...

  6. xcode工程编译错误:error: Couldn’t materialize

    错误信息: error: Couldn't materialize: couldn't get the value of variable amount: variable not available ...

  7. 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.免费应用程序调试最 ...

  8. 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 ...

  9. xcode工程编译错误之iOS解决CUICatalog: Invalid asset name supplied问题

    [问题分析]: 这个问题其实是老问题,产生原因就是因为在使用的时候 [UIImage imageNamed:]时,图片不存在或者传入的图片名为nil. [解决方法]: 添加一个系统断点,来判断如果图片 ...

随机推荐

  1. [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)

    kube-dns原理 参考: 组件架构看这个就够了 http://cizixs.com/2017/04/11/kubernetes-intro-kube-dns 设置细节看这个就够了 http://b ...

  2. FAL_CLIENT和FAL_SERVER参数详解

    FAL_CLIENT和FAL_SERVER参数详解 转载:http://openwares.net/database/fal_client_fal_server.html FAL_CLIENT和FAL ...

  3. Java如何进行Base64的编码(Encode)与解码(Decode)?

    https://blog.csdn.net/zhou_kapenter/article/details/62890262 *************************************** ...

  4. 第三百九十七节,Django+Xadmin打造上线标准的在线教育平台—其他插件使用说,主题本地化设置

    第三百九十七节,Django+Xadmin打造上线标准的在线教育平台—其他插件使用说,主题本地化设置 主题设置是在xadmin\plugins\themes.py这个文件 默认xadmin是通过下面这 ...

  5. 【NPM】设置代理

    https://yutuo.net/archives/c161bd450b2eaf88.html npm config set proxy http://server:port npm config ...

  6. Linux权限详解 命令之 chmod:修改权限

    权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...

  7. [Tensorflow] RNN - 02. Movie Review Sentiment Prediction with LSTM

    From: Predicting Movie Review Sentiment with TensorFlow and TensorBoard Ref: http://www.cnblogs.com/ ...

  8. jar各个版本号的意义

    jar版本号的意义: Alpha: Alpha是内部测试版,一般不向外部发布,会有很多Bug.除非你也是测试人员,否则不建议使用.是希腊字母的第一位,表示最初级的版本,alpha 就是α,beta 就 ...

  9. 看看大网站都用什么操作系统和Web服务器

    以下内容为网络上转载总结,不是很准确 Google 用哪些软件做 Web Server? 除了有两个节点操作系统看出来是 Linux 外,其他的都是未知的. Web 服务器用的都是 GWS ? 我估计 ...

  10. iOS - 切换rootViewController时,销毁之前的控制器

    一.iOS在切换根控制器时,如何销毁之前的控制器?(切换rootViewController时注意的内存泄漏) 首先.在iOS的ARC机制下,任何对象,当没有其他对象对他进行强引用时,都会被自动释放. ...