Xcode中使用Objective-C基础语法学习教程

移动开发 iOS
Objective-C基础语法学习是本文要介绍的内容,主要是来学习语法的内容,如果想从事iphone开发的话 Objective-C 这门语言就不得不学会 我们都知道C语言是没有面向对象的 而Objective-C 则是ANSI C 的一个严格超集。

Objective-C基础语法学习是本文要介绍的内容,主要是来学习语法的内容,如果想从事iphone开发的话,Objective-C 这门语言就不得不学会 我们都知道C语言是没有面向对象的 而Objective-C 则是ANSI C 的一个严格超集,它是具有面向对象的特性的 由于IPHONE 的成功 让这门语言现在非常的火热 今天笔者为大家介绍一下在xcode中使用Objective-C 的基本语法。

1、打开mac系统中强大的Xcode软件 单击Create a new Xcode project  创建一个Xcode项目,如图:

Xcode中使用Objective-C基础语法学习

2、选择“View-based Application” 因为只是介绍基本语法 所以 “View-based Application” 已经够用了 。 选择完后  点击Next 。

3、输入相应的信息后点击Next,如图:

Xcode中使用Objective-C基础语法学习

Product Name: 指产品名称 ,可以随意命名。

Company Identifier: 公司标识符,一般命名规则为 “com.公司名”

Bundle Identifier: 指包标识符,用于唯一标识应用程序,默认会根据公司标识符和产品名来组合生成

Device Family: 指该应用支持的设备类型,共三个选项:iPhone、iPad、Universal(即iPhone、iPad通用)

Include Unite Tests: 是否包含单元测试代码模板,如果勾选,Xcode会帮助生成单元测试代码模板,如图:

Xcode中使用Objective-C基础语法学习

这样 我们的第一个项目就创建好了,接下来开始为大家介绍 Objective-C 的语法

在项目视图中 打开 helloWorldViewController.m文件 找到 - (void)viewDidLoad 方法 (这个方法每次启动程序都会调用 )

学过C++的朋友应该都知道 新写一个类会有 一个.h 声明类的变量 方法等 .cpp 用来实现方法  Objective-C   则也类似C++  .h 声明类的变量 方法  .m 用来实现方法

在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"//导入新写的类  
  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.         //打印一个字符串  
  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 %@ 表示是一个对象 这里也可以用 %d  %s 在这里就不一一举例了。         
  44.     str = [NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];           
  45.     //打印出str        
  46.     NSLog(@"string plus %@",str);            
  47.     //self 好比C++ 或者 java 语言中的 this 指针 指向本类 这里调用了本类的 putString方法 将字符串"pass string"作为参数传递了进去     
  48.     [self putString:@"pass string"];              
  49.     //在内存中new了一个MyClass的对象  alloc是在内存中 分配内存  init 则是初始化 这样写 属于规定写法           
  50.     MyClass * myclass = [[MyClass alloc] init];  
  51.     // 用myclass指针调用 类中putclass方法 将字符串 "pass class string"作为参数传递进去       
  52.     [myclass putclass:@"pass class string"];   
  53.     //调用类中静态方法 将字符串"static pass class string "作为参数传递进去      
  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. //这个类的声明  
  76. #import <UIKit/UIKit.h> 
  77. @interface helloWorldViewController : UIViewController {  
  78. }  
  79. -(void) putString:(NSString*)str;   
  80. @end 

MyClass类的实现

  1. #import "MyClass.h"  
  2. @implementation MyClass  
  3. //方法前是-号的说明这是一个实力方法 必需本类new过才能调用  
  4. -(void)putclass:(NSString *)str  
  5. {  
  6.     NSLog(@"%@",str);  
  7. }  
  8. //方法前是+号的说明这是一个类方法 这种方法无权访问实例变量  
  9. //这里声明了一个静态方法 无需本类new过也可以调用  
  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 基本的语法就给大家介绍完了, 希望有兴趣的朋友可以和我一起讨论 我们可以一起进步。

小结:Xcode中使用Objective-C基础语法学习教程的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: starming社区
相关推荐

2011-08-05 14:16:47

Objective-C 变量 方法

2015-07-07 10:43:59

Swift语法基础

2011-07-25 11:02:29

Objective-C Xcode 标签

2011-07-25 10:30:41

Objective-C Xcode 重构

2011-07-25 10:14:13

Objective-C Xcode

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2009-09-10 13:54:27

LINQ语法

2010-11-04 16:32:00

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString

2015-07-07 10:58:29

Swift语法高级

2011-08-05 14:03:39

Objective-C 对象 模板

2011-07-06 11:19:45

Objective-C

2011-08-09 15:53:28

2014-04-30 10:16:04

Objective-CiOS语法

2011-08-17 11:15:22

Objective-C语法

2011-08-02 13:16:36

Objective-C 语法 函数

2011-05-11 13:54:08

Objective-C

2014-08-05 10:51:09

Xcode警告Objective-C

2011-07-27 17:41:35

Objective-C Xcode

2011-07-08 13:49:46

Objective-C UUID
点赞
收藏

51CTO技术栈公众号