自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)教程

移動開發(fā) iOS
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)格超集。

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)目,如圖:

Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)

2、選擇“View-based Application” 因?yàn)橹皇墙榻B基本語法 所以 “View-based Application” 已經(jīng)夠用了 。 選擇完后  點(diǎn)擊Next 。

3、輸入相應(yīng)的信息后點(diǎn)擊Next,如圖:

Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)

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會幫助生成單元測試代碼模板,如圖:

Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)

這樣 我們的第一個(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

  1. //  
  2. //  helloWorldViewController.m  
  3. //  helloWorld  
  4. //  
  5. //  Created by  宣雨松 on 11-7-4.  
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.  
  9. #import "helloWorldViewController.h"  
  10. #import "MyClass.h"//導(dǎo)入新寫的類  
  11.  
  12. @implementation helloWorldViewController  
  13.  
  14. - (void)dealloc  
  15. {  
  16.     [super dealloc];  
  17.      
  18. }  
  19.  
  20. - (void)didReceiveMemoryWarning  
  21. {  
  22.     // Releases the view if it doesn't have a superview.  
  23.     [super didReceiveMemoryWarning];  
  24.       
  25.     // Release any cached data, images, etc that aren't in use.  
  26. }  
  27.  
  28. #pragma mark - View lifecycle  
  29.  
  30. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  31. - (void)viewDidLoad  
  32. {  
  33.     [super viewDidLoad];  
  34.       
  35.         //打印一個(gè)字符串  
  36.     NSLog(@"only log hello world");   
  37.       
  38.     //字符串相加          
  39.     NSString *str;      
  40.     NSString *str1 = @"plusA ";      
  41.     NSString *str2 = @"+";    
  42.     NSString *str3 = @"plusB";       
  43.     // 把str1 str2 str3 相加后賦值給str %@ 表示是一個(gè)對象 這里也可以用 %d  %s 在這里就不一一舉例了。         
  44.     str = [NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];           
  45.     //打印出str        
  46.     NSLog(@"string plus %@",str);            
  47.     //self 好比C++ 或者 java 語言中的 this 指針 指向本類 這里調(diào)用了本類的 putString方法 將字符串"pass string"作為參數(shù)傳遞了進(jìn)去     
  48.     [self putString:@"pass string"];              
  49.     //在內(nèi)存中new了一個(gè)MyClass的對象  alloc是在內(nèi)存中 分配內(nèi)存  init 則是初始化 這樣寫 屬于規(guī)定寫法           
  50.     MyClass * myclass = [[MyClass alloc] init];  
  51.     // 用myclass指針調(diào)用 類中putclass方法 將字符串 "pass class string"作為參數(shù)傳遞進(jìn)去       
  52.     [myclass putclass:@"pass class string"];   
  53.     //調(diào)用類中靜態(tài)方法 將字符串"static pass class string "作為參數(shù)傳遞進(jìn)去      
  54.     [MyClass staticPutClass:@"static pass class string"];  
  55. }  
  56.  
  57. - (void)viewDidUnload  
  58. {  
  59.     [super viewDidUnload];  
  60.     // Release any retained subviews of the main view.  
  61.     // e.g. self.myOutlet = nil;  
  62. }  
  63. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  64. {  
  65.     // Return YES for supported orientations  
  66.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  67. }  
  68. //自己寫的類方法輸出字符串  
  69. -(void)putString:(NSString *)str  
  70. {  
  71.     NSLog(@"%@",str);  
  72. }  
  73. @end  
  74.  
  75. //這個(gè)類的聲明  
  76. #import <UIKit/UIKit.h> 
  77. @interface helloWorldViewController : UIViewController {  
  78. }  
  79. -(void) putString:(NSString*)str;   
  80. @end 

MyClass類的實(shí)現(xiàn)

  1. #import "MyClass.h"  
  2. @implementation MyClass  
  3. //方法前是-號的說明這是一個(gè)實(shí)力方法 必需本類new過才能調(diào)用  
  4. -(void)putclass:(NSString *)str  
  5. {  
  6.     NSLog(@"%@",str);  
  7. }  
  8. //方法前是+號的說明這是一個(gè)類方法 這種方法無權(quán)訪問實(shí)例變量  
  9. //這里聲明了一個(gè)靜態(tài)方法 無需本類new過也可以調(diào)用  
  10.  +(void)staticPutClass:(NSString *)str{  
  11.     NSLog(@"%@",str);  
  12. }  
  13. @end 

MyClass類的聲明

  1. #import <Foundation/Foundation.h> 
  2.  
  3. @interface MyClass :NSObject{  
  4. }  
  5. -(void) putclass : (NSString *) str;  
  6. +(void) staticPutClass :(NSString *) str;  
  7. @end 

這樣Objective-C 基本的語法就給大家介紹完了, 希望有興趣的朋友可以和我一起討論 我們可以一起進(jìn)步。

小結(jié):Xcode中使用Objective-C基礎(chǔ)語法學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: starming社區(qū)
相關(guān)推薦

2011-08-05 14:16:47

Objective-C 變量 方法

2011-07-25 10:14:13

Objective-C Xcode

2011-07-25 10:30:41

Objective-C Xcode 重構(gòu)

2011-07-25 11:02:29

Objective-C Xcode 標(biāo)簽

2015-07-07 10:43:59

Swift語法基礎(chǔ)

2010-11-04 16:32:00

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2009-09-10 13:54:27

LINQ語法

2015-07-07 10:58:29

Swift語法高級

2011-08-05 14:03:39

Objective-C 對象 模板

2011-08-17 11:15:22

Objective-C語法

2011-07-06 11:19:45

Objective-C

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-05-11 13:54:08

Objective-C

2011-08-09 15:53:28

2014-04-30 10:16:04

Objective-CiOS語法

2011-07-18 16:36:51

Objective-C XCode

2014-08-05 10:51:09

Xcode警告Objective-C

2011-07-27 17:41:35

Objective-C Xcode
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號