青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::
Learn Objective-C

Objective-C

Objective-C is the primary language used to write Mac software. If you're comfortable with basic object-oriented concepts and the C language, Objective-C will make a lot of sense. If you don't know C, you should read the C Tutorial first. 

This tutorial is written and illustrated by Scott Stevenson
 
Copyright © 2008 Scott Stevenson
1

Calling Methods

To get started as quickly as possible, let's look at some simple examples. The basic syntax for calling a method on an object is this:
 
[object method]; [object methodWithInput:input];
Methods can return a value:
 
output = [object methodWithOutput]; output = [object methodWithInputAndOutput:input];
You can call methods on classes too, which is how you create objects. In the example below, we call the string method on the NSString class, which returns a new NSString object:
 
id myObject = [NSString string];
The id type means that the myObject variable can refer to any kind of object, so the actual class and the methods it implements aren't known when you compile the app. 

In this example, it's obvious the object type will be an NSString, so we can change the type:
 
NSString* myString = [NSString string];
This is now an NSString variable, so the compiler will warn us if we try to use a method on this object which NSString doesn't support. 

Notice that there's a asterisk to the right of the object type. All Objective-C object variables are pointers types. The id type is predefined as a pointer type, so there's no need to add the asterisk.
 

Nested Messages

In many languages, nested method or function calls look like this:
 
function1 ( function2() );
The result of function2 is passed as input to function1. In Objective-C, nested messages look like this:
 
[NSString stringWithFormat:[prefs format]];
Avoid nested nesting more than two message calls on a single line, as it easily gets unreadable.
 

Multi-Input Methods

Some methods take multiple input values. In Objective-C, a method name can be split up into several segments. In the header, a multi-input method looks like this:
 
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
You call the method like this:
 
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
These are not just named arguments. The method name is actuallywriteToFile:atomically: in the runtime system.
2

Accessors

All instance variables are private in Objective-C by default, so you should use accessors to get and set values in most cases. There are two syntaxes. This is the traditional 1.x syntax:
 
[photo setCaption:@"Day at the Beach"]; output = [photo caption];
The code on the second line is not reading the instance variable directly. It's actually calling a method named caption. In most cases, you don't add the "get" prefix to getters in Objective-C. 

Whenever you see code inside square brackets, you are sending a message to an object or a class.
 

Dot Syntax

The dot syntax for getters and setters is new in Objective-C 2.0, which is part of Mac OS X 10.5:
 
photo.caption = @"Day at the Beach"; output = photo.caption;
You can use either style, but choose only one for each project. The dot syntax should only be used setters and getters, not for general purpose methods.
3

Creating Objects

Object Instance
There are two main ways to create an object. The first is the one you saw before:
 
NSString* myString = [NSString string];
This is the more convenient automatic style. In this case, you are creating an autoreleased object, which we'll look at in more detail later. In many cases, though, you need to create an object using the manual style:
 
NSString* myString = [[NSString alloc] init];
This is a nested method call. The first is the alloc method called on NSString itself. This is a relatively low-level call which reserves memory and instantiates an object. 

The second piece is a call to init on the new object. The init implementation usually does basic setup, such as creating instance variables. The details of that are unknown to you as a client of the class. 

In some cases, you may use a different version of init which takes input:
 
NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];
4
Object Instance

Basic Memory Management

If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory management until you get to more complex cases. 

However, you may not always be working with an environment that supports garbage collection. In that case, you need to know a few basic concepts. 

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

Here are two examples:
 
// string1 will be released automatically NSString* string1 = [NSString string]; // must release this when done NSString* string2 = [[NSString alloc] init]; [string2 release];
For this tutorial, you can assume that an automatic object will go away at the end of the current function. 

There's more to learn about memory management, but it will make more sense after we look at a few more concepts.
5
Editor Window

Designing a Class Interface

The Objective-C syntax for creating a class is very simple. It typically comes in two parts. 

The class interface is usually stored in the ClassName.h file, and defines instance variables and public methods. 

