热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

int类型的setter/getter的正确属性是什么?-Whatisthecorrectattributeforasetter/getterforanint?

Ihaveseenanumberofdifferentwaystopropertisesynthesizeaint,butIdontknowtheproperw

I have seen a number of different ways to propertise/synthesize a int, but I don't know the proper way.

我已经看到了许多不同的方法来正确地使用/合成int,但是我不知道正确的方法。

I usually do:

我通常做的事:

@property (nonatomic, assign) int myInt

But I have seen people use:

但是我看到人们用:

@property (nonatomic) int myInt
@property int myInt

Which way is correct?

哪条路是正确的?

4 个解决方案

#1


3  

Here is a great post about the difference between atomic and nonatomic:

这里有一篇关于原子和非原子的区别的文章:

Assuming that you are @synthesizing the method implementations, atomic vs. non-atomic changes the generated code. If you are writing your own setter/getters, atomic/nonatomic/retain/assign/copy are merely advisory.

假设您是@ synzing方法实现,原子vs非原子更改生成的代码。如果您正在编写自己的setter/getter,那么原子/非原子/保留/分配/复制仅仅是咨询。

With atomic, the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A.

对于原子,合成的setter/getter将确保所有值始终从getter中返回或由setter设置,而不考虑任何其他线程上的setter活动。也就是说,如果线程A位于getter的中间,而线程B调用setter,那么一个实际可行的值——最可能是一个autoreleated对象——将在A中返回给调用者。

In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than atomic.

在非原子中,没有这样的保证。因此,非原子比原子快得多。

What atomic does not do is make any guarantees about thread safety. If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned -- the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell.

原子不做的是保证线程安全。如果线程与线程同时调用getter B和C调用setter与不同价值观,线程A可能会返回的三个值中的任何一个,前一个被调用setter或者值传递到setter的B和C .同样,价值的对象可能会从B或C,无法告诉。

Ensuring data integrity -- one of the primary challenges of multi-threaded programming -- is achieved by other means.

确保数据完整性(多线程编程的主要挑战之一)是通过其他方式实现的。

Note that the default is atomic, so the last example is equivalent to

注意,默认值是原子的,所以最后一个示例等价于

@property (atomic) int myInt

Another default value is assign, therefore these two options are equivalent:

另一个默认值是assign,因此这两个选项是等价的:

@property (nonatomic, assign) int myInt
@property (nonatomic) int myInt

And, similarly, the following are also equivalent

同样,下面的内容也是等价的

@property (atomic, assign) int myInt
@property (atomic) int myInt

EDIT: As Josh points out, atomic being included is a hypothetical example.

编辑:正如Josh所指出的,原子被包含是一个假设的例子。

#2


0  

defaults are assign and atomic

默认值是赋值和原子的

#3


0  

It's more of a style issue than an issue of technical correctness. For an int type, the only thing that has to true the assign modifier (because you don't retain scalars). Otherwise, it's context specific: readwrite or readonly, atomic or nonatomic, it depends on the application.

与其说是技术正确性问题,不如说是风格问题。对于int类型,唯一需要对赋值修饰符进行赋值的东西(因为不保留标量)。否则,它是特定于上下文的:readwrite或readonly、atomic或nonatomic,这取决于应用程序。

Note that if you don't specify a modifier, the default applies (atomic, readwrite, assign). So your example your first other example are identical, and the third only differs in atomicity.

注意,如果没有指定修饰符,则默认应用(原子、读写、赋值)。你的例子你的第一个例子是相同的,第三个只是原子性不同。

#4


0  

@property(nonatomic,assign)int myVariable;

@ property(原子,分配)int myVariable;

always remember assign will be used for primitive data types like int , bool

永远记住,assign将用于像int、bool这样的原始数据类型


推荐阅读
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 如何提高PHP编程技能及推荐高级教程
    本文介绍了如何提高PHP编程技能的方法,推荐了一些高级教程。学习任何一种编程语言都需要长期的坚持和不懈的努力,本文提醒读者要有足够的耐心和时间投入。通过实践操作学习,可以更好地理解和掌握PHP语言的特异性,特别是单引号和双引号的用法。同时,本文也指出了只走马观花看整体而不深入学习的学习方式无法真正掌握这门语言,建议读者要从整体来考虑局部,培养大局观。最后,本文提醒读者完成一个像模像样的网站需要付出更多的努力和实践。 ... [详细]
  • 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
    本文旨在全面介绍Windows内存管理机制及C++内存分配实例中的内存映射文件。通过对内存映射文件的使用场合和与虚拟内存的区别进行解析,帮助读者更好地理解操作系统的内存管理机制。同时,本文还提供了相关章节的链接,方便读者深入学习Windows内存管理及C++内存分配实例的其他内容。 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • win10电脑蓝屏代码0x000000a5无法进入系统解决方法详解
    许多用户在使用电脑的时候遇到蓝屏问题,重启无法进入系统。本文提供了解决方法:调整BIOS设置、禁用安全启动、重装系统等。如果以上方法都无法解决问题,需要重新安装一个系统。详细步骤请参考正文内容。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
author-avatar
herogan
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有