手机版
你好,游客 登录 注册
背景:
阅读新闻

iPhone Objective-C 基础语法详解

[日期:2011-07-25] 来源:Linux社区  作者:xys289187120 [字体:大 中 小]

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

在C语言中 我们在控制台输出信息是用printf()   Java语言则是 System.out.println() 而Objective-C  则是用 NSLog();

打开控制台的快捷键为 command + shift + R

iPhone Objective-C 基础语法详解

  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.   
  31. // Implement viewDidLoad to do additional setup after loading the view, typically from a    
  32.   
  33. nib.  
  34. - (void)viewDidLoad  
  35. {  
  36.     [super viewDidLoad];  
  37.       
  38.         //打印一个字符串   
  39.     NSLog(@"only log hello world");   
  40.       
  41.     //字符串相加           
  42.     NSString *str;      
  43.     NSString *str1 = @"plusA ";      
  44.     NSString *str2 = @"+";    
  45.     NSString *str3 = @"plusB";       
  46.     // 把str1 str2 str3 相加后赋值给str %@ 表示是一个对象 这里也可以用 %d  %s 在这里就不一一举例了。          
  47.     str = [NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];           
  48.     //打印出str         
  49.     NSLog(@"string plus %@",str);            
  50.     //self 好比C++ 或者 java 语言中的 this 指针 指向本类 这里调用了本类的 putString方法 将字符串"pass string"作为参数传递了进去      
  51.     [self putString:@"pass string"];              
  52.     //在内存中new了一个MyClass的对象  alloc是在内存中 分配内存  init 则是初始化 这样写 属于规定写法            
  53.     MyClass * myclass = [[MyClass alloc] init];  
  54.     // 用myclass指针调用 类中putclass方法 将字符串 "pass class string"作为参数传递进去        
  55.     [myclass putclass:@"pass class string"];   
  56.     //调用类中静态方法 将字符串"static pass class string "作为参数传递进去       
  57.     [MyClass staticPutClass:@"static pass class string"];  
  58. }  
  59.   
  60.   
  61. - (void)viewDidUnload  
  62. {  
  63.     [super viewDidUnload];  
  64.     // Release any retained subviews of the main view.   
  65.     // e.g. self.myOutlet = nil;   
  66. }  
  67.   
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  69. {  
  70.     // Return YES for supported orientations   
  71.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  72. }  
  73.   
  74. //自己写的类方法输出字符串   
  75. -(void)putString:(NSString *)str  
  76. {  
  77.     NSLog(@"%@",str);  
  78. }  
  79.   
  80.   
  81. @end  

//这个类的声明

  1. #import <UIKit/UIKit.h>   
  2.   
  3. @interface helloWorldViewController : UIViewController {  
  4.       
  5. }  
  6.   
  7. -(void) putString:(NSString*)str;   
  8.   
  9. @end  

MyClass类的实现

  1. #import "MyClass.h"   
  2.   
  3.   
  4. @implementation MyClass  
  5.   
  6. //方法前是-号的说明这是一个实力方法 必需本类new过才能调用   
  7. -(void)putclass:(NSString *)str  
  8. {  
  9.     NSLog(@"%@",str);  
  10. }  
  11. //方法前是+号的说明这是一个类方法 这种方法无权访问实例变量   
  12. //这里声明了一个静态方法 无需本类new过也可以调用   
  13.  +(void)staticPutClass:(NSString *)str{  
  14.     NSLog(@"%@",str);  
  15. }  
  16. @end  

MyClass类的声明

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

这样Objective-C 基本的语法就给大家介绍完了, 希望有兴趣的朋友可以和我一起讨论 我们可以一起进步。

linux
相关资讯       iPhone 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款