第一讲 Objective-C开发入门

来源:互联网 发布:mysql #什么意思 编辑:程序博客网 时间:2024/05/01 04:32

NSLog()函数的使用
使用NSLog()函数来打印“Hello,world!”消息

//导入Foundation框架头文件,自动防止头文件的重复包含#import<Foundation/Foundation.h>int main(){   //新建项目时,生成的自动释放池   @autoreleasepool{   //类似于printf,用于输出数据,@+"C字符串"为Objc字符串   NSLog(@"Hello,world!");   }   return 0;}//程序运行情况如下  :Hello,world!

NSLog()函数的使用
使用NSLog()函数来打印系统当前时间

#import<Foundation/Foundation.h>int main(){   //自动释放池   @autoreleasepool{   NSLog(@"the current date and time is:%@",[NSDate date]);   }   return 0;}//程序运行情况如下  :the current date and time is:2015-08-18 12:16:11 +0000

格式控制符 解释描述信息
%@ Object输出对象,调用对象的description方法
%d,%i signed int 有符号32位整数
%u unsigned int 无符号32位整数
%f float/double
%p pointer 以十六进制数据打印指针数据
%c character ASCll字符
%C unichar
%s C String(bytes)
%S C String(unichar)
%ld,%li long int 长整型
%lld long long有符号64位整数,至少8个字节
%llu unsigned long long
%Lf long double

void testNSLog(){    //%@ : Object    NSString *str = @"aaa";  //oc上面的对象创建只能创建指针,    NSLog(@"NSString:%@",str);    NSNumber *num = [NSNumber numberWithInt:100];  //[类名  方法名:参数];    NSLog(@"NSNumber:%@",num);  //NSSumber is a class!}//结果:aaa,100void TestNSLog(){    //%d signed int    int a = -100;    NSInteger b = -999;    NSLog(@"%d",a);    NSLog(@"%ld",b);  //电脑是64位系统  NSInterger 就是%ld 如果是32位就是%d}//结果:-100,-999void TEstNSLog(){    //%u:unsigned int    unsigned int a = -100;    unsigned int b = 200;    NSLog(@"%u    %u",a,b);  //电脑是64位系统  NSInterger 就是%ld 如果是32位就是%d} //结果:4294967196    200 //%f : float/double    float floatValue = 20.20;    double doubleValue = 50.55;    NSLog(@"float: %f double: %f", floatValue,doubleValue);    //结果:20.200001      50.550000// %p : pointerchar* p = "test pointer"; NSLog(@"pointer : %p", p);//打印指针的地址NSLog(@"char * p : %s", p);//打印字符串内容// %c : char; %C : unicharchar aChar = 'G'; //char,1个字节char bChar = 0x61;// char型 'a'unichar aUnichar = 0x4f60; // L'你' //unichar,占两个字节NSLog(@"char: '%c' '%c'", aChar,bChar); NSLog(@"unichar: %C", aUnichar);//'G''a'      你// %s : C string(bytes); %S : C string(unichar)NSLog(@"C string(bytes) : %s", p);const unichar pUnichar[] = {L'你',L'好', 0x0000}; //声明一个unicode编码格式的变量,L表示unicode,最后的0x0000相当于字符串的结束标识'\0'NSLog(@"C string(unichar) : %S", pUnichar); //unicode编码的字符串使用%S来进行输出// %lld : long long; %llu : unsigned long long; %Lf : long double    long int longint1 = 1213232490;    NSLog(@"longint1 : %ld",longint1);    long long verylong = -12345678901234567;    NSLog(@"long long : %lld", verylong);    unsigned long long uVeryLong = 123456789012345564;    NSLog(@"unsigned long long : %llu", uVeryLong);    long double aLongDouble = 1233242423123.234242342445;    NSLog(@"long double : %Lf", aLongDouble);//函数来打印不同格式的数据
0 0
原创粉丝点击