Learn Objective-C

Learn Objective-C from http://cocoadevcentral.com

Calling Methods

You can call a methon on object or class

[object method];
[object methodWithInput:input];
id myObject = [NSString string];

id type can refer to any kind of object.
a method name can splite to several segments, not named arguments.

-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

The method name is actually writeToFile:atomically: in the runtime system.

Accessors

All instance variables are private by default.

Traditional 1.x syntax:

[photo setCaption:@"Day at the Beach"];
output = [photo caption];

Objective-C 2.0 dot syntax:

photo.caption = @"Day at the Beach";
output = photo.caption;   

Choose only one style for each project.

The dot syntax should only be used setters and getters, not for general purpose methods.

Creating Objects

Create a autoreleased object:

NSString* myString = [NSString string];

The nested method call way:

NSString* myString = [[NSString alloc] init];
NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];

Basic Memory Management

If you create an object using the manual alloc style, you need to release the object later. You should not manually release an autoreleased object because your application will crash if you do.

Designing a Class Interface

#import directive automatically guards against including a single file multiple times.
The @interface says that this is a declaration of the class Photo.
Objective-C methods typically leave out the "get" prefix.
The compiler assumes a method returns an id object, and that all input values are id.

Class Implementation

There are actually two ways to free a reference to an object: release and autorelease. The standard release will remove the reference immediately. The autorelease method will release it sometime soon

We can create an init method to set inital values for our instance variables:

- (id) init
{
    if ( self = [super init] )
    {
        [self setCaption:@"Default Caption"];
        [self setPhotographer:@"Default Photographer"];
    }
    return self;
}

The dealloc method is called on an object when it is being removed from memory.

- (void) dealloc
{
    [caption release];
    [photographer release];
    [super dealloc];
}

[super dealloc] to ask the superclass to do its cleanup. If we don't do this, the object will not be removed, which is a memory leak.
The dealloc method is not called on objects if garbage collection is enabled. Instead, you implement the finalize method.

More on Memory Management

By ARC, you alloc an object, maybe retain it at some point, then send one release for each alloc/retain you sent.
if you create an object with alloc or copy, send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.

Logging

NSLog ( @"The current date and time is: %@", [NSDate date] );

Properties

Properties are a feature in Objective-C that allow us to automatically generate accessors
In the .h file:

@property (retain) NSString* caption;
@property (retain) NSString* photographer;

In the .m file:

@synthesize caption;
@synthesize photographer;    

Accessors will only be generated if the don't already exist.

Calling Methods on Nil

In Objective-C, the nil object is the functional equivalent to the NULL pointer,The difference is that you can call methods on nil without crashing or throwing an exception.

Categories

A category allows you to add methods to an existing class without subclassing it or needing to know any of the details of how it's implemented.
//.h
#import <Cocoa/Cocoa.h>

@interface NSString (Utilities)
- (BOOL) isURL;
@end

//.m
#import "NSString-Utilities.h"
        
@implementation NSString (Utilities)

- (BOOL) isURL
{
    if ( [self hasPrefix:@"http://"] )
        return YES;
    else
        return NO;
}
@end
//using
NSString* string1 = @"http://pixar.com/";
NSString* string2 = @"Pixar";

if ( [string1 isURL] )
    NSLog (@"string1 is a URL");

if ( [string2 isURL] )
    NSLog (@"string2 is a URL"); 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末揪漩,一起剝皮案震驚了整個濱河市住闯,隨后出現(xiàn)的幾起案子奔则,更是在濱河造成了極大的恐慌止吁,老刑警劉巖浇雹,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異导狡,居然都是意外死亡纹坐,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門痊夭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刁岸,“玉大人,你說我怎么就攤上這事她我『缡铮” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵番舆,是天一觀的道長酝碳。 經(jīng)常有香客問我,道長恨狈,這世上最難降的妖魔是什么疏哗? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮禾怠,結(jié)果婚禮上沃斤,老公的妹妹穿的比我還像新娘圣蝎。我一直安慰自己,他們只是感情好衡瓶,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布徘公。 她就那樣靜靜地躺著,像睡著了一般哮针。 火紅的嫁衣襯著肌膚如雪关面。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天十厢,我揣著相機與錄音等太,去河邊找鬼。 笑死蛮放,一個胖子當(dāng)著我的面吹牛缩抡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播包颁,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瞻想,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了娩嚼?” 一聲冷哼從身側(cè)響起蘑险,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎岳悟,沒想到半個月后佃迄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡贵少,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年呵俏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滔灶。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡普碎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宽气,到底是詐尸還是另有隱情随常,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布萄涯,位于F島的核電站绪氛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏涝影。R本人自食惡果不足惜枣察,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧序目,春花似錦臂痕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至叛赚,卻和暖如春澡绩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背俺附。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工肥卡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人事镣。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓步鉴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親璃哟。 傳聞我的和親對象是個殘疾皇子氛琢,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容