The implementation is in the ClassName.m file and contains the actual code for these methods. It also often defines private methods that aren't available to clients of the class. 

Here's what an interface file looks like. The class is called Photo, so the file is named Photo.h:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } @end
First, we import Cocoa.h, to pull in all of the basic classes for a Cocoa app. The #import directive automatically guards against including a single file multiple times. 

The @interface says that this is a declaration of the class Photo. The colon specifies the superclass, which is NSObject. 

Inside the curly brackets, there are two instance variables: caption andphotographer. Both are NSStrings, but they could be any object type, including id. 

Finally, the @end symbol ends the class declaration.
 

Add Methods

Let's add some getters for the instance variables:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } - caption; - photographer; @end
Remember, Objective-C methods typically leave out the "get" prefix. A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. 

By default, the compiler assumes a method returns an id object, and that all input values are id. The above code is technically correct, but it's unusual. Let's add specific types for the return values:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } - (NSString*) caption; - (NSString*) photographer; @end
Now let's add setters:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } - (NSString*) caption; - (NSString*) photographer; - (void) setCaption: (NSString*)input; - (void) setPhotographer: (NSString*)input; @end
Setters don't need to return a value, so we just specify them as void.
6
Object Instance

Class Implementation

Let's create an implementation, starting with the getters:
 
#import "Photo.h" @implementation Photo - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @end
This part of the code starts with @implementation and the class name, and has @end, just like the interface. All methods must appear between these two statements. 

The getters should look very familiar if you've ever written code, so let's move on to the setters, which need a bit more explanation:
 
- (void) setCaption: (NSString*)input { [caption autorelease]; caption = [input retain]; } - (void) setPhotographer: (NSString*)input { [photographer autorelease]; photographer = [input retain]; }
Each setter deals with two variables. The first is a reference to the existing object, and the second is the new input object. In a garbage collected environment, we could just set the new value directly:
 
- (void) setCaption: (NSString*)input { caption = input; }
But if you can't use garbage collection, you need to release the old object, and retain the new one. 

There are actually two ways to free a reference to an object: release andautorelease. The standard release will remove the reference immediately. The autorelease method will release it sometime soon, but it will definitely stay around until the end of the current function (unless you add custom code to specifically change this). 

The autorelease method is safer inside a setter because the variables for the new and old values could point to the same object. You wouldn't want to immediately release an object which you're about to retain. 

This may seem confusing right now, but it will make more sense as you progress. You don't need to understand it all yet.
 

Init

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; }
This is fairly self-explanatory, though the second line may look a bit unusual. This is a single equals sign, which assigns the result of [super init] to self

This essentially just asks the superclass to do its own initialization. The ifstatement is verifying that the initialization was successful before trying to set default values.
 
 

Dealloc

The dealloc method is called on an object when it is being removed from memory. This is usually the best time to release references to all of your child instance variables:
 
- (void) dealloc { [caption release]; [photographer release]; [super dealloc]; }
On the first two lines, we just send release to each of the instance variables. We don't need to use autorelease here, and the standard release is a bit faster. 

The last line is very important. We have to send the message
[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.
 
7

More on Memory Management

Objective-C's memory management system is called reference counting. All you have to do is keep track of your references, and the runtime does the actual freeing of memory. 

In simplest terms, you alloc an object, maybe retain it at some point, then send one release for each alloc/retain you sent. So if you used alloc once and then retain once, you need to release twice.
 
That's the theory of reference counting. But in practice, there are usually only two reasons to create an object: 

1. To keep it as an instance variable
2. To use temporarily for single use inside a function 

In most cases, the setter for an instance variable should just autoreleasethe old object, and retain the new one. You then just make sure to release it in dealloc as well. 

So the only real work is managing local references inside a function. And there's only one rule: if you create an object with alloc or copy, send it arelease or autorelease message at the end of the function. If you create an object any other way, do nothing. 

Here's the first case, managing an instance variable:
 
- (void) setTotalAmount: (NSNumber*)input { [totalAmount autorelease]; totalAmount = [input retain]; } - (void) dealloc { [totalAmount release]; [super dealloc]; }
Here's the other case, local references. We only need to release the object created with alloc:
 
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75]; NSNumber* value2 = [NSNumber numberWithFloat:14.78]; // only release value1, not value2 [value1 release];
And here's a combo: using a local reference to set an object as an instance variable:
 
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75]; [self setTotal:value1]; NSNumber* value2 = [NSNumber numberWithFloat:14.78]; [self setTotal:value2]; [value1 release];
Notice how the rules for managing local references are exactly the same, regardless of whether you're setting them as instance variables or not. You don't need to think about how the setters are implemented. 

