`
modabobo
  • 浏览: 509688 次
文章分类
社区版块
存档分类
最新评论

OC学习篇之---@property和@synthesize的使用

 
阅读更多

在之前一片文章我们介绍了OC中的内存管理:http://blog.csdn.net/jiangwei0910410003/article/details/41924683,今天我们来介绍两个关键字的使用:@property和@synthesize


一、@property关键字

这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题

下面来看一下他的使用方法:

//
//  Person.h
//  25_Property
//
//  Created by jiangwei on 14-10-12.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface User : NSObject{
    //NSString *_userName;
    //NSString *_passWord;
    //...
}

//第一步生成_userName属性
//第二步为_userName属性自动生成set/get方法

//property在生成的set方法中,有没有做引用的操作?
//set方法的三种方式:
//第一种方式:
//普通赋值
//一般对象类型的引用操作
//NSString对象类型的引用操作

//第一个位置
//atomic:线程保护的,默认
//nonatomic:线程不保护的

//第二个位置
//assign:直接赋值,默认
//retain:保留对象
//copy:拷贝对象

//第三个位置
//readwrite:生成get/set方法,默认
//readonly:只生成get方法
@property NSString *userName;

@end

还记得我们之前定义属性的时候,在{...}中进行定义,而且定义完之后还有可能需要实现get/set方法,这里我们直接使用@property关键字进行定义:

@property NSString *userName;
这样定义完之后,我们就可以使用这个属性了:

这样定义的方式之后,这个属性就会自动有set/get方法了

第一步生成_userName属性

第二步为_userName属性自动生成set/get方法

这样定义是不是比以前方便多了


下面再来看一下他还有三个值可以设置:

@property(atomic,retain,readwrite) Dog *dog;

1、第一个位置的值:

atomic:线程保护的,默认

nonatomic:线程不保护的

2、第二个位置的值:

assign:直接赋值,默认

retain:保留对象,内部会自动调用retain方法,引用计数+1

copy:拷贝对象

3、第三个位置的值:

readwrite:生成get/set方法,默认

readonly:只生成get方法


这里来做一个例子:

main.m

//
//  main.m
//  25_Property
//
//  Created by jiangwei on 14-10-12.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "User.h"
#import "Dog.h"

//当一个类中有很多个属性的时候,那么我们需要手动的编写他们的set/get方法
//这样就比较费时,所以这时候就可以使用@property
int main(int argc, const char * argv[]) {
    
    User *user = [[User alloc] init];
    Dog *dog = [[Dog alloc] init];
    NSLog(@"count:%ld",[dog retainCount]);
    
    [user setDog:dog];
    
    NSLog(@"count:%ld",[dog retainCount]);
    
    
    return 0;
}
运行结果:




二、@synthesize关键字

//
//  Person.m
//  25_Property
//
//  Created by jiangwei on 14-10-12.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "User.h"

//有时候我们不想定义属性为_开头的
//这时候我们就可以使用@synthesize,来修改我们想要的属性名

//这时候属性_userName变成了$userName

@implementation User
@synthesize userName = $userName;

@end
因为我们使用@property定义属性之后,如果我们想修改这个属性的名称,就可以使用@synthesize关键字来对属性名称进行修改

@synthesize userName = $userName;


总结

这一篇主要介绍了两个关键字的使用,@property和@synthesize,特别是@property关键字,后面定义属性的时候几乎就是用它了,非常方便







分享到:
评论

相关推荐

    Objective-C中的@property和@synthesize用法详解

    相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459

    Python库 | synthesize-0.0.1-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    iOS开发中属性 property 和 synthesize 详解

    针对iOS开发中属性 property 和 synthesize 进行了详细介绍

    Objective-c解析XML封装

    @property (nonatomic, retain) NSMutableString *currentResult; @property (nonatomic, retain) NSMutableDictionary *map; @property (nonatomic, retain) NSMutableArray *list; -(NSMutableDictionary *)...

    3D-Synthesize3DviaDepthOrSil.zip

    3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...

    Android代码-Synthesize

    Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation

    objective-c小技巧

    1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行...

    irrelon-synthesize:一个用于 JavaScript 模块的简单 getter setter 合成器

    自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...

    matlab脑部ct代码-synthesize-brain-mri-ct:CT和MR脑图像的基于模型的图像合成

    matlab脑部代码脑部MR和CT合成 这是用于脑部CT和MRI的基于模型的图像合成(翻译)的代码。 给定有效输入模态(MR-T1w,MR-T2w,MR-PDw,CT)的任何组合,将综合缺少的模态。 例如,如果对象仅进行了T1w扫描,则将...

    Objective-c对象组装XML

    @synthesize objectName; @synthesize lvUp; @synthesize root; @synthesize buildEnd; -(NSData *)objctPackage:(NSMutableDictionary *)object objectName:(NSString *)name xmlTemplateName:(NSString *)...

    传智博客-Objective-C PPT

    本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等

    Synthesize & Power Analyze

    Synthesize & Power Analyze

    varidict_handler

    变量处理程序一个ObjC助手来使用varidict处理程序。 一行代码值得一千个单词。 @interface Consumer/* * The type is void (^)() or void (^)(id self). */@property ( nonatomic , copy ) NDHandler handler;- ( ...

    Senfore_DragDrop_v4.1

    "Ignore All" when the IDE complains that this or that property doesn't exist and finally save the forms. ------------------------------------------- 3. Getting started: -----------------------------...

    react-native-hybrid-app-examples:在现有iOS应用程序中使用React Native的示例集合

    React Native Hybrid App示例在现有iOS应用程序中使用React Native的示例集合概念预装桥如果要使混合应用程序具有良好的性能,应该做的第一件事就是预先加载RCTBridge并将其引用保存在某个地方(可能是AppDelegate ...

    alu.zip_Cyclone_altera_de2_logic_logic synthesize

    Code to synthesize Arithmetic Logic Unit

    利用iOS绘制图片生成随机验证码示例代码

    @property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain) NSMutableString *changeString; @property (nonatomic, retain) UILabel *codeLabel; -(void)changeCode; @end .m文件 @...

    openrisc-HW-tutorial-Altera.pdf

    ‘CVSROOT’ should be cvs@cvs.opencores.org:/home/oc/cvs ??‘Authentication” needs to be “passwd file on the cvs server” ??disable “Checkout read-only” at the ‘Globals’ tab. I

    破解Objective-C面试:笑到最后的技术攻略!.zip

    C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category...property、NSOperation、NSOperationQueue、synthesize、动态绑定、SEL、协议继承、元类(meta...

Global site tag (gtag.js) - Google Analytics