- 浏览: 228494 次
- 性别:
- 来自: 广州
-
文章分类
- 全部博客 (109)
- IOS (15)
- 设计模式 (7)
- XML (1)
- Android (31)
- 面试经 (1)
- J2EE (3)
- android md5 加密 (2)
- android imagebutton文字实现 (1)
- 反射机制 (2)
- 基础知识 (1)
- linux (3)
- java (4)
- java基础 (2)
- 文章 (1)
- myeclipse tomcat (1)
- Hadoop (1)
- ubuntu (2)
- redmine (1)
- python (4)
- jmeter (10)
- xamarin (1)
- selenium (9)
- nexus (1)
- appium (3)
- BDD (1)
- apache2 (1)
- zabbix (2)
- python,webdriver (1)
- ajax (1)
- jmeter,正则表达式,关联 (2)
- 性能测试工具 (1)
- Django (0)
- Intelij (1)
- RAP (0)
- 性能测试 (0)
最新评论
[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路径下缓存删了,重试
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路径下缓存删了,重试
发表评论
-
appStore上传苹果应用程序软件发布流程
2012-10-31 09:51 18781.进入开发者中心,进 ... -
iPhone的App的目录结构
2012-10-31 09:57 995iPhone的App的目录结构如下: 对于一个运 ... -
iPhone开发经典语录集锦
2012-10-30 00:50 916如果大家和我一样有感触的话,可以跟在帖子下面,最好简短并附上 ... -
ASIHTTPRequest 详解
2012-10-31 09:57 1031目录 目录 发起一个同步请求 创建一个异步请求 队列请 ... -
【引用】iPhone的App的目录 【引用】iPhone-国际化Iphon
2012-11-04 21:30 952博主:易飞扬 原文链接 : http://www.yifei ... -
iphone开发之 - 启动页面设置
2012-10-31 09:58 1232不管是开发个人项目还是公司项目,大家通常都有一个需求,就 ... -
限制UITextField输入长度的方法
2013-01-26 23:04 900在 iPhone 应用里经常要限制用户输入字符的长度,比如密 ... -
【引用】iphone开发的好网站
2012-10-20 02:29 2789分享iphone开发的好网站,希望大家也能提供一些分享下 ... -
IOS开源项目汇总
2013-01-26 23:05 1473前几天看到一位会员,总结了一些开源的IOS项目,我结合自己开发 ... -
Object-C 中的Selector 概念
2013-01-26 23:05 929Object-C 中的Selector 概念 Andrew ... -
Xcode SVN配置
2012-10-19 00:14 1068Xcode SVN配置 编辑 ~/.subversion/co ... -
Iphone 图片
2012-10-13 00:08 1150如何自定义分组表视图/边框颜色的背景您需要设置UITableV ... -
iphone定位 基本知识
2012-10-04 23:54 879找到一个关于iphone定位 ... -
object c 中 retain 和copy的区别
2012-10-04 23:49 968原来简单解释过属性定义(Property) ,并且提起了简单 ...
相关推荐
Objective-C是一种结合了C语言特性和面向对象编程思想的编程语言。它最初由Brad Cox和Tom Love在20世纪80年代初设计,并在1986年由NEXTSTEP系统引入。Objective-C的主要特点是其独特的消息传递机制以及对C语言的兼容...
Objective-C是在C语言的基础上扩展了Smalltalk式的面向对象特性,使得它既有C语言的强大功能,又具备了面向对象编程的灵活性。 在Objective-C中,类是所有对象的基础。类定义了一组属性(实例变量)和方法(函数)...
**Objective-C**是一种结合了C语言的基础特性和Smalltalk式消息传递机制的面向对象编程语言。作为C语言的一个超集,Objective-C允许开发者直接使用C语言代码。此外,它还借鉴了C++的一些特性,尽管它并非C++的直接...
在Objective-C中,属性(Properties)用于封装数据,它们提供了访问和修改对象状态的方式。你可以通过@property关键字声明属性,并使用@synthesize关键字自动生成getter和setter方法。此外,Objective-C还支持动态...
Objective-C中的控制结构与C语言基本一致,包括但不限于if语句、switch语句、循环等。 ##### 2.7 属性(Properties) 属性提供了一种简洁的方式来管理类的实例变量。通过属性,可以轻松地设置和获取类的成员变量。...
4. **属性和ivar**:Objective-C中的属性提供了封装、访问控制和自动内存管理。使用`@property`关键字声明属性,`@synthesize`来生成存取方法,或者自定义getter和setter。ivar(实例变量)存储对象的状态,通常以下...
1. **Objective-C语法**:Objective-C的语法基于C语言,但引入了类、接口和消息传递等概念。这包括定义类、属性(@property)和方法(@selector)的语法,以及理解类接口(.h)和实现(.m)文件的作用。 2. **对象...
3. **C语言的超集**:由于Objective-C是C语言的超集,因此可以在Objective-C程序中直接使用C语言的语句和数据类型,这使得Objective-C具有很高的兼容性。 4. **内存管理**:Objective-C早期版本使用引用计数的方式...
2. **属性(Property)**:在Objective-C中,属性用来封装数据。`FractionDemo`类可能有` numerator`和`denominator`两个属性,分别代表分数的分子和分母,它们可能是`@property`关键字声明的。 3. **初始化方法**...
### Objective-C语言教程、案例与项目资源详解 #### 一、Objective-C基础知识 **1.1 类与对象** Objective-C的核心概念是类和对象。类是对象的模板或蓝图,它定义了对象应该具备哪些属性(如变量)和行为(如方法...
在Objective-C中,类通过@interface和@implementation来定义,属性通过@property声明,方法通过-(return_type)selector:(参数类型)parameters;定义。 其次,Objective-C中的消息传递是其核心特性。与其他语言不同,...
此外,Objective-C还支持.mm文件类型,允许在Objective-C代码中混合使用C++代码,这对于需要高性能计算的场景非常有用。 #### 示例代码分析 在Objective-C中定义一个类,如MyClass,通常包含以下结构: 1. **类的...
类型编码(Type Encoding)允许Objective-C编译器和运行时系统处理类型信息,而属性声明(Property Declaration)则是一种语法糖,用来声明对象的获取和设置方法(getters和setters)。这两种机制简化了代码的编写,...
属性是Objective-C中的一个重要特性,它简化了属性的管理和访问。通过声明属性,可以自动生成对应的setter和getter方法。 - **属性声明**:属性可以用关键字`@property`来声明,例如: ```objective-c @interface...
Objective-C是在C语言的基础上扩展的,因此,它继承了C的语法特性,如变量声明、控制结构(如if-else、for、while)、函数等。同时,它引入了消息传递机制,这是面向对象编程的核心。在Objective-C中,对象通过发送...
Objective-C 2.0引入了新的关键字,比如@property、@dynamic等,来支持新的特性,如属性的声明和动态绑定。 在Objective-C中,还包含了一些特殊的值,如nil、Nil、YES和NO,这些与C++中的NULL或布尔值true和false不...
首先,Objective-C是在C语言的基础上扩展的,因此,理解C语言的基本语法是学习Objective-C的前提。它引入了消息传递机制,这是Objective-C的关键特性,允许对象之间进行通信。消息传递类似于函数调用,但更加灵活,...
- **与C语言兼容:** Objective-C保留了C语言的所有特性,并在此基础上进行了扩展,因此可以轻松地在Objective-C代码中使用C语言编写的库或代码。 #### 二、Objective-C语言的基础语法 **1. 类与对象** 在...
Objective-C 2.0 是苹果公司开发的一种面向对象的编程语言,它是C语言的超集,主要用于iOS和macOS的应用程序开发。Objective-C 2.0在Objective-C的基础上添加了一些新特性,使得代码更加简洁易读,提高了开发效率。...
总之,"objective-c的一些代码"涵盖了Objective-C语言的基础、面向对象编程的原理以及iOS和macOS开发中的实际应用场景。通过练习、作业和课堂示例,开发者可以逐步掌握这个语言并成为熟练的iOS或macOS开发者。