导航: 

  • 基本类型 
  • ID
  • 对象类型常见的有
  • 对象类型
  • -NSLog
  • -NSNumber
  • -NSString和NSMutableString
  • -NSArray和NSMutableArray
  • -NSSet和NSMutableSet
  • -NSDictionary和NSMutableDictionary

基本类型:

Objective-C中的基本类型和C语言中的基本类型一样.主要有:int,long,float,double,char,void, bool等.

在Foundation中,也为些数据定义了别名,如:NSInteger为long,CGFloat为double,BOOL等.

Objective-C也可以用C语言的构造类型,如数组、结构体、同用体等。

对于基本类型变量,不需要用指针,也不用手动回收,方法执行结束会自动回收。

ID:

在object-c中,对象标识被作为一个特殊的数据类型:id。这个数据类型定义为引用对象的指针。实际上是指向对象实例变量的指针。

对象类型常见的有:

NSlog

NSString

NSInteger

NSURL

NSImage

NSNumber

NSLog

格式如下

%@对象

%d,%i整数

%u无符整形

%f浮点/双字

%x,%X二进制整数

%zu size_t %p指针

%e浮点/双字

%g浮点/双字

%s C字符串

%*s Pascal字符串

%c 字符

%C unicha

%lld 64位长整数

(long long)%llu无符64位长整数

%Lf 64位双字

NSNumber

NSNumber是Objective-c的数字对象。需求考虑内存释放问题。

1   NSNumber *number = [NSNumber numberWithInt:123];
2   NSLog(@"%i",[number intValue]);
3   NSLog(@"%i",[number retainCount]);

//输出

2010-12-29 16:02:35.040 HelloWorld[4710:a0f] 123

2010-12-29 16:02:35.042 HelloWorld[4710:a0f] 1

NSString和NSMutableString

NSString是不可变字符串(NSContantString),其变量和其本类型一样不需要手动释放(它的retainCount为-1)。

NSString赋值:

NSString *str1 = @"str....";  //(不需要手动释放)
NSString *str2 = [[NSString alloc] initWithString:@"str..."]; //不需要手动释放

因为对NSString赋值,会产生成的对象,所在方法中用NSString作临时对象,也要考虑内存开消问题。

NSMutableString是可变字符串,若用 “[[NSMutableString alloc] init...]”方法初始化,需要考虑手动释放。

 NSString *str = @"this is str...";
NSMutableString *mstr = [NSMutableString stringWithString:str];
str = @"sss";
NSLog(@"%@",mstr);
NSLog(@"%@",str);

输出:

1 this is str...
2 sss

注:因为NSMutableString是NSString的子类,实际应用中很可以把NSMutableString变量赋给NSString。所以若用NSString做类的属性,也会用手动释放的方式:

 //接口文件
@interface TestProperty : NSObject {
NSString *name;
NSInteger myInt;
} @property (copy,nonatomic) NSString *name;
@property NSInteger myInt;
@end

 //实现类
@implementation TestProperty
@synthesize name;
@synthesize myInt; -(void) dealloc{
self.name = nil;
[super dealloc];
} @end

例:

代码

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableString *str1 = [NSMutableString stringWithString:@"this is str"];
NSMutableString *str2 = [NSMutableString stringWithString:str1];
[str2 appendString:@"sss"];
NSLog(@"%@",str1);
NSLog(@"%@",str2);
[pool drain];
return ;
}

//输出
2010-12-30 11:43:13.511 HelloWorld[2119:a0f] this is str
2010-12-30 11:43:13.521 HelloWorld[2119:a0f] this is strsss
可以看出str2不是指向str1的,而是新的对象!!

NSArray和NSMutableArray

NSArray是不可变数组,一般用于保存固定数据。和NSString不同的是,NSArray有retainCount,所以释放问题。

NSMubleArray是变数组,可以直接对其值进行操作。也可考虑释放问题。

NSMubleArray是NSArray的子类。

 NSArray *arr = [NSArray arrayWithObjects:@"Sep",@"Januay",@"",nil];
