惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
Cloudbric
Cloudbric
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
博客园_首页
The Cloudflare Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
IT之家
IT之家
Cyberwarzone
Cyberwarzone
罗磊的独立博客
美团技术团队
V
V2EX
Project Zero
Project Zero
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
S
Schneier on Security
P
Privacy International News Feed
V
Visual Studio Blog
量子位
T
Tor Project blog
S
Securelist
腾讯CDC
A
About on SuperTechFans
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
B
Blog RSS Feed
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
B
Blog
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LINUX DO - 热门话题
Recorded Future
Recorded Future

博客园 - hai

会移动的文字(Marquee)学习 How To Share The Internet Connection Between Mac and PC Mac OSX 下配置 Google Android 开发环境 Mac OSX Eclipse3.6下安装erlide搭建erlang集成开发环境 Installing Erlang on Mac OS X 浏览器解析 XML DOM How to set the JAVA_HOME variable in Mac OS X – Snow Leopard code sign error doesn't match any valid certificate private key pair in the default keych 错误的解决办法 CSS的跨浏览器代码准则 Google Maps API详解--02 Google Earth API 讲解--02 Google Maps API 详解--01 Network Programming Tutorial: Networking and Bonjour on iPhone 5个必看的cocos2d 开源游戏 有机会都学习下 iPhone开发技巧之工具篇(1)— 将XIB文件转换为Objective-C源程序 Google Earth API 讲解--04 Google Earth API 讲解--05 浏览器获取地理位置
Using Properties And Synthesize In Objective-C 2.0 For Getters And Setters
hai · 2010-07-20 · via 博客园 - hai

In my last entry First steps in Objective-C I described how to create getters and setters and how to handle objects in Objective-C. Well, since then (about 1 day) a lot happened, a lot was read … and I found a nice way how to do this all without writing too much code which is always bug prone.

Let’s take a short look at a possible header file OTSupport.h:

@interface OTSupport : NSObject {
        float aFloat;
        NSString *aString;
}
property(readwrite, assign) float aFloat; property(readwrite, copy) NSString *aString;- (void)printOutAttributes;
@end

We created two attributes in this class, a float and a NSString variable. Furthermore, we have the prototyping line for a method called printOutAttributes. And right between those two declarations, we find a new way of handling getters and setters: the properties. For each attribute we declare a property whereas NSStrings need to be copied if used later on, floats can simply be assigned.

Let’s have a look at the class file:

#import <Foundation/Foundation.h>
#import <OTSupport.h>
@implementation OTSupport

synthesize aFloat; synthesize aString;
- (id)init{
[super init]; self.aFloat = 5.0; self.aString = @“Just a test string”; return self; }
- (void) printOutAttributes{ NSLog("Printing out attributes:"); NSLog(“Setting aFloat = %.3f”, self.aFloat); NSLog("Setting aString = %”, self.aString); }
@end
Just for the kicks, I created a constructor method called init which is called at initialization of an instance of this class. All it does is setting values for the attributes. Furthermore, we see the declaration of the printOutAttributes method which simply prints out the values of the instance attributes.

We don’t see any getter or setter methods, right? Well, this is handled by the two magical lines “synthesize”. These handle the access to the attributes.

Far easier than describing how this works (I’m not even sure I would really get it right this close to the beginning of diving into Objective-C) is showing the main function which uses the instance and its attributes:

#import <Foundation/Foundation.h>
#import <OTSupport.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// [1] Creating an instance of OTSupport and initializing it
id myObject;
myObject = [[OTSupport alloc] init];
// [2] Calling the printOutAttributes method
[myObject printOutAttributes];
// [3] Using the getter
float af = [myObject aFloat];
NSLog(@“Float value is af = %.2f”, af);

// [4] Using the setter
[myObject setAFloat:9.43];
af = [myObject aFloat];
NSLog(@“Float value is af = %.2f”, af);
[myObject release];
[pool drain];
return 0; }

In [1] we simply create an instance and initialize it (giving values to the attributes).

In [2] we use the printOutAttributes method in order to, as the name already states, print out the instance attributes.

In [3] we take advantage of the getter method, simply asking for the float value.

In [4] we overwrite the original value with a new one and print this one out short after.

A neat way to create getters and setters in Objective-C 2.0.