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.
#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.
#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.
int intVal = 10;
double doubleVal = 3.21;
float floatVal = 9.87;
unsigned long unsignedLongVal = 12345678;
NSInteger nsIntegerVal = 998877;
char charVal = 'a';
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.
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
{
int foo;
}
@property (nonatomic) int foo;
-(void)someMethod;
@end
@interface
identifier is used for define a class. The way of deriving is similar to C++. Useproperty
keyword to indicate a member field. Use-
to define a member method (or funciton) prototype.
This following program tells you how to implement a class.
#import "TestClass.h"
@implementation TestClass
@synthesize foo;
-(void)someMethod
{
NSLog(@"some method got called");
}
@end
implementation
is 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
-
分享到:
相关推荐
Objective-C是苹果公司用于iOS和OS X应用开发的编程语言,由于其与C语言的兼容性以及面向对象的特性,使其成为苹果早期开发者的首选语言。 书中提到了“Developer’s Library”,这是一套专门为编程专业人士设计的...
通过上述分析,可以看出《Programming in Objective-C (4th Edition)》是一本为程序员准备的实用教程,它不仅覆盖了Objective-C语言的基础知识,还结合了Xcode开发工具和iOS 5平台的开发环境,适合初学者和希望提升...
《Programming in Objective-C》是一本专注于Objective-C编程语言的书籍,由Stephen G. Kochan撰写,是开发者图书馆系列的一部分。这本书特别适合那些对iOS开发感兴趣的人阅读。在书中,首先介绍了Objective-C的基础...
综上所述,这本《Programming in Objective-C, 6th Edition》是一本针对苹果操作系统的应用开发者的权威教材,旨在提供Objective-C语言的系统性学习和应用指导。它着重强调了Objective-C编程技能的学习,为开发者...
MATLAB for Photomechanics- A Primer.zip.003
标题:“MATLAB for Photomechanics - A Primer.pdf” 明确指出了本书的主要内容是关于如何运用MATLAB软件来处理光力学问题。这是一本入门级书籍,旨在帮助读者了解并掌握如何使用MATLAB进行光学领域的散斑图像分析...
推荐的C语言教材包括《C程序设计语言》和《C Primer Plus》,它们都是深入学习C语言的经典书籍。 总的来说,学好大学课程不仅要求学术成绩,更需要结合个人兴趣和市场需求,注重实践和技能的培养。合理规划,充分...
《Programming in Objective-C (6th Edition)》是由Stephen G. Kochan编写的,这是一本关于Objective-C编程语言的教科书,该书是Developer’s Library系列的一部分。Developer’s Library系列的书籍旨在为从事日常...
1. **频率域计算**:首先,在每个子通道带宽内选定若干频率点进行计算。 2. **系数导出**:根据这些频率点上的结果,推导出子载波级均衡器的系数。 3. **性能评估**:对所提出的方案进行误码率性能评估,并与SIMO/...
c语言学习笔记!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! c语言学习笔记!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! c语言学习笔记!!!!!!!!!!!...
MATLAB for Photomechanics- A Primer.zip.002
《C Primer Plus》是一本广泛认可的C语言学习教材,其Master版本通常包含了完整的课程内容、练习题解答以及可能的补充材料。这个压缩包“C-Primer-Plus-master”很可能是该书的中文版答案资源,对于正在学习C语言的...
MATLAB for Photomechanics- A Primer.zip.001
《C++ Primer 5 Answers (C++ 11/14)》是针对C++编程语言的学习资源,尤其是对应《C++ Primer》第五版的解答集。这本书的目的是帮助读者解决在学习C++ 11或14标准时遇到的问题。C++ 11和14是C++语言的重要更新,引入...
1. **系统资源检查**:分析服务器的CPU、内存和磁盘I/O,以确定是否满足MySQL的最佳运行环境。 2. **MySQL配置检查**:评估当前的my.cnf配置文件,提出可能的优化建议,如innodb_buffer_pool_size、max_connections...