【《Objective-C基础教程 》笔记ch02】(二)Boolean类型及实例

来源:互联网 发布:淘宝网外衣 编辑:程序博客网 时间:2024/04/30 01:41

一、布尔类型

        布尔类型是一种对带符号的字符类型(signed char)的类型定义,使用8位的存储空间。

        通过#define指令把YES定义为1,NO定义为0,都是8位的二进制数。


二、实例——比较两个整数来判断它们是否相同

      1、项目创建过程 点击打开链接

      2、main.m源代码:

#import <Foundation/Foundation.h>/* 比较两个整数看它们是否相同,相同则返回YES,不同则返回NO */BOOL areIntsDifferent(int thing1, int thing2){    if (thing1 == thing2) {        return (NO);    } else {        return (YES);    }}//areIntsDifferent/** 将数值类型的BOOL值映射为便于人们理解的字符串格式 */NSString *boolString(BOOL yesNo){    if (yesNo == NO) {        return (@"NO");    } else {        return (@"YES");    }}//boolStringint main(int argc, const char * argv[]){    BOOL areTheyDifferent;    areTheyDifferent = areIntsDifferent(5, 5);    //使用NSLog()输出任意对象的值时,都会使用%@格式来表示,实质是给对象发送一个description消息    NSLog(@"are %d and %d different? %@",5,5,boolString(areTheyDifferent));        areTheyDifferent = areIntsDifferent(23, 42);    //@表示对c拓展的objective-C    NSLog(@"are %d and %d different? %@",23,42,boolString(areTheyDifferent));        return (0);    }//main

       3、运行效果


0 0