iOS中你可能没有完全弄清楚的(一)synthesize
1. 什么是synthesize
synthesize
中文意思是合成,代码中我们经常这样用。
@interface Test: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end@implementation Test
@synthesize i;
@end
复制代码
使用synthesize
的2个步骤:
- 首先你要有在类声明中使用
property
声明的属性。 - 第二在类实现中,写出
@synthesize
+ 变量名。
2. synthesize有什么作用?
平时在使用中,在类声明中添加了property
后,根本不需要在实现中添加@synthesize
。
因为OC
为property
属性声明添加了自动合成,也就是说系统自动帮你添加了@synthesize
。
所以,synthesize
是property
属性的一部分,它是系统为property
生成变量的重要步骤。
synthesize
具体做了些什么呢?它只做2件事:
- 生成成员变量,如上面的
Test
类就会生成一个名字为_i
的int
型变量。 - 为属性生成
setter/getter
方法,如上面的Test类会生成setI:
和i
两个方法。
3. synthesize什么情况下会用?
正常情况下,你不需要使用的synthesize
,因为大多数情况下,系统都会为你自动合成。
但是,你必须能清楚,系统自动合成有时候也是会失效的。这时候就需要你手动添加 synthesize
。
这些情况大约有3种:
- 修改生成的成员变量名字。
- 手动添加了
setter/getter
方法。 - 实现了带有
property
属性的protocol
。
3.1 修改生成的成员变量名字
@interface Test: NSObject
@property (nonatomic, readonly, unsafe_unretained) int i;
@end@implementation Test@synthesize i = shuaiI;//@synthesize i; //如果只写这个相当于 @synthesize i = i;-(void) print{//NSLog(@" print test i = %d", _i); //这个_i好土,换个帅气的名字。NSLog(@" print test i = %d", shuaiI); //帅气的名字就是shuaiI。
}
@end
复制代码
3.2 手动添加了setter/getter方法
如果你的属性是只读属性,但是你重写了getter
方法,系统不会为你自动生成成员变量。你需要添加@synthesize
。
@interface Test: NSObject
@property (nonatomic, readonly, unsafe_unretained) int i;
@end@implementation Test
-(int)i{return _i;
}@synthesize i = _i;//不加这个会报错-(void) print{NSLog(@" print test i = %d", _i);
}@end
复制代码
如果你的属性可读可写,但是你同时重写了setter/getter
方法,系统不会为你自动生成成员变量。你需要添加@synthesize
。这种情况下,你如果只重写了setter/getter
其中一个,系统仍然会执行自动合成。
@interface Test: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end@implementation Test-(int)i{return _i;
}-(void) setI:(int)i{_i = i;
}@synthesize i = _i;//不加这个会报错-(void) print{NSLog(@" print test i = %d", _i);
}@end
复制代码
3.3 实现了带有property属性的protocol
@protocol TestProtocol<NSObject>
@property (nonatomic, unsafe_unretained) int i;
@end@interface Test: NSObject<TestProtocol>
@end@implementation Test@synthesize i = _i;//不加这个会报错-(void) print{NSLog(@" print test i = %d", _i);
}@end
复制代码
4. 其他不会自动合成的情况
还有些情况,系统不会为属性自动合成变量和setter/getter方法,但是你也不需要手动添加@synthesize。
这些情况有:
- 使用了
@dynamic
。 - 在
Category
中添加的property
。 - 子类覆盖了父类的同名属性。
4.1 什么是@dynamic
@dynamic
使用场合同 @synthesize
,它的作用和@synthesize
相反,它告诉系统,不要为property
声明的属性添加成员变量。会有其他地方添加的。
所以用到@dynamic
的地方很少,那么在什么情况下会使用到呢?
能想到的大概只有1种:动态生成类和变量的情景中。如动态model生成。
4.2 Category中声明property
Category
中直接声明property
是没有意义的,相当于没写。
所以你在Category
中即使写了property
,也要手动添加setter/getter
方法。
property
就是为了简写setter/getter
方法,你都手动写了,也就没必要加property
了。
但是你仍然可以写property
。
4.3 子类覆盖父类同名属性
@interface Super: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end@implementation Super
@end@interface Child: Super
@property (nonatomic, unsafe_unretained) int i;
@end@implementation Child-(int)i{return 1;
}+(void) test{IMP superSetterMethod = class_getMethodImplementation(Super.class, @selector(setI:));IMP childSetterMethod = class_getMethodImplementation(Child.class, @selector(setI:));NSLog(@" setter is Equal = %d", superSetterMethod == childSetterMethod);IMP superGetterMethod = class_getMethodImplementation(Super.class, @selector(i));IMP childGetterMethod = class_getMethodImplementation(Child.class, @selector(i));NSLog(@" getter is Equal = %d", superGetterMethod == childGetterMethod);
}
@end
复制代码
上面代码写出来,xcode就会有提示,告诉你要为Child的i属性添加@synthesize。
而执行[Child test]的输出结果为:
1
0
复制代码
说明Child里虽然写了property,但是并未生成setter/getter方法。
另外需要注意的是,子类同名属性如果和父类的属性类型不同。则可能会崩溃。所以不要这样写,换个变量名字好了。(完)
相关文章:
framer x使用教程_如何使用Framer Motion将交互式动画和页面过渡添加到Next.js Web应用程序
framer x使用教程The web is vast and its full of static websites and apps. But just because those apps are static, it doesnt mean they have to be boring. 网络非常庞大,到处都是静态的网站和应用。 但是,仅仅因为这些应用程序是静态的…

