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

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)执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值