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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

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