If you understand this, you understand 90% of what you will ever need to know about Objective-C memory management.
 
8

Logging

Logging messages to the console in Objective-C is very simple. In fact, theNSLog() function is nearly identical to the C printf() function, except there's an additional %@ token for objects.
 
NSLog ( @"The current date and time is: %@", [NSDate date] );
You can log an object to the console. The NSLog function calls thedescription method on the object, and prints the NSString which is returned. You can override the description method in your class to return a custom string.
9

Properties

When we wrote the accessor methods for caption and author earlier, you might have noticed that the code is straightforward, and could probably be generalized. 

Properties are a feature in Objective-C that allow us to automatically generate accessors, and also have some other side benefits. Let's convert the Photo class to use properties. 

Here's what it looked like before:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } - (NSString*) caption; - (NSString*) photographer; - (void) setCaption: (NSString*)input; - (void) setPhotographer: (NSString*)input; @end
Here's what it looks like once converted to properties:
 
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } @property (retain) NSString* caption; @property (retain) NSString* photographer; @end
The @property is an Objective-C directive which declares the property. The "retain" in the parenthesis specifies that the setter should retain the input value, and the rest of the line simply specifies the type and the name of the property. 

Now let's take a look at the implementation of the class:
 
#import "Photo.h" @implementation Photo @synthesize caption; @synthesize photographer; - (void) dealloc { [caption release]; [photographer release]; [super dealloc]; } @end
The @synthesize directive automatically generates the setters and getters for us, so all we have to implement for this class is the dealloc method. 

Accessors will only be generated if they don't already exist, so feel free to specify @synthesize for a property, then implement your custom getter or setter if you want. The compiler will fill in whichever method is missing. 

There are many other options for the property declarations, but those are outside of the scope of this tutorial.
 
10

Calling Methods on Nil

In Objective-C, the nil object is the functional equivalent to the NULLpointer in many other languages. The difference is that you can call methods on nil without crashing or throwing an exception. 

This technique used by the frameworks in a number of different ways, but the main thing it means to you right now that you usually don't need to check for nil before calling a method on an object. If you call a method on nil that returns an object, you will get nil as a return value. 

We can also use this to improve our dealloc method slightly:
 
- (void) dealloc { self.caption = nil; self.photographer = nil; [super dealloc]; }
This works because when we set nil as an instance variable, the setter just retains nil (which does nothing) and releases the old value. This approach is often better for dealloc because there's no chance of the variable pointing at random data where an object used to be. 

Note that we're using the self.<var> syntax here, which means we're using the setter and picking up the memory management for free. If we just directly set the value like this, there would be a memory leak:
 
// incorrect. causes a memory leak. // use self.caption to go through setter caption = nil;
11

Categories

Categories are one of the most useful features of Objective-C. Essentially, 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. 

This is particularly useful because you can add methods to built-in objects. If you want to add a method to all instances of NSString in your application, you just add a category. There's no need to get everything to use a custom subclass. 

For example, if I wanted to add a method to NSString to determine if the contents is a URL, it would look like this:
 
#import <Cocoa/Cocoa.h> @interface NSString (Utilities) - (BOOL) isURL; @end
This is very similar to a class declaration. The differences are that there is no super class listed, and there's a name for the category in parenthesis. The name can be whatever you want, though it should communicate what the methods inside do. 

