int main(int argc,const char *argv[])
{
return 0;
}
0代表程序是正常退出,任何非0的返回值代表有错误产生
NSLog输出各种数据类型的表达式
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
char a = 'a';
short int sint = 12;
unsigned short int usint = 22;
int intVar = 32;
unsigned int uintVar = 42;
long int lintVar = 52;
unsigned long int ulongIntVar= 62;
long long int llintVar = 500ll;
unsigned long long int ullintVar = 12ull;
float floatVar = 12.34f;
double doubleVar = 12.34;
long double ldoubleVar = 12.34L;
id idVar = nil;
NSString *name = @"test";
BOOL isTrue = YES;
NSLog(@"bool = %i",isTrue);//布尔值
NSLog(@"String = %@",name);//字符串
NSLog(@"charVar = %c",a);//字符
NSLog(@"shortIntVar: hi:%hi,hx:%hx,ho:%ho",sint,sint,sint);//十进制,十六进制,八进制short int
NSLog(@"unsignedShortIntVar: hu:%hu,hx:%hx,ho:%ho",usint,usint,usint);//十进制,十六进制,八进制unsigned short int
NSLog(@"intValue:i:%i,x:%x,o:%o",intVar,intVar,intVar);//十进制,十六进制,八进制int
NSLog(@"unsigned int Value:u:%u,x:%x,o:%o",uintVar,uintVar,uintVar);//十进制,十六进制,八进制unsigned int
NSLog(@"long int:li:%li,lx:%lx,lo:%lo",lintVar,lintVar,lintVar);//十进制,十六进制,八进制long int
NSLog(@"unsigned long int:lu:%lu,lx:%lx,lo:%lo",ulongIntVar,ulongIntVar,ulongIntVar);//十进制,十六进制,八进制unsigned long int
NSLog(@"long long int:lli:%lli,llx:%llx,llo:%llo",llintVar,llintVar,llintVar);//十进制,十六进制,八进制unsigned long long int
NSLog(@"unsigned long long int:llu:%llu,llx:%llx,llo:%llo",ullintVar,ullintVar,ullintVar);//十进制,十六进制,八进制unsigned long long int
NSLog(@"float:f:%f,e:%e,g:%g,a:%a",floatVar,floatVar,floatVar,floatVar);//float
NSLog(@"double:f:%f,e:%e,g:%g,a:%a",doubleVar,doubleVar,doubleVar,doubleVar);//double
NSLog(@"long double:Lf:%Lf,Le:%Le,Lg:%Lg",ldoubleVar,ldoubleVar,ldoubleVar);//long double
NSLog(@"id:p=%p",idVar);//id
}
return 0;
}
键盘输入的示例
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int n,number,triangularNumber;
NSLog(@"What triangular number do you want?");
scanf("%i",&number);
triangularNumber = 0;
for (n = 1; n<= number; ++n)
{
triangularNumber += n;
}
NSLog(@"Triangular number %i is %i\n",number,triangularNumber);
}
return 0;
}
对象用.符号获得属性,相当于调用其的setter和getter方法
instance.property = value 相当于[instance setProperty:value]
动态类型的一些常用函数
-(BOOL) isKindOfClass: class-object 判断该对象是否是class-object或其子类的实例
-(BOOL) isMemberOfClass:class-object 判断该对象是否是class-object的实例
-(BOOL) respondsToSelector:selector 判断该类是否响应selector方法
+(BOOL) instancesRespondToSelector:selector 判断该类的实例是否响应selector方法
+(BOOL)isSubclassOfClass:class-object 该类是否是class-object的子类
-(id) performSelector: selector 调用selector方法
-(id) performSelector:selector withObject:object 调用selector方法,object为该方法的参数
-(id) performSelector:selector withObject:object1 withObject:object2调用selector方法,object1,object2为该方法的两个参数
异常的例子:
#import <Foundation/Foundation.h>
@interface testClass : NSObject
{
}
@end
@implementation testClass
@end
int main(int argc, const char * argv[])
{
@autoreleasepool{
testClass *tc = [[testClass alloc] init];
@try {
[tc noSuchMethod];
}
@catch (NSException *exception) {
NSLog(@"Caught %@%@",[exception name],[exception reason]);
}
@finally {
}
NSLog(@"Execution continues");
[tc release];
}
return 0;
}
block实例
int main(int argc, const char * argv[])
{
@autoreleasepool{
void (^print_message)(void) =
^(void){
NSLog(@"Programming is fun.");
};
print_message();
//block可以访问当它定义的时候的区域外的变量
int foo = 10;
void (^printFoo)(void) =
^(void)
{
NSLog(@"foo = %i",foo);//这个值是10,不是15
//foo = 20; //这会产生一个编译错误,它不能改变这个变量的值
};
foo = 15;
printFoo();
}
return 0;
}
如果变量foo前面加一个__block,那就可以在block里面改变它的值了
int main(int argc, const char * argv[])
{
@autoreleasepool{
void (^print_message)(void) =
^(void){
NSLog(@"Programming is fun.");
};
print_message();
__block int foo = 10;
void (^printFoo)(void) =
^(void)
{
NSLog(@"foo = %i",foo);
foo = 20;
};
foo = 15;
printFoo();
NSLog(@"foo = %i",foo);
}
return 0;
}
结果为:
foo = 15
foo = 20
结构体的初始化方法
int main(int argc, const char * argv[])
{
@autoreleasepool{
struct date
{
int month;
int day;
int year;
};
struct date today;
today.month = 9;
today.day = 25;
today.year = 2011;
NSLog(@"Today's date %i/%i/%.2i.",today.month,today.day,today.year %100);
struct date t1 = {7,2,2011};
struct date t2 = {7};
struct date t3 = {.month = 7,.day = 2, .year = 2011};
struct date t4 = {.year = 2011};
}
return 0;
}
在ARC内存管理模式下,一般来说都不必自己写dealloc方法,如果必须重写dealloc方法,也不需要调用父类的dealloc方法,编译器会自己填上去