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)执行
cat /etc/lsb_release
![]() |
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;
}
![]() |
app:$(s)
gcc
`gnustep-config --objc-flags` $(s) -o app
-lgnustep-base
clean:
rm
*.d
rm
app
注意:gcc和rm前的TAB键
![]() |
make s=hello.m
![]() |
./app
![]() |
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里声明不能在类的实现里声明!!!
![]() |
(1)编译
![]() |
![]() |
相关文章推荐
- user_segments和user_objects
- user_segments和user_objects
- CVPR2014 Objectness Estimation at 300fps
- objective-C学习 KVC
- Object-C--->Swift之(七)丰富的函数形参功能
- Objective-C Associated Objects 的实现原理
- 类 Object
- Object C 语法入门
- Scala的object和class的区别
- 【Objective-C】09-空指针和野指针
- Object-C--->Swift之(五)函数入门级讲解
- Objective-C UI之UITableView详解
- objective-c宏定义
- Objective-C 10 NSValue和NSNull使用方法
- 水晶报表设置FiledObject支持HTML格式的数据
- Android ObjectAnimator ValueAnimation学习
- IOS开发基础Object-C( 15)-- block的使用方法
- 初识Objective-C
- Non-Maximum Suppression for Object Detection in Python
- Java不可变对象(Immutable Objects)