您的位置:首页 > 移动开发 > Objective-C

ubuntu下配置Objective-C(2.0)学习…

2016-01-17 16:56 435 查看
1 使用平台, ubuntu12.04

cat /etc/lsb_release


2 安装GNUstep

sudo aptitude install build-essential gobjc
gobjc++ gnustep gnustep-devel libgnustep-base-dev
-y

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

以下是在ubuntu下测试Objective-C非面向对象编程(普通C)

需要注意一点:Objective-C2.0


将“NSAutoreleasePool
*pool = [NSAutoreleasePool alloc] init]; 和 [pool drain];


换成了

“@autoreleasepool
{和}" ,

如果只是想在ubuntu里学习一下Objective-C编程,可以不写NSAutoreleasePool、
[pool drain];这两行代码,也不报错!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3 编写测试用例hello.m(打印两行字符串)

#import

int main(int argc, const char *argv[])

{

NSAutoreleasePool
*pool = [NSAutoreleasePool alloc]init];

NSLog(@"hello

www.jeapdu.com");

NSLog(@"welcome
to IOS Training Center!");

[pool
drain];

return
0;

}


4 编写makefile

app:$(s)

gcc
`gnustep-config --objc-flags` $(s) -o app
-lgnustep-base

clean:

rm
*.d

rm
app

注意:gcc和rm前的TAB键


5 编译测试用例hello.m

make s=hello.m


6 测试可执行文件app

./app


7 清除编译和可执行文件

make clean


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

以下是在ubuntu下测试Objective-C面向对象编程

需要注意一点:成员变量须在类的定义里声明,不能在实现里声明。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

附1 测试一下面向对象编程class.m(通过类实现打印分数 1 / 3)

// Program to work with fractions – class version

#import

//---- @interface section ----

@interface Fraction: NSObject #类方法和成员变量声明

-(void) print;

-(void) setNumerator: (int) n;

-(void) setDenominator: (int) d;

int numerator; #ubuntu下成员必须在此声明,如果是Mac可以在类实现里声明

int denominator;

@end

//---- @implementation section ---- #类方法实现

@implementation Fraction

{

#不能在此处声明类的成员变量,否则编译会报错!

}

-(void) print

{

NSLog
(@"%i/%i", numerator, denominator);

}

-(void) setNumerator: (int) n

{

numerator
= n;

}

-(void) setDenominator: (int) d

{

denominator
= d;

}

@end

//---- program section ----

int main (int argc, char * argv[])

{

Fraction
*myFraction;

// Create an instance of a Fraction

myFraction
= [Fraction alloc];

myFraction
= [myFraction init];

// Set fraction to 1/3

[myFraction
setNumerator: 1];

[myFraction
setDenominator: 3];

// Display the fraction using the print method

NSLog
(@"The value of myFraction is:");

[myFraction
print];

return 0;

}

注意:类成员变量声明须在interfaces里声明不能在类的实现里声明!!!


附2. 测试一下

(1)编译


(2)执行


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: