`

objective-c中property的属性

    博客分类:
  • IOS
阅读更多
[size=large][/size][color=blue][/color]
Property:
是Objective-C 2.0中新增加的語法。利用這個語法,在precompiler時會幫我們產生getter及setter。@property放在物件宣告的地方。基本語法如下:
@property(attributes, …) type propertyName;
Attributes間用”,”分開,可設定的屬性有:
1.getter,setter的名稱,如getter=getVaribleName
2.讀寫設定:
預設為readwrite,會產生getter與setter。
readonly表示沒有setter所以不能出現在等號的左邊。
3.setter設定
預設為assign,把等號右邊的數值與位址賦予等號左邊。
retain,只能用在objective-C的物件類型,在賦值的同時會呼叫retain,原本的值會被release釋放。
copy,用在Objective-C的物件類型,賦值時會自動呼叫copy,原本的值會被release釋放。此物件類型需實作NSCopying protocol。
assign与retain的区别:
There's no such thing as the "scope of an object" in Objective-C. Scope rules have nothing to do with an object's lifetime — the retain count is everything.

You usually need to claim ownership of your instance variables. See the Objective-C memory management rules. With a retain property, your property setter claims ownership of the new value and relinquishes ownership of the old one. With an assign property, the surrounding code has to do this, which is just as mess in terms of responsibilities and separation of concerns. The reason you would use an assign property is in a case where you can't retain the value (such as non-object types like BOOL or NSRect) or when retaining it would cause unwanted side effects.

Incidentally, in the case of an NSString, the correct kind of property is usually copy. That way it can't change out from under you if somebody passes in an NSMutableString (which is valid — itis a kind of NSString).


copy与retain的区别
copy是复制一个新对象给指针,retain是将原来的对象给指针。当需要修改值,但又不影响原来的值时,使用copy
copy的实验如下
代码:
NSMutableString *test = [[[NSMutableString alloc]initWithFormat: @"helloaaaa"]autorelease];
       
        NSLog(@"1The test retaincount = %d", [test retainCount]);
        A *obj = [A new];
        NSMutableString *a = [test copy];
        NSRange r;
        r.length = 3;
        r.location = 2;
        [test replaceCharactersInRange:r withString:@"aaa"];
        NSLog(@"2The test retaincount = %d, %@", [test retainCount], test);
        NSLog(@"3The a retaincount = %d, %@", [a retainCount], a);
        NSLog(@"After a release");
        [a release];
        NSLog(@"4The test retaincount = %d, %@", [test retainCount], test);
        NSLog(@"5The a retaincount = %d, %@", [a retainCount], a);
实验结果:
2010-08-12 21:28:38.135 TestRetain[1941:a0f] 1The test retaincount = 1
2010-08-12 21:28:38.138 TestRetain[1941:a0f] 2The test retaincount = 1, heaaaaaaa
2010-08-12 21:28:38.138 TestRetain[1941:a0f] 3The a retaincount = 1, helloaaaa
2010-08-12 21:28:38.139 TestRetain[1941:a0f] After a release
2010-08-12 21:28:38.139 TestRetain[1941:a0f] 4The test retaincount = 1, heaaaaaaa
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)


4.getter設定
  noatomic,預設是atomic(不寫就是用預設值,沒有atomic這個值),主要是用在多執行緒時,atomic保証可以取得正確的值,但效率較差。在手機上因效率考量,多用noatomic。
使用時在implementation中加入synthesize,語法如下
把build下及/Users/Jason/Library/Application Support/iPhone Simulator/4.0路径下缓存删了,重试
分享到:
评论

相关推荐

    举例讲解Objective-C中@property属性的用法

    学过c/c++的朋友都知道,我们定义struct/class的时候,如果把访问限定符(public,protected,private)设置为public的话,那么我们是可以直接用.号来访问它内部的数据成员的。比如 //in Test.h class Test { public:...

    Swift-Study:Swift学习:从Objective-C到Swift

    我们熟悉的Objective-C特性在Swift中如何展现。 从Objective-C到Swift的进步改进。研究对比Swift在安全性,易用性上的提升,给我们带来的新编程范式。 目录: 1.属性(property)和实例变量(instance variable) ###...

    FFCTemplate:Objective-C中基于模型的模板

    Objective-C中的一个简单的模板渲染器。 用对象属性,方法和填充模板。 用法 给定 @interface MYObject : NSObject @property ( nonatomic , copy ) NSString *color; @property ( nonatomic , copy ) NSNumber *...

    ClassProperty:用于获取 Objective-C 类的所有实例变量及其对应类型的代码(使用 Objective-C 运行时)

    用于获取 Objective-C 类的所有实例变量及其对应类型的代码(使用 Objective-C 运行时) 它还支持超级类用法要运行示例项目, pod install克隆 repo,然后从 Example 目录运行pod install 。安装ClassProperty 可...

    BMExport:一个 JSON 转 Objective-C,Swift class, Swift struct Model 属性的 Mac 小工具,【点击直接下载 https

    一个 JSON 转 Model 属性代码的 Mac 小工具,目前支持 Objective-C,Swift class, Swift struct 。【点击直接下载,建议直接编译源码马上体验最新更全功能 】。当前功能 JSON -> Model 属性代码 添加注释占位 属性...

    Objective-C 代码与Javascript 代码相互调用实例

    由于本人在项目中会用到这部分功能,做下记录! JS调用OC 很多应用里面或多或少的调用了网页,来达到绚丽的效果,所谓的JS调用OC…..举个例子吧,网页上有个按钮 点击按钮跳转界面,跳转的动作由OC的代码实现。 OC...

    iOS中详解Block作为property属性实现页面之间传值

    我们可以把Block当做Objective-C的匿名函数。Block允许开发者在两个对象之间将任意的语句当做数据进行传递,往往这要比引用定义在别处的函数直观。另外,block的实现具有封闭性(closure),而又能够很容易获取上下文...

    PropertyMapper:Objective-C iOS应用程序的属性映射

    我在创建创建了Property Mapper , 是一个简单的自包含解决方案,它使您可以指定接收到的数据与应用程序中具有的数据表示之间的映射,以及一些其他功能,例如类型装箱,验证。 我不喜欢传递JSON,所以我在诸如...

    BLCircularProgress:圆形进度栏。 Objective-C版本

    它基于但通过可控制的界面提供了更灵活的属性。 BLCircularProgress允许用户通过触摸本机更新进度。 可以选择(3 * 7 +1)个动画方法来更新当前进度。 Swift版本: 特性 @property (nonatomic) CGFloat ...

    iosStudy:Objective-C和iOS研究

    iosStudy1_UIViewExample UIView UIImageView ViewController2_ProtocolDelegateExample @协议委托模式@property弱属性(保留周期问题)3_TableViewExample UITableView UITableViewCell UITableViewDataSource ...

    Objective-C基础 自定义对象归档详解及简单实例

     NSCoding协议有两个方法,encodeWithCoder方法对对象的属性数据做编码处理,initWithCoder解码归档数据来初始化对象。 示例1 .h头文件 #import @interface user : NSObject @property(nonatomic,retain)...

    headers:标头。 不适用于文件。 页脚并非相反

    Objective-C属性应采用@property (attributes) type name;的样式@property (attributes) type name; Objective-C方法的样式应为- (type)methodName:(type)variableName parameter:(type)name; Objective-C泛型应...

    VDProperty:提取property的声明的属性

    VD属性 提取属性的声明的属性 进口 pod 'VDProperty', '~> 1.0.0' 用法 # import " NSObject+VDProperty.h " NSArray *properties = [[ self class ] vd_properties ]; 有 @property (nonatomic, copy) NSString *...

    Json2Property:Json转换为属性(Xcode编辑器扩展Xcode8或更高版本)

    IntroductionJson to Property (Xcode8 Editor Extension)快速把jsonString格式为属性InstallOSX 10.11 only sudo /usr/libexec/xpccachectlDownload, unzip and run -> System Preferences... -> Extensions -> ...

    XBUserDefaults:利用OC RunTime封装NSUserDefaults

    XBUserDefaultsXBUserDefaults是利用Objective-C RunTime 机制对NSUserDefaults进行一层封装。...实现思路数据存取将@property属性设置为@dynamic,然后利用消息机制在 +(BOOL)resolveInstanceMethod:(SEL)sel方法中

    BDCamera:BDCamera是带有AVFoundation的简单相机

    是Objective-C的依赖项管理器,它可以自动化并简化在项目中使用诸如AFNetworking之类的第三方库的过程。 播客文件 platform :ios , '7.0' pod "BDCamera" , "~> 0.1" 默认安装 将BDCamera文件夹拖到您的项目中。 该...

    AsyncPager:跟踪表视图和集合视图的分页

    AsyncPager是一个Objective-C微型库,可帮助在表视图和集合视图中进行分页。 它跟踪当前页面,并调用您的api加载下一页,因此您无需跟踪该页面。 它是从博客文章中得到启发的。 ###如何安装? AsyncPager支持 。...

    object c/iphone 开发 试题

    other sources:存放除objective-c类之外的源代码文件; resources: 包含应用程序中的非代码文件(因为应用程序只能在自己的沙盒中运行,不然找不到); Frameworks:特殊的库,可以存放库、框架、图像、声音等资源; ...

Global site tag (gtag.js) - Google Analytics