Here's the implementation. Keep in mind this is not a good implementation of URL detection. We're just trying to get the concept of categories across:
 
#import "NSString-Utilities.h" @implementation NSString (Utilities) - (BOOL) isURL { if ( [self hasPrefix:@"http://"] ) return YES; else return NO; } @end
Now you can use this method on any NSString. The following code will print "string1 is a URL" in the console:
 
NSString* string1 = @"http://pixar.com/"; NSString* string2 = @"Pixar"; if ( [string1 isURL] ) NSLog (@"string1 is a URL"); if ( [string2 isURL] ) NSLog (@"string2 is a URL");
Unlike subclasses, categories can't add instance variables. You can, however, use categories to override existing methods in classes, but you should do so very carefully. 

Remember, when you make changes to a class using a category, it affects all instances of that class throughout the application.
 

Wrap Up

This is a basic overview of Objective-C. As you've seen, the language is pretty easy to pick up. There's not much special syntax to learn, and the same conventions are used over and over again throughout Cocoa. 

If you'd like these examples in action, download the project below and look through the source code:
LearnObjectiveC Xcode 3.0 Project (56k)
 
Thank you.
From: http://www.cocoadevcentral.com/d/learn_objectivec/
Cocoa Dev Central is a service mark of Tree House Ideas
Site design © 2004-2008 Scott Stevenson | Made with TextMate
posted on 2011-12-02 00:17 逛奔的蝸牛 閱讀(675) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            裸体女人亚洲精品一区| 久久亚洲欧美| 国产精品免费观看在线| 久久一区国产| 亚洲一区久久久| 亚洲免费在线观看视频| 亚洲国内在线| 在线看成人片| 日韩视频不卡中文| 亚洲激情午夜| 亚洲香蕉网站| 亚洲老司机av| 一本高清dvd不卡在线观看| 亚洲少妇诱惑| 亚洲午夜在线观看| 久久九九热re6这里有精品| 欧美一区二区三区四区夜夜大片| 在线亚洲免费视频| 久久久亚洲综合| 欧美一区成人| 欧美日韩国产成人在线91| 亚洲欧美日韩视频一区| 欧美大片一区| 欧美成人综合网站| 噜噜噜噜噜久久久久久91 | 亚洲欧美在线看| 一区二区三区产品免费精品久久75| 欧美激情第9页| 欧美激情一区二区久久久| 日韩亚洲一区在线播放| 一区二区三区高清不卡| 亚洲自拍偷拍麻豆| 欧美高清在线视频| 国产精品久久9| 国产日韩亚洲欧美| 亚洲精品小视频在线观看| aaa亚洲精品一二三区| 99精品福利视频| 久久精品91久久久久久再现| 久久一区二区三区四区五区| 亚洲欧美日韩专区| 欧美承认网站| aa成人免费视频| 欧美成人亚洲| 国产精品一二一区| 在线日韩av| 久久爱www.| 亚洲第一综合天堂另类专| 亚洲日产国产精品| 久久久久久久一区二区三区| 欧美日韩精品久久| 欧美黄色成人网| 午夜精品久久久久久久| 久久深夜福利免费观看| 国产日韩精品一区二区三区在线| 亚洲国产精彩中文乱码av在线播放| 一区二区三区精品视频| 亚洲电影观看| 欧美专区日韩视频| 欧美视频国产精品| 亚洲午夜电影网| 欧美wwwwww| 久久九九有精品国产23| 国产精品成人观看视频国产奇米| 亚洲第一天堂av| 性欧美1819sex性高清| 欧美激情视频免费观看| 久久免费偷拍视频| 欧美大片免费观看| 精品动漫av| 欧美主播一区二区三区| 国产精品久久9| 欧美一区免费| 美女图片一区二区| 一本久久综合亚洲鲁鲁五月天| 欧美亚洲日本网站| 国产亚洲成年网址在线观看| 亚洲自拍偷拍网址| 欧美一区在线看| 欧美一区2区三区4区公司二百| 欧美日韩国产一区二区| 亚洲一区二区av电影| 亚洲欧洲综合| 欧美国产精品一区| 99精品国产福利在线观看免费| 美女性感视频久久久| 亚洲精品黄网在线观看| 欧美日韩精品在线视频| 亚洲乱码精品一二三四区日韩在线| 亚洲国产小视频在线观看| 蜜桃伊人久久| 亚洲精品韩国| 亚洲一区二区三区影院| 欧美亚州韩日在线看免费版国语版| 亚洲在线一区二区| 亚洲视频电影在线| 国产精品毛片a∨一区二区三区|国| 久久丁香综合五月国产三级网站| 国产精品99久久久久久久女警| 久久精品国产一区二区三| 国产一区二区丝袜高跟鞋图片| 久久蜜桃精品| 美女成人午夜| 香蕉久久一区二区不卡无毒影院| 性色av一区二区三区红粉影视| 国内精品**久久毛片app| 亚洲国产精品久久久久婷婷老年 | 亚洲天天影视| 一区二区三区精品在线| 国产精品视频网| 亚洲第一页中文字幕| 亚洲欧洲日本一区二区三区| 免费在线成人| 国产精品区一区二区三区| 久久久国产精品一区二区中文| 欧美猛交免费看| 欧美一级电影久久| 欧美在线观看视频在线 | 国产精品自拍在线| 免费黄网站欧美| 国产日韩欧美一区| 亚洲国产成人不卡| 欧美va天堂| 久久综合色播五月| 欧美日韩伊人| 欧美专区在线| 一本色道久久综合亚洲91| 夜色激情一区二区| 国产欧美亚洲日本| 日韩天天综合| 黑人巨大精品欧美黑白配亚洲| 亚洲午夜高清视频| 亚洲欧洲精品一区二区精品久久久| 中文av字幕一区| 亚洲日本aⅴ片在线观看香蕉| 欧美成人自拍视频| 伊人久久亚洲美女图片| 亚洲一区日韩在线| 亚洲欧美日韩成人| 欧美激情麻豆| 亚洲精品影视在线观看| 免费视频亚洲| 久久久久久穴| 激情视频一区二区| 欧美一级久久久| 日韩视频免费| 欧美日韩精品是欧美日韩精品| 免费观看成人www动漫视频| 精品成人a区在线观看| 亚洲性xxxx| 在线观看视频免费一区二区三区| 久久免费精品日本久久中文字幕| 午夜电影亚洲| 国产香蕉97碰碰久久人人| 亚洲天堂免费观看| 亚洲欧美激情四射在线日| 国产免费成人在线视频| 亚洲一区二区欧美日韩| 久久精品中文字幕一区二区三区| 国产精品亚洲综合天堂夜夜| 免费欧美视频| 一区二区三区高清视频在线观看| 久久免费高清| 日韩视频一区二区三区在线播放| 最新成人av网站| 久久本道综合色狠狠五月| 欧美大色视频| 亚洲激情影视| 国产精品第一区| 亚洲在线视频| 亚洲一区二区视频在线| 国产一区二区视频在线观看 | 亚洲综合第一| 国产精品免费看片| 麻豆freexxxx性91精品| 欧美高清视频| 国产一区二区黄| 欧美日韩国产小视频在线观看| 亚洲天堂黄色| 美女精品在线| 99精品国产福利在线观看免费| 欧美日韩天堂| 先锋亚洲精品| 亚洲第一区色| 亚洲欧美日韩久久精品| 国产亚洲精品久久久| 亚洲制服av| 欧美~级网站不卡| 一区二区三区 在线观看视频| 国产精品日日摸夜夜添夜夜av | 欧美日韩成人免费| 亚洲一区中文| 国产精品一区二区黑丝| 欧美人妖另类| 亚洲综合第一| 亚洲精选国产| 久久成人免费电影| 亚洲第一精品影视| 国产乱肥老妇国产一区二| 久久婷婷综合激情|