POJ 2429
思路:a/n*b/nlcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况。套Miller_Rabin和pollard_rho模板 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c交2 #include<cstdio>3 #include<cstring>4 #include<cstdlib…

iOS中你可能没有完全弄清楚的(二)自己实现一个KVO源码及解析
前几天写了一篇blog(点这里),分析了系统KVO可能的实现方式。并添加了简单代码验证。 既然系统KVO不好用,我们完全可以根据之前的思路,再造一个可以在项目中使用的KVO的轮子。 代码已经上传到github: https://github.…

js中的preventDefault与stopPropagation详解
1. preventDefault: 比如<a href"http://www.baidu.com">百度</a>,这是html中最基础的东西,起的作用就是点击百度链接到http://www.baidu.com,这是属于<a>标签的默认行为;preventDefault方法就是可以阻止它的默认行为的发生而发生其他…

angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器
angular过滤字符介绍 (Introduction) In this article, we will create an optical character recognition (OCR) application using Angular and the Azure Computer Vision Cognitive Service. 在本文中,我们将使用Angular和Azure计算机视觉认知服务创建一个光学字…

javascript函数全解
0.0 概述 本文总结了js中函数相关的大部分用法,对函数用法不是特别清晰的同学可以了解一下。 1.0 简介 同其他语言不同的是,js中的函数有2种含义。 普通函数:同其他语言的函数一样,是用于封装语句块,执行多行语句的…
MYSQL explain详解[转载]
explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。 虽然这篇文章我写的很长,但看起来真的不会困啊,真的都是干货啊!!!! 先解析一条sql语句&…

CodeForces 157A Game Outcome
A. Game Outcometime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSherlock Holmes and Dr. Watson played some game on a checkered board n n in size. During the game they put numbers on the boards squares…

我使用Python和Django在自己的网站上建立了一个会员专区。 这是我学到的东西。
I decided it was time to upgrade my personal website in order to allow visitors to buy and access my courses through a new portal. 我认为是时候升级我的个人网站了,以允许访问者通过新的门户购买和访问我的课程 。 Specifically, I wanted a place for v…

详解AFNetworking的HTTPS模块
0.0 简述 文章内容包括: AFNetworking简介ATS和HTTPS介绍AF中的证书验证介绍如何创建服务端和客户端自签名证书如何创建简单的https服务器对CA正式证书和自签名证书的各种情况进行代码验证 文中所涉及的文件和脚本代码请看这里。 1.0 AFNetworking简介 AFNetwo…

字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser] 自动存donser到map并且值加一,如果发现重复元素不新建直接加一, map第一个参数是key&…

【洛谷P1508】吃吃吃
题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢;挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处…
前端和后端开发人员比例_前端开发人员vs后端开发人员–实践中的定义和含义
前端和后端开发人员比例Websites and applications are complex! Buttons and images are just the tip of the iceberg. With this kind of complexity, you need people to manage it, but which parts are the front end developers and back end developers responsible fo…

Linux 创建子进程执行任务
Linux 操作系统紧紧依赖进程创建来满足用户的需求。例如,只要用户输入一条命令,shell 进程就创建一个新进程,新进程运行 shell 的另一个拷贝并执行用户输入的命令。Linux 系统中通过 fork/vfork 系统调用来创建新进程。本文将介绍如何使用 fo…

metasploit-smb扫描获取系统信息
1.msfconsle 2.use auxiliary/scanner/smb/smb_version 3. msf auxiliary(smb_version) > set RHOSTS 172.16.62.1-200RHOSTS > 172.16.62.1-200msf auxiliary(smb_version) > set THREADS 100THREADS > 100msf auxiliary(smb_version) > run 4.扫描结果&#x…

