1. #import <Foundation/Foundation.h>
  2.  
  3. @interface EOCRectangle : NSObject<NSCoding>
  4. @property (nonatomic , readonly , assign) float width;
  5. @property (nonatomic , readonly , assign) float height;
  6. -(id)initWithWidth:(float) width andHeight:(float) height;
  7. @end
  8.  
  9. #import "EOCRectangle.h"
  10. /**
  11. * 为对象提供必要信息以便其能完成工作的初始化方法叫做“全能初始化方法”
  12. */
  13. @implementation EOCRectangle
  14. -(id)initWithWidth:(float) width andHeight:(float) height
  15. {
  16. if ((self = [super init])){
  17. _width = width;
  18. _height = height;
  19. }
  20. return self;
  21. }
  22. /**
  23. * 初始化设置默认的值
  24. */
  25. //-(id)init
  26. //{
  27. // return [self initWithWidth:10.0 andHeight:10.0];
  28. //}
  29. /**
  30. * 初始化抛出异常
  31. */
  32. -(id)init{
  33. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  34. reason:@"Must use initWithWidth:(float) width andHeight:(float) height instead"
  35. userInfo:nil];
  36.  
  37. }
  38. /**
  39. * 初始化NSCoding
  40. */
  41. -(id)initWithCoder:(NSCoder *)aDecoder{
  42. if ((self = [super init])){
  43. _width = [aDecoder decodeFloatForKey:@"width"];
  44. _height = [aDecoder decodeFloatForKey:@"height"];
  45. }
  46. return self;
  47. }
  48. @end

但在我自己写的过程中,忘记将初始化方法名以 init 开头,导致错误:

  1. 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. django 拷贝一个 model 实例

    今天做一个拷贝功能,把某个 obj 拷贝并修改部分数据,提交表单后保存为一个新实例.结果google 出来的结果不对,都是相互copy 的代码,大概如下: obj = MyModel.objects. ...

  2. iOS系统及客户端软件测试的基础介绍

    iOS系统及客户端软件测试的基础介绍 iOS现在的最新版本iOS5是10月12号推出,当前版本是4.3.5 先是硬件部分,采用iOS系统的是iPad,iPhone,iTouch这三种设备,其中iPho ...

  3. Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

    /** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 ...

  4. Envoy 代替nginx https://www.jianshu.com/p/0a1f67b42fdb

    官方文档: https://www.envoyproxy.io/docs1.6.0版官方文档: https://www.envoyproxy.io/docs/envoy/v1.6.0/ 一. 编译和安 ...

  5. rabbitmq启动异常table_attributes_mismatch

    rabbitmq启动异常table_attributes_mismatch 2017年01月09日 16:57:50 growithus 阅读数:18     [root@localhost rabb ...

  6. Java知多少(15)字符串

    从表面上看,字符串就是双引号之间的数据,例如“微学苑”.“http://www.weixueyuan.net”等.在Java中,可以使用下面的方法定义字符串:    String stringName ...

  7. WAS生成的文件:javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc

    WAS生成的常见文件有哪些? 原文链接:http://blog.csdn.net/pqh20085101092/article/details/39370389 javacore.***.txt : ...

  8. 说说自己对RESTful API的理解

    REST不是英文上的rest单词,其英文缩写为presentational State Transfer ,直译为表现状态转移,咋看起来很学术,不懂,其实不用去死抠这个词的意思.REST是一种约束和架 ...

  9. Spring注解开发之Spring常用注解

    https://blog.csdn.net/Adrian_Dai/article/details/80287557主要的注解使用: 本文用的Spring源码是4.3.16@Configuration ...

  10. Spark学习笔记——键值对操作

    键值对 RDD是 Spark 中许多操作所需要的常见数据类型 键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式. Spark 为包 ...