原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:Objective-C研究院之基础语法(一)
如果想从事iphone开发的话Objective-C这门语言就不得不学会我们都知道C语言是没有面向对象的而Object-C则是ANSIC的一个严格超集它是具有面向对象的特性的由于IPHONE的成功让这门语言现在非常的火热今天笔者为大家介绍一下在xcode中使用Objective-C的基本语法。1.打开mac系统中强大的Xcode软件单击CreateanewXcodeproject创建一个Xcode项目。
2.选择“View-basedApplication”因为只是介绍基本语法所以“View-basedApplication”已经够用了。选择完后点击Next。
3.输入相应的信息后点击Next。
ProductName:指产品名称,可以随意命名。
CompanyIdentifier:公司标识符,一般命名规则为“com.公司名”
BundleIdentifier:指包标识符,用于唯一标识应用程序,默认会根据公司标识符和产品名来组合生成
DeviceFamily:指该应用支持的设备类型,共三个选项:iPhone、iPad、Universal(即iPhone、iPad通用)
IncludeUniteTests:是否包含单元测试代码模板,如果勾选,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
viewsource
02 | //helloWorldViewController.m |
05 | //Createdby宣雨松on11-7-4. |
06 | //Copyright2011年__MyCompanyName__.Allrightsreserved. |
09 | #import"helloWorldViewController.h" |
10 | #import"MyClass.h"//导入新写的类 |
12 | @implementationhelloWorldViewController |
20 | -( void )didReceiveMemoryWarning |
22 | //Releasestheviewifitdoesn'thaveasuperview. |
23 | [superdidReceiveMemoryWarning]; |
25 | //Releaseanycacheddata,images,etcthataren'tinuse. |
28 | #pragmamark-Viewlifecycle |
30 | //ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfroma |
38 | NSLog(@ "onlyloghelloworld" ); |
42 | NSString*str1=@ "plusA" ; |
44 | NSString*str3=@ "plusB" ; |
45 | //把str1str2str3相加后赋值给str%@表示是一个对象这里也可以用%d%s在这里就不一一举例了。 |
46 | str=[NSStringstringWithFormat:@ "%@%@%@" ,str1,str2,str3]; |
48 | NSLog(@ "stringplus%@" ,str); |
49 | //self好比C++或者java语言中的this指针指向本类这里调用了本类的putString方法将字符串"passstring"作为参数传递了进去 |
50 | [selfputString:@ "passstring" ]; |
51 | //在内存中new了一个MyClass的对象alloc是在内存中分配内存init则是初始化这样写属于规定写法 |
52 | MyClass*myclass=[[MyClassalloc]init]; |
53 | //用myclass指针调用类中putclass方法将字符串"passclassstring"作为参数传递进去 |
54 | [myclassputclass:@ "passclassstring" ]; |
55 | //调用类中静态方法将字符串"staticpassclassstring"作为参数传递进去 |
56 | [MyClassstaticPutClass:@ "staticpassclassstring" ]; |
62 | //Releaseanyretainedsubviewsofthemainview. |
63 | //e.g.self.myOutlet=nil; |
66 | -( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
68 | //ReturnYESforsupportedorientations |
69 | return
(interfaceOrientation==UIInterfaceOrientationPortrait); |
73 | -( void )putString:(NSString*)str |
//这个类的声明
viewsource
3 | @interfacehelloWorldViewController:UIViewController{ |
7 | -( void )putString:(NSString*)str; |
MyClass类的实现
viewsource
05 | //方法前是-号的说明这是一个实力方法必需本类new过才能调用 |
06 | -( void )putclass:(NSString*)str |
10 | //方法前是+号的说明这是一个类方法这种方法无权访问实例变量 |
11 | //这里声明了一个静态方法无需本类new过也可以调用 |
12 | +( void )staticPutClass:(NSString*)str{ |
MyClass类的声明
viewsource
1 | #import<Foundation/Foundation.h> |
3 | @interfaceMyClass:NSObject{ |
6 | -( void )putclass:(NSString*)str; |
8 | +( void )staticPutClass<imgsrc= "http://www.xuanyusong.com/wp-includes/images/smilies/icon_sad.gif"
alt= ":(" class = "wp-smiley" >NSString*)str; |
这样Objective-C基本的语法就给大家介绍完了,希望有兴趣的朋友可以和我一起讨论我们可以一起进步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理