`
Poechant
  • 浏览: 229675 次
博客专栏
Bebe66e7-3a30-3fc9-aeea-cfa3b474b591
Nginx高性能Web服务...
浏览量:24331
5738817b-23a1-3a32-86de-632d7da73b1e
Cumulus实时媒体服务...
浏览量:22105
社区版块
存档分类
最新评论

Objective-C Primer(1)Get started!

 
阅读更多

Objective-C Primer(1)Get started!

  • Author: Michael Zhong
  • Email: zhongchao.ustc#gmail.com (# -> @)
  • Blog:blog.csdn.net/poechant
  • Date: May 6th, 2012

1 Get started! – Hello World

Before Xcode 4.2, Manual Reference Counting is required.


//
//  main.m
//  ObjectiveCTest
//
//  Created by 超 钟 on 12-5-6.
//  Copyright (c) 2012年 http://blog.csdn.net/poechant. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSDate* date = [[NSDate alloc] init];
    NSLog(@"The time is: %@", date);
    [date release];
    [pool drain];
    return 0;
}

However, from Xcode version 4.2, it’s very easy to handle your memory management and avoid to leak memory. Auto Reference Counting(ARC) is optional when you create a project.


//
//  main.m
//  ObjectiveCTest
//
//  Created by 超 钟 on 12-5-6.
//  Copyright (c) 2012年 http://blog.csdn.net/poechant. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    NSDate* date = [[NSDate alloc] init];
    NSLog(@"The time is: %@", date);
    return 0;
}

Then how to switch ARC and MRC? When you are setting project properties, you will find that there is an option about Apple LLVM compiler 3.0 - language / Objective-C Automatic Reference Counting.

2 Basic types

How to output basic types is always one of the first several things people concerns.


// Variables of basic type
int             intVal          = 10;
double          doubleVal       = 3.21;
float           floatVal        = 9.87;
unsigned long   unsignedLongVal = 12345678;
NSInteger       nsIntegerVal    = 998877;
char            charVal         = 'a';

// Output
NSLog(@"Hello, World!");
NSLog(@"%d", intVal);
NSLog(@"%lf", doubleVal);
NSLog(@"%f", floatVal);
NSLog(@"%ld", unsignedLongVal);
NSLog(@"%ld", nsIntegerVal);
NSLog(@"%c", charVal);
NSLog(@"The time is: %@", date);

As you see, it’s similar to C language. But you should use%@for outputing a string instead of%s.

3 How to define and implement a class?

As we know, Objective-C is an Object-oriented language. Thus, you can make your own class. The following program is a complete one, which shows how to define a class.


//
//  TestClass.h
//  ObjectiveCTest
//
//  Created by 超 钟 on 12-5-6.
//  Copyright (c) 2012年 http://blog.csdn.net/poechant. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TestClass : NSObject
{
    int foo;
}
@property (nonatomic) int foo;
-(void)someMethod;
@end

@interfaceidentifier is used for define a class. The way of deriving is similar to C++. Usepropertykeyword to indicate a member field. Use-to define a member method (or funciton) prototype.

This following program tells you how to implement a class.


//
//  TestClass.m
//  ObjectiveCTest
//
//  Created by 超 钟 on 12-5-6.
//  Copyright (c) 2012年 http://blog.csdn.net/poechant. All rights reserved.
//

#import "TestClass.h"

@implementation TestClass
@synthesize foo;

-(void)someMethod
{
    NSLog(@"some method got called");
}
@end

implementationis used for implementing the class.

4 Try to invoke the method you define in a class

It’s very easy as well.


TestClass *test = [[TestClass alloc] init];
[test someMethod];
[test release];

-

转载请注明来自柳大的CSDN博客:blog.csdn.net/poechant

-

分享到:
评论

相关推荐

    高清彩版 Programming in Objective-C(6th)

    Objective-C是苹果公司用于iOS和OS X应用开发的编程语言,由于其与C语言的兼容性以及面向对象的特性,使其成为苹果早期开发者的首选语言。 书中提到了“Developer’s Library”,这是一套专门为编程专业人士设计的...

    Programming in Objective-C (4th Edition)

    通过上述分析,可以看出《Programming in Objective-C (4th Edition)》是一本为程序员准备的实用教程,它不仅覆盖了Objective-C语言的基础知识,还结合了Xcode开发工具和iOS 5平台的开发环境,适合初学者和希望提升...

    Programming in Objective-C

    《Programming in Objective-C》是一本专注于Objective-C编程语言的书籍,由Stephen G. Kochan撰写,是开发者图书馆系列的一部分。这本书特别适合那些对iOS开发感兴趣的人阅读。在书中,首先介绍了Objective-C的基础...

    Programming in Objective-C, 6th Edition

    综上所述,这本《Programming in Objective-C, 6th Edition》是一本针对苹果操作系统的应用开发者的权威教材,旨在提供Objective-C语言的系统性学习和应用指导。它着重强调了Objective-C编程技能的学习,为开发者...

    MATLAB for Photomechanics- A Primer.zip.003

    MATLAB for Photomechanics- A Primer.zip.003

    MATLAB for Photomechanics- A Primer.pdf

    标题:“MATLAB for Photomechanics - A Primer.pdf” 明确指出了本书的主要内容是关于如何运用MATLAB软件来处理光力学问题。这是一本入门级书籍,旨在帮助读者了解并掌握如何使用MATLAB进行光学领域的散斑图像分析...

    Programming in Objective-C (6th Edition)

    《Programming in Objective-C (6th Edition)》是由Stephen G. Kochan编写的,这是一本关于Objective-C编程语言的教科书,该书是Developer’s Library系列的一部分。Developer’s Library系列的书籍旨在为从事日常...

    MIMO-FBMC primer

    1. **频率域计算**:首先,在每个子通道带宽内选定若干频率点进行计算。 2. **系数导出**:根据这些频率点上的结果,推导出子载波级均衡器的系数。 3. **性能评估**:对所提出的方案进行误码率性能评估,并与SIMO/...

    system-design-primer-master.zip

    2. **架构设计**:选择合适的架构模式,如客户端-服务器(C/S)、微服务、事件驱动等。 3. **数据存储**:考虑数据库的选择,如关系型数据库、NoSQL、键值存储等。 4. **负载均衡**:确保流量均匀分配,提高系统的...

    MATLAB for Photomechanics- A Primer.zip.002

    MATLAB for Photomechanics- A Primer.zip.002

    C-Primer-Plus-master

    《C Primer Plus》是一本广泛认可的C语言学习教材,其Master版本通常包含了完整的课程内容、练习题解答以及可能的补充材料。这个压缩包“C-Primer-Plus-master”很可能是该书的中文版答案资源,对于正在学习C语言的...

    MATLAB for Photomechanics- A Primer.zip.001

    MATLAB for Photomechanics- A Primer.zip.001

    Introduction to Wavelets and Wavelet Transforms -- A Primer

    总之,“Introduction to Wavelets and Wavelet Transforms -- A Primer”这本书结合了理论与实践,通过Matlab代码为初学者提供了一个全面了解和掌握小波变换的入口。通过深入学习和实践,读者将能够运用小波分析...

    cpp-CPrimer5AnswersC1114

    《C++ Primer 5 Answers (C++ 11/14)》是针对C++编程语言的学习资源,尤其是对应《C++ Primer》第五版的解答集。这本书的目的是帮助读者解决在学习C++ 11或14标准时遇到的问题。C++ 11和14是C++语言的重要更新,引入...

Global site tag (gtag.js) - Google Analytics