How to Create a Class in Objective-C

Have you been wondering how to create a class and use it as an object in Objective-C?

Anyone who is used to object-oriented programming will recognize the pattern demonstrated here. You will learn how to: use properties, allocate and de-allocate objects in memory, use a methods and you will see how to launch the Google Maps app on the iPhone with your address information.

Please note that their are many different ways and some subtleties to implementing a class in Objective-C that will not be discussed here. My goal is to show you the simplest way to get up to speed as soon as possible.

Address Class Introduction

The “Address” class simply stores address information such as street, city and zip code. Address will also return the correctly formated link that Google Maps needs to map the address location. Finally, Address will take the Google link, launch the Google Maps app and drop a pin at the location of the address.

NSObject Subclass

From XCode, select “File” and then “New File…”. You will see a dialog box come that looks like something like this:


Choose NSObject subclass and call it “Address”. XCode will create two files for you: Address.h and Address.m. Classes have two files associated with them: the header file (”h” file extension) and the implementation file (”m” file extension).

The best way to think of the header file is that this is the place were you will define what your class does (but you do not implement anything here). Most of the coding will be done in the “m” file.

Add the Street, City and Zip Properties

In object-oriented programming, classes have properties and methods. Properties describe attributes of the thing represented by the class while methods are the class behaviors. The properties of the Address class are: Street, City, State and Zip – attributes that describe an address.

In Objective-C you must add code in a few places in both the header and implementation files in order to use properties.

In the header file you add code in two places:

 

#import <Foundation/Foundation.h>


@interface Address : NSObject {

	NSString *Street;
	NSString *City;
	NSString *State;
	NSString *Zip;
}

@property (nonatomic,retain)NSString *Street;
@property (nonatomic,retain)NSString *City;
@property (nonatomic,retain)NSString *State;
@property (nonatomic,retain)NSString *Zip;

-(NSString *)googleHttpLink;
-(void) openGoogleMapsAppWithThisAddress;

@end

 In the implementation file you need to “synthesize” the properties using @synthesize. It is also important to add code to release the memory associated with the properties in the class dealloc method.

 

@implementation Address
@synthesize Street,City,State,Zip;

-(void)dealloc
{
	[Street release];
	[City release];
	[State release];
	[Zip release];
	[super dealloc];
}

 

After you put your code in these four places your properties are ready to be used.

Detour: Using the Address Class Properties in Your Code

Now that you have properties defined in your address class you can use the class to create an object that stores address information. To keep things simple I will add the code right in the app delegate in the applicationDidFinishLaunching method. This is generally the first place you will add code to a new iPhone app.

NOTE: this file is the implementation file for you app delegate. It is usually named something like: “<yourProject>AppDelegate.m”.

First thing I had to do was add a reference to the Address header file I created. This goes at the very top and looks like this:

#import “Address.h”

Now, I simply need to add code to use an address object. The basic pattern is this: allocate memory, create object, use object and de-allocate memory. I will create an Address object, assign address information to it, write out that information to the log and finally I will de-allocate the memory associated with the Address object.

//
//  GoogleTestAppDelegate.m
//  GoogleTest
//
//  Created by cenphoenix on 10/25/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "GoogleTestAppDelegate.h"
#import "GoogleTestViewController.h"
#import "Address.h"

@implementation GoogleTestAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
	Address *addressObject=[[Address alloc] init];
	addressObject.Street=@"1600 Pennsylvania Ave NW";
	addressObject.City=@"Washington";
	addressObject.State=@"DC";
	addressObject.Zip=@"20500";
	[addressObject openGoogleMapsAppWithThisAddress];
	
	[addressObject release];
	
    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end
 

So, that is that – now you can create and use a class in Objective-C. Before we end I want to make this class a little bit more useful by adding a method to create the hyperlink that Google needs to find the address on the map. Then I want give my address objects the ability to launch the Google Map app on the iPhone with the address information contained in the object.

Launch Google Maps from the Address Class

In order to Google Map functionality to the Address class I need to do two things: create a function that will return the address information in string formatted to work with Google Maps and I need to create a method that will take that string and open the Google Maps app.

To create the function I add this to the header file:

-(NSString *)googleHttpLink;
-(void) openGoogleMapsAppWithThisAddress;
 

and this to the implementation file:

-(NSString *)googleHttpLink
{
	NSString *street_temp=[NSString stringWithString:self.Street];
	street_temp=[street_temp stringByReplacingOccurrencesOfString:@" " withString:@"+"];
	
	NSString *city_temp=[NSString stringWithString:self.City];
	city_temp=[city_temp stringByReplacingOccurrencesOfString:@" " withString:@"+"];
	
	NSString *state_temp=[NSString stringWithString:self.State];
	state_temp=[state_temp stringByReplacingOccurrencesOfString:@" " withString:@"+"];
	
	NSString *zip_temp=[NSString stringWithString:self.Zip];
	zip_temp=[zip_temp stringByReplacingOccurrencesOfString:@" " withString:@"+"];
	
	NSString *temp=[NSString stringWithFormat:@"http://maps.google.com/?q=%@+%@+%@+%@",
					street_temp,city_temp,state_temp,zip_temp];
	return temp;
	
}


-(void) openGoogleMapsAppWithThisAddress
{
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[self googleHttpLink]]];
}
 

 

Conclusion + Where to Get the Files

So, that is it – you now should be able to create your own Objective-C classes to use in your iPhone projects and you have some clues about how to use the Google Maps app. I hope you enjoyed this tutorial. If you have any comments please put them in the form below.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值