算法(1)斐波那契数列
1.0 问题描述 实现斐波那契数列,求第N项的值 2.0 问题分析 斐波那契数列最简单的方法是使用递归,递归和查表法同时使用,可以降低复杂度。根据数列特点,同时进行计算的数值其实只有3个,所以可以使用3个变量循环递进计…

主键SQL教程–如何在数据库中定义主键
Every great story starts with an identity crisis. Luke, the great Jedi Master, begins unsure - "Who am I?" - and how could I be anyone important? It takes Yoda, the one with the Force, to teach him how to harness his powers.每个伟大的故事都始于…

算法(2)KMP算法
1.0 问题描述 实现KMP算法查找字符串。 2.0 问题分析 “KMP算法”是对字符串查找“简单算法”的优化。字符串查找“简单算法”是源字符串每个字符分别使用匹配串进行匹配,一旦失配,模式串下标归0,源字符串下标加1。可以很容易计算字符串查…

告别无止境的增删改查:Java代码生成器
对于一个比较大的业务系统,我们总是无止境的增加,删除,修改,粘贴,复制,想想总让人产生一种抗拒的心里。那有什么办法可以在正常的开发进度下自动生成一些类,配置文件,或者接口呢&…

Maven国内源设置 - OSChina国内源失效了,别更新了
Maven国内源设置 - OSChina国内源失效了,别更新了 原文:http://blog.csdn.net/chwshuang/article/details/52198932 最近在写一个Spring4.x SpringMVCMybatis零配置的文章,使用的源配的是公司的私有仓库,但是为了让其他人能够通过…
如何使用Next.js创建动态的Rick and Morty Wiki Web App
Building web apps with dynamic APIs and server side rendering are a way to give people a great experience both with content and speed. How can we use Next.js to easily build those apps?使用动态API和服务器端渲染来构建Web应用程序是一种使人们在内容和速度上都…

安装部署Spark 1.x Standalone模式集群
Configuration spark-env.sh HADOOP_CONF_DIR/opt/data02/hadoop-2.6.0-cdh5.4.0/etc/hadoop JAVA_HOME/opt/modules/jdk1.7.0_67 SCALA_HOME/opt/modules/scala-2.10.4 ####################################################### #主节点 …

算法(3)简单四则运算
1.0 问题描述 实现10以内四则运算(只包含数字,*/和小括号) 2.0 问题分析 四则运算使用“后缀表达式”算法来计算,后缀表达式可以无需考虑运算符优先级,直接从左至右依次计算。问题分解成2部分,一是将“中…

调用短信接口,先var_dump()看数据类型是object需要json_decode(json_encode( $resp),true)转换成array...
返回的数据.先看类型,如果是object类型 先json_encode, 再json_decode,加true 转换成数组 $resp $c->execute($req); var_dump($resp); object(stdClass)#12 (2) { ["result"]> object(stdClass)#13 (3) { ["err_code"]> string(1) "0"…

nlp文本数据增强_如何使用Texthero为您的NLP项目准备基于文本的数据集
nlp文本数据增强Natural Language Processing (NLP) is one of the most important fields of study and research in today’s world. It has many applications in the business sector such as chatbots, sentiment analysis, and document classification.Preprocessing an…

R语言-基础解析
二、操作基础%%取余%/%整数除法(1)eigen(...)求解方阵的特征值和特征向量(2)solve(D,A)求解DXA(3)data<-list(...)取里面的对象data[["列名称"]];data[[下标]];data$列名称(4)unlist(列表对象)把列表对象转化为向量对象(5)names(数据框)读取…

算法(4)数据结构:堆
1.0 问题描述 实现数据结构:堆。 2.0 问题分析 堆一般使用数组来表示,其中某个节点下标i的两个子节点的下标为 2i1 和 2i2。堆是一棵完全二叉树。堆有3种基本操作:创建,插入,删除。这3种操作都需要通过“调整堆”的…

cookie 和session 的区别详解
转自 https://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 这些都是基础知识,不过有必要做深入了解。先简单介绍一下。 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会…

如何设置Java Spring Boot JWT授权和认证
In the past month, I had a chance to implement JWT auth for a side project. I have previously worked with JWT in Ruby on Rails, but this was my first time in Spring. 在过去的一个月中,我有机会为辅助项目实现JWT auth。 我以前曾在Ruby on Rails中使用…

算法(5)哈希表
1.0 问题描述 实现数据结构:哈希表。 2.0 问题分析 哈希表可以看作我们经常使用的字典(swift)或对象(js),可以让一个key&value对一一对应,可以快速根据key找到value。哈希表内部使用数组…