`

Objective-C 基础语法log打印那些事儿(一)

 
阅读更多

Objective-C 基础语法详解

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




1.打开mac系统中强大的Xcode软件 单击Create a new Xcode project 创建一个Xcode项目。



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



3.输入相应的信息后点击Next。


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

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

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

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

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



这样 我们的第一个项目就创建好了,接下来开始为大家介绍 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. //Createdby宣雨松on11-7-4.
  6. //Copyright2011年__MyCompanyName__.Allrightsreserved.
  7. //
  8. #import"helloWorldViewController.h"
  9. #import"MyClass.h"//导入新写的类
  10. @implementationhelloWorldViewController
  11. -(void)dealloc
  12. {
  13. [superdealloc];
  14. }
  15. -(void)didReceiveMemoryWarning
  16. {
  17. //Releasestheviewifitdoesn'thaveasuperview.
  18. [superdidReceiveMemoryWarning];
  19. //Releaseanycacheddata,images,etcthataren'tinuse.
  20. }
  21. #pragmamark-Viewlifecycle
  22. //ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfroma
  23. nib.
  24. -(void)viewDidLoad
  25. {
  26. [superviewDidLoad];
  27. //打印一个字符串
  28. NSLog(@"onlyloghelloworld");
  29. //字符串相加
  30. NSString*str;
  31. NSString*str1=@"plusA";
  32. NSString*str2=@"+";
  33. NSString*str3=@"plusB";
  34. //把str1str2str3相加后赋值给str%@表示是一个对象这里也可以用%d%s在这里就不一一举例了。
  35. str=[NSStringstringWithFormat:@"%@%@%@",str1,str2,str3];
  36. //打印出str
  37. NSLog(@"stringplus%@",str);
  38. //self好比C++或者java语言中的this指针指向本类这里调用了本类的putString方法将字符串"passstring"作为参数传递了进去
  39. [selfputString:@"passstring"];
  40. //在内存中new了一个MyClass的对象alloc是在内存中分配内存init则是初始化这样写属于规定写法
  41. MyClass*myclass=[[MyClassalloc]init];
  42. //用myclass指针调用类中putclass方法将字符串"passclassstring"作为参数传递进去
  43. [myclassputclass:@"passclassstring"];
  44. //调用类中静态方法将字符串"staticpassclassstring"作为参数传递进去
  45. [MyClassstaticPutClass:@"staticpassclassstring"];
  46. }
  47. -(void)viewDidUnload
  48. {
  49. [superviewDidUnload];
  50. //Releaseanyretainedsubviewsofthemainview.
  51. //e.g.self.myOutlet=nil;
  52. }
  53. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  54. {
  55. //ReturnYESforsupportedorientations
  56. return(interfaceOrientation==UIInterfaceOrientationPortrait);
  57. }
  58. //自己写的类方法输出字符串
  59. -(void)putString:(NSString*)str
  60. {
  61. NSLog(@"%@",str);
  62. }
  63. @end


//这个类的声明

  1. #import<UIKit/UIKit.h>
  2. @interfacehelloWorldViewController:UIViewController{
  3. }
  4. -(void)putString:(NSString*)str;
  5. @end
MyClass类的实现

  1. #import"MyClass.h"
  2. @implementationMyClass
  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. @interfaceMyClass:NSObject{
  3. }
  4. -(void)putclass:(NSString*)str;
  5. +(void)staticPutClass:(NSString*)str;
  6. @end


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics