use gnustep objective-c
first app
#import <Foundation/Foundation.h>int main(int argc, const char * argv[])
{NSAutoreleasePool *pool = [NSAutoreleasePool new];NSLog(@"first start");[pool drain];return 0;
}
tech
- 专注于概念,而不是迷失在语言技术细节中
- 编程语言的目的是成为一个更好的程序员; 也就是说,在设计和实现新系统以及维护旧系统方面变得更加有效
the basic structure of objc program has
- header preprocess
- interface
- implementation
- method
- variable
- declare and expression
- comment
use interface and implementation
#import <Foundation/Foundation.h>@interface SampleClass:NSObject
- (void)sampleMethod;
@end@implementation SampleClass
- (void)sampleMethod
{NSLog(@"hello from sample class");
}
@endint main()
{SampleClass *sampleClass = [[SampleClass alloc]init];[sampleClass sampleMethod];return 0;
}
type system
- basic integer set and float set
- enum type
- void type
- derive type include pointer, array, struct, union, function
// so far we still use Foundation
int main()
{NSLog(@"type int takes %d\n",sizeof(int));NSLog(@"Storage size for float : %d , maxval=%f \n", sizeof(float), FLT_MAX);int c_i = 1;NSInteger i = 1;NSLog(@"%d\n",i);
}
support c type variable and c type function
void test()
{printf("this is c type function\n");
}#define DEBUG YES
const int G_error_no = 1;// notice that upper case of const is a good style
int main()
{ test();// cosume the c type functionNSLog(@"err no: %d\n",G_error_no);
}
基本运算符(算术,逻辑,位运算)的支持是和c保持一致,这里比较简单
-
https://www.yiibai.com/objective_c/objective_c_operators.html#article-start
method in objc
- (return_type) method_name:( argumentType1 )argumentName1
joiningArgument2:( argumentType2 )argumentName2 …
joiningArgumentn:( argumentTypen )argumentNamen {
body of the function
}
// skip the class declare and impl
// notice that java style is good style
// and joiningArgument just needed to take its place,but not a real name of argument
-(int)max:(int)num1 secondNumber:(int)num2{if (num1 > num2)return num1;else return num2;
}int main(){// call the method,for example using a classSampleClass *sampleClass = [[SampleClass alloc]init];int ret = [sampleClass max:1 secondNumber:2];printf("%d\n",ret);
}
// 函数参数按值传参和按引用/指针传参是支持的,有机会在深入一遍c语言
block is an instance (error with enable blocks using -fblocks)[notfix]
仅表示单个任务或行为单元而不是方法集合是有意义的(using block),它允许创建不同的代码段,这些代码段可以传递给方法或函数,就像它们是值一样。 块是Objective-C对象,因此它们可以添加到NSArray或NSDictionary等集合中。 它们还能够从封闭范围中捕获值,使其类似于其他编程语言中的闭包或lambda
returntype (^blockName)(argumentType);
returntype (^blockName)(argumentType)= ^{
};
use application.make
#filename must:GNUmakefile
GNUSTEP_MAKEFILES = /usr/share/GNUstep/Makefiles
SRC_DIR = . #this dir is source code directory
include ${GNUSTEP_MAKEFILES}/common.make
APP_NAME = main #name of application
main_OBJC_FILES = $(SRC_DIR)/main.m # a main.m file, if add .m file just add behind, we should have header as well
main_RESOURCE_FILES = # yet I dont know how to use
include ${GNUSTEP_MAKEFILES}/application.make
include ${GNUSTEP_MAKEFILES}/tool.make
include ${GNUSTEP_MAKEFILES}/Master/tool.make
# GNUstep reference may be in `linux mint`: /usr/share/GNUstep/Documentation
- then run make in the makefile folder
code reference
- the above link
Todos
- read the rest post an get this file done