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

推荐订阅源

C
Check Point Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
腾讯CDC
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
Vercel News
Vercel News
P
Proofpoint News Feed
雷峰网
雷峰网
博客园_首页
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题

博客园 - 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 Ge...
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.