NSArray *arr_ = [arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%i",[arr retainCount]);
for(NSString *name in arr_){
NSLog(@"%@",name);
}
//输出
2010-12-29 13:36:16.830 HelloWorld[3325:a0f] 1
2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Januay
2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Sep

代码

 NSMutableArray *arr = [NSMutableArray   arrayWithObjects:@"Sep",@"Januay",@"",nil];
[arr sortUsingSelector:@selector(compare:)];
NSLog(@"%i",[arr retainCount]);
for(NSString *name in arr){
NSLog(@"%@",name);
}
//输出
2010-12-29 13:41:34.925 HelloWorld[3415:a0f] 1
2010-12-29 13:41:34.928 HelloWorld[3415:a0f] Januay
2010-12-29 13:41:34.930 HelloWorld[3415:a0f] Sep

NSSet和NSMutableSet

NSSet和NSMutableSet分别是不可变集合和可变集合。集合是一组单值的操作。NSSet和NSMutableSet都需要考虑释放问题。

代码

 NSSet *set = [NSSet setWithObjects:[NSNumber numberWithInt:],@"bb",@"aa",@"bb",@"aa",nil];
for(id *obj in set){
NSLog(@"%@",obj);
}
NSLog(@"%i",[set count]);
NSLog(@"%i",[set retainCount]);

//输出

2010-12-29 13:56:08.397 HelloWorld[3709:a0f] 10

2010-12-29 13:56:08.400 HelloWorld[3709:a0f] aa

2010-12-29 13:56:08.401 HelloWorld[3709:a0f] bb

2010-12-29 13:56:08.401 HelloWorld[3709:a0f] 3

2010-12-29 13:56:08.402 HelloWorld[3709:a0f] 1

NSDictionary和NSMutableDictionary

dictionary是由键-对象对组成的数据集合。NSDictionay和NSMutableDicionary都需要考虑内存释放问题。

代码

 NSDictionary *dict = [NSDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:@"val1",@"val2",nil]
forKeys:[NSArray arrayWithObjects:@"key2",@"key1",nil]];
for(NSString *key in dict){
NSLog(@"%@",[dict objectForKey:key]);
}
NSLog(@"%i",[dict retainCount]);
[pool drain];

//输出

2010-12-29 15:37:42.745 HelloWorld[4085:a0f] val2

2010-12-29 15:37:42.748 HelloWorld[4085:a0f] val1

2010-12-29 15:37:42.749 HelloWorld[4085:a0f] 1

由上面结果可以看出Dicionary是按Key排序的。


Object-c 中的数据类型的更多相关文章

  1. Object C中的数据类型表

    类型 例子 NSLog chars char 'a', '\n'  %c short int   — %hi, %hx, %ho unsigned short int   %hu, %hx, %ho ...

  2. js中获取数据类型

    ES5中,js中数据类型:number.string.boolean.undefined.null.object js中获取数据类型常用的四种方式 实例: var a = 123, b = true, ...

  3. JavaScript 中的数据类型

    Javascript中的数据类型有以下几种情况: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Function,Date,Ar ...

  4. js中的数据类型

    JS中的数据类型: ——数字  (number)NaN ——字符串(string) ——布尔  (boolean)——函数  (function)     也是对象的一种 ——对象  (object) ...

  5. 如何判断js中的数据类型?

    js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...

  6. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  7. [转]如何判断js中的数据类型

    原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...

  8. 数据库中字段类型对应的C#中的数据类型

    数据库中字段类型对应C#中的数据类型: 数据库                 C#程序 int int32 text string bigint int64 binary System.Byte[] ...

  9. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  10. JS中检测数据类型的四种方式及每个方式的优缺点

    //1.typeof 用来检测数据类型的运算符 //->typeof value //->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number". ...

随机推荐

  1. duilib入门简明教程 -- 界面布局(9)

        上一个教程实现的标题栏代码中,并没有看到处理自适应窗口大小的代码,但是窗口大小变化后,按钮的位置会跟着变化,这是因为我们将按钮放到了HorizontalLayout.VerticalLayou ...

  2. CLR via C# 读书笔记-27.计算限制的异步操作(上篇)

    前言 学习这件事情是一个习惯,不能停...另外这篇已经看过两个月过去,但觉得有些事情不总结跟没做没啥区别,遂记下此文 1.CLR线程池基础 2.ThreadPool的简单使用练习 3.执行上下文 4. ...

  3. CentOS7系统更改网卡名为eth0

    centOS7系统更改网卡名为eth0 1.编辑grub参数 [root@localhost ~]# vim /etc/sysconfig/grub 2.在GRUB_CMDLINE_LINUX行中添加 ...

  4. CF|codeforces 280C Game on Tree

    题目链接:戳我 大概题意:给一棵树,然后每次可以删除一个子树,问你期望多少次能把整棵树都删完? 概率和期望是个神仙..我不会 对于这个题,我们要有一个前置知识--期望的线性性,就是说总期望的值等于各个 ...

  5. 二十一、Node.js-EJS 模块引擎

    初识 EJS 模块引擎 我们学的 EJS 是后台模板,可以把我们数据库和文件读取的数据显示到 Html 页面上面.它是一个第三方模块,需要通过 npm 安装https://www.npmjs.com/ ...

  6. windbg符号路径设置

    .sympath C:\Users\leoyin\Desktop\last;SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols  

  7. 973. K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  8. 【OCP|052】OCP换题库,052最新题库及答案整理-第10题

    10.Which two are true about consistent database backups? A) They can only be taken when a RECOVERY C ...

  9. 【OCP|052】iZ0-052最新题库及答案整理-第9题

    9.Which is true about the Automatic Diagnostic Repository (ADR)? A) It includes diagnostic data for ...

  10. Linux 开启路由转发功能

    想让一台Red Hat Enterprise Linux 7开通iptables的nat转发功能 A服务器:192.168.30.20/24 B服务器:192.168.30.1/24,eth0; 19 ...