设为首页 加入收藏

TOP

Objective-c学习笔记―― 基础内容
2015-01-22 21:34:45 来源: 作者: 【 】 浏览:77
Tags:Objective-c 学习 笔记 基础 内容

BOOL

首先编写程序:

#import 
  
   

BOOL areIntDifferent(int ver1 , int ver2){
        if(ver1 == ver2){
                return (NO);
        }else{
                return (YES);
        }
}

NSString* boolString(BOOL yesNo){
        if(yesNo == NO){
                return (@"NO");
        }else{
                return (@"YES");
        }
}

int main(int argc , const char *argv[]){
        BOOL isDiff;
        isDiff = areIntDifferent(6,6);
        NSLog(@"are %d and %d different ? %@" , 6,6,boolString(isDiff));
        isDiff = areIntDifferent(29,48);
        NSLog(@"are %d and %d different ? %@" , 29,48,boolString(isDiff));
        return (0);
}

  
然后在相同目录编写GNUMakefile文件
include $(GNUSTEP_ROOT)/common.make

TOOL_NAME = CompareTwoNum
CompareTwoNum_OBJC_FILES = compare.m

include $(GNUSTEP_ROOT)/tool.make

执行命令:

make 编译成功 , 执行文件 ./obj/CompareTwoNum , 输出如下结果:

2014-05-06 05:26:58.665 CompareTwoNum[3277] are 6 and 6 different ? NO
2014-05-06 05:26:58.666 CompareTwoNum[3277] are 29 and 48 different ? YES

注意事项: 1、在areIntDifferent函数中不要使用 以下语句替代函数中的内容

return ver1 - ver2 ;

2、 不要用BOOL 值和 YES 比较 , 和NO比较一定安全。(OBJC中1不等于YES)


变量 + 循环

#import 
  
   

int main(int argc , const char *argv[]){
        int count = 3 ;
        NSLog(@"the number from 0 to %d is : ",count);
        int i ;
        for(i = 0 ; i <= count  ; i++){
                NSLog(@"%d ", i);
        }
        return (0);
}
  
GNUMakefile 文件内容参见上文。


字符串操作 + strlen (求字符串长度函数)

#import 
  
   

int main(int argc , const char * argv[]){
        const char * words[4] = {"hello" , "zhangting" , "latitude" , "longitude"};
        int count = 4 ;
        int i ;
        for(i = 0 ; i < count ; i++){
                NSLog(@"the length of %s is %d" , words[i] , strlen(words[i]));
        }
        return 0;
}

  


文件操作 + strlen

#import 
  
   

int main(int argc , const char * argv[]){
        FILE * rf = fopen("tmp.txt" , "r");
        char word[256];
        while(fgets(word,256,rf)){
                word[strlen(word) -1] = '\0';
                NSLog(@"The length of %s is %d ." , word , strlen(word));
        }
        fclose(rf);
        return (0);
}
  
执行该文件之前需准备好文件 tmp.txt

fgets 会连每行的换行符都读取,这样会占用一个字符的长度,所以使用

word[strlen(word) -1] = '\0';
将该换行符替换为字符串结尾符号。


objective-c 中的消息发送表示

[object say] : 表示向object对象发送say 消息


objective-c 中新术语

方法: 为响应消息而运行的代码。

方法调度程序 :Objective-c 使用的一种用于推测执行什么方法来响应特定的消息。

接口:对象的类应该提供的操作。

实现:使接口正常工作的代码。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言优化实例:循环中减少判断 下一篇Objective-c中的Block(块)详解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: