• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

How to Create a Class in Objective-C

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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];
	[email protected]"1600 Pennsylvania Ave NW";
	[email protected]"Washington";
	[email protected]"DC";
	[email protected]"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.

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap