Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)教程
Objective-C基礎(chǔ)語法學(xué)習(xí)是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)語法的內(nèi)容,如果想從事iphone開發(fā)的話,Objective-C 這門語言就不得不學(xué)會 我們都知道C語言是沒有面向?qū)ο蟮?而Objective-C 則是ANSI C 的一個(gè)嚴(yán)格超集,它是具有面向?qū)ο蟮奶匦缘?由于IPHONE 的成功 讓這門語言現(xiàn)在非常的火熱 今天筆者為大家介紹一下在xcode中使用Objective-C 的基本語法。
1、打開mac系統(tǒng)中強(qiáng)大的Xcode軟件 單擊Create a new Xcode project 創(chuàng)建一個(gè)Xcode項(xiàng)目,如圖:
2、選擇“View-based Application” 因?yàn)橹皇墙榻B基本語法 所以 “View-based Application” 已經(jīng)夠用了 。 選擇完后 點(diǎn)擊Next 。
3、輸入相應(yīng)的信息后點(diǎn)擊Next,如圖:
Product Name: 指產(chǎn)品名稱 ,可以隨意命名。
Company Identifier: 公司標(biāo)識符,一般命名規(guī)則為 “com.公司名”
Bundle Identifier: 指包標(biāo)識符,用于唯一標(biāo)識應(yīng)用程序,默認(rèn)會根據(jù)公司標(biāo)識符和產(chǎn)品名來組合生成
Device Family: 指該應(yīng)用支持的設(shè)備類型,共三個(gè)選項(xiàng):iPhone、iPad、Universal(即iPhone、iPad通用)
Include Unite Tests: 是否包含單元測試代碼模板,如果勾選,Xcode會幫助生成單元測試代碼模板,如圖:
這樣 我們的第一個(gè)項(xiàng)目就創(chuàng)建好了,接下來開始為大家介紹 Objective-C 的語法
在項(xiàng)目視圖中 打開 helloWorldViewController.m文件 找到 - (void)viewDidLoad 方法 (這個(gè)方法每次啟動程序都會調(diào)用 )
學(xué)過C++的朋友應(yīng)該都知道 新寫一個(gè)類會有 一個(gè).h 聲明類的變量 方法等 .cpp 用來實(shí)現(xiàn)方法 Objective-C 則也類似C++ .h 聲明類的變量 方法 .m 用來實(shí)現(xiàn)方法
在c語言中 我們在控制臺輸出信息是用printf() Java語言則是 System.out.println() 而Objective-C 則是用 NSLog();
打開控制臺的快捷鍵為 command + shift + R
- //
- // helloWorldViewController.m
- // helloWorld
- //
- // Created by 宣雨松 on 11-7-4.
- // Copyright 2011年 __MyCompanyName__. All rights reserved.
- //
- #import "helloWorldViewController.h"
- #import "MyClass.h"//導(dǎo)入新寫的類
- @implementation helloWorldViewController
- - (void)dealloc
- {
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //打印一個(gè)字符串
- NSLog(@"only log hello world");
- //字符串相加
- NSString *str;
- NSString *str1 = @"plusA ";
- NSString *str2 = @"+";
- NSString *str3 = @"plusB";
- // 把str1 str2 str3 相加后賦值給str %@ 表示是一個(gè)對象 這里也可以用 %d %s 在這里就不一一舉例了。
- str = [NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];
- //打印出str
- NSLog(@"string plus %@",str);
- //self 好比C++ 或者 java 語言中的 this 指針 指向本類 這里調(diào)用了本類的 putString方法 將字符串"pass string"作為參數(shù)傳遞了進(jìn)去
- [self putString:@"pass string"];
- //在內(nèi)存中new了一個(gè)MyClass的對象 alloc是在內(nèi)存中 分配內(nèi)存 init 則是初始化 這樣寫 屬于規(guī)定寫法
- MyClass * myclass = [[MyClass alloc] init];
- // 用myclass指針調(diào)用 類中putclass方法 將字符串 "pass class string"作為參數(shù)傳遞進(jìn)去
- [myclass putclass:@"pass class string"];
- //調(diào)用類中靜態(tài)方法 將字符串"static pass class string "作為參數(shù)傳遞進(jìn)去
- [MyClass staticPutClass:@"static pass class string"];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- //自己寫的類方法輸出字符串
- -(void)putString:(NSString *)str
- {
- NSLog(@"%@",str);
- }
- @end
- //這個(gè)類的聲明
- #import <UIKit/UIKit.h>
- @interface helloWorldViewController : UIViewController {
- }
- -(void) putString:(NSString*)str;
- @end
MyClass類的實(shí)現(xiàn)
- #import "MyClass.h"
- @implementation MyClass
- //方法前是-號的說明這是一個(gè)實(shí)力方法 必需本類new過才能調(diào)用
- -(void)putclass:(NSString *)str
- {
- NSLog(@"%@",str);
- }
- //方法前是+號的說明這是一個(gè)類方法 這種方法無權(quán)訪問實(shí)例變量
- //這里聲明了一個(gè)靜態(tài)方法 無需本類new過也可以調(diào)用
- +(void)staticPutClass:(NSString *)str{
- NSLog(@"%@",str);
- }
- @end
MyClass類的聲明
- #import <Foundation/Foundation.h>
- @interface MyClass :NSObject{
- }
- -(void) putclass : (NSString *) str;
- +(void) staticPutClass :(NSString *) str;
- @end
這樣Objective-C 基本的語法就給大家介紹完了, 希望有興趣的朋友可以和我一起討論 我們可以一起進(jìn)步。
小結(jié):Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!