Objective-C研究院之基础语法(一)

来源:互联网 发布:家有儿女 知乎 编辑:程序博客网 时间:2024/05/17 06:13

原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址: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

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

//这个类的声明

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

MyClass类的实现

view source
01#import "MyClass.h"
02 
03@implementation MyClass
04 
05//方法前是-号的说明这是一个实力方法 必需本类new过才能调用
06-(void)putclass:(NSString *)str
07{
08    NSLog(@"%@",str);
09}
10//方法前是+号的说明这是一个类方法 这种方法无权访问实例变量
11//这里声明了一个静态方法 无需本类new过也可以调用
12 +(void)staticPutClass:(NSString *)str{
13    NSLog(@"%@",str);
14}
15@end

MyClass类的声明

view source
1#import <Foundation/Foundation.h>
2 
3@interface MyClass :NSObject{
4 
5}
6-(void) putclass : (NSString *) str;
7 
8+(void) staticPutClass <img src="http://www.xuanyusong.com/wp-includes/images/smilies/icon_sad.gif"alt=":(" class="wp-smiley"> NSString *) str;
9@end

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


原创粉丝点击