数据类型和表达式

作者:追风剑情 发布于:2019-2-20 10:07 分类:Objective-C

基础数据类型
类型 实例 NSLog字符
char 'a'、'\n' %c
short int
%hi、%hx、%ho
unsigned short int
%hu、%hx、%ho %hu、%hx、%ho
int 12、-97、0xFFE0、0177 %i、%x、%o
unsigned int 12u、100U、0XFFu %u、%x、%o
long int 12L、-200l、0xffffL %li、%lx、%lo
unsigned long int 12UL、100ul、0xffeeUL %lu、%lx、%lo
long long int 0xe5e5e5e5LL、500ll %lli、%llx、%llo
unsigned long long int 12ull、0xffeeULL %llu、%llx、%llo
float 12.34f、3.1e-5f、0x1.5p10、0x1P-1 %f、%e、%g、%a
double 12.34、3.1e-5、0x.1p3 %f、%e、%g、%a
long double 12.34L、3.1e-5l %Lf、%Le、%Lg
id nil %p
NAN nan 表示不是一个数字,这个字符被定义在系统的头文件math.h中
BOOL YES、NO b1=YES; b2=NO

注意,在整型常量中以0开头表示常量是八进制(基数8)的,以0x开头或(0X)表示它是十六进制(基数16)的,数字0x.1p3表示十六进制浮点常量。此外,前缀f、l(L)、u(U)和ll(LL)用来明确表示常量是float、long、unsigned和long long类型。
id数据类型可存储任何类型的对象。从某种意义说,它是一般对象类型。id类型是Objective-C中十分重要的特性,它是多态和动态绑定的基础。

关系运算符
运算符 含义 例子
== 等于 count == 10
!= 不等于 flag != DONE
< 小于 a < b
<= 小于或等于 low <= high
> 大于 points > POINT_MAX
>= 大于或等于 j >= 0

示例一

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. int integerVar = 100;
  6. float floatingVar = 331.79;
  7. double doubleVar = 8.44e+11;
  8. char charVar = 'W';
  9. NSLog(@"integerVar = %i", integerVar);
  10. NSLog(@"floatingVar = %f", floatingVar);
  11. NSLog(@"doubleVar = %e", doubleVar);
  12. NSLog(@"doubleVar = %g", doubleVar);
  13. NSLog(@"charVar = %c", charVar);
  14. }
  15. return 0;
  16. }

运行测试

9999.png

示例二:计算器类

3333.png


Calculator.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Calculator : NSObject
  4.  
  5. // 累加方法
  6. -(void) setAccumulator: (double) value;
  7. -(void) clear;
  8. -(double) accumulator;
  9. // 算术运算
  10. -(void) add: (double) value;
  11. -(void) subtract: (double) value;
  12. -(void) multiply: (double) value;
  13. -(void) divide: (double) value;
  14.  
  15. @end


Calculator.m

  1. #import "Calculator.h"
  2.  
  3. @implementation Calculator
  4. {
  5. double accumulator;
  6. }
  7. -(void) setAccumulator: (double) value
  8. {
  9. accumulator = value;
  10. }
  11.  
  12. -(void) clear
  13. {
  14. accumulator = 0;
  15. }
  16.  
  17. -(double) accumulator
  18. {
  19. return accumulator;
  20. }
  21.  
  22. -(void) add: (double) value
  23. {
  24. accumulator += value;
  25. }
  26.  
  27. -(void) subtract: (double) value
  28. {
  29. accumulator -= value;
  30. }
  31.  
  32. -(void) multiply: (double) value
  33. {
  34. accumulator *= value;
  35. }
  36.  
  37. -(void) divide: (double) value
  38. {
  39. accumulator /= value;
  40. }
  41.  
  42. @end


main.m

  1. //<>导入系统文件
  2. //""导入本地文件(自己创建的.h)
  3. #import <Foundation/Foundation.h>
  4. #import "Calculator.h"
  5.  
  6. int main(int argc, const char * argv[]) {
  7. @autoreleasepool {
  8. Calculator *deskCalc = [[Calculator alloc] init];
  9. [deskCalc setAccumulator: 100.0];
  10. [deskCalc add: 200.];
  11. [deskCalc divide: 15.0];
  12. [deskCalc subtract: 10.0];
  13. [deskCalc multiply: 5];
  14. NSLog(@"The result is %g", [deskCalc accumulator]);
  15. }
  16. return 0;
  17. }

运行测试

1111.png

示例三:类型转换


  1. #import <Foundation/Foundation.h>
  2. #import "Calculator.h"
  3.  
  4. int main(int argc, const char * argv[]) {
  5. @autoreleasepool {
  6. float f1 = 123.125, f2;
  7. int i1, i2 = -150;
  8. i1 = f1; // 浮点数到整数的转换
  9. NSLog(@"%f assigned to an int produces %i", f1, i1);
  10. f1 = i2; // 整数到浮点数的转换
  11. NSLog(@"%i assigned to a float produces %f", i2, f1);
  12. f1 = i2 / 100; //整数除以整数
  13. NSLog(@"%i divided by 100 produces %f", i2, f1);
  14. f2 = i2 / 100.0; // 整数除以浮点数
  15. NSLog(@"%i divided by 100.0 produces %f", i2, f2);
  16. f2 = (float) i2 / 100; //类型强制转换运算符
  17. NSLog(@"(float) %i divided by 100 produces %f", i2, f2);
  18. //类型转换运算符通常用于将一般id类型的对象转换成特定类的对象。
  19. //将id变量myNumber的值强制类型转换成一个Calculator对象,转换结果赋给变量myCalculator
  20. id myNumber;
  21. Calculator *myCalculator;
  22. myCalculator = (Calculator *) myNumber;
  23. NSLog(@"myNumber %p", myCalculator);
  24. }
  25. return 0;
  26. }


运行测试

33333.png


标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号