Object-C学习之NSSet和NSMutableSet
转自:http://blog.csdn.net/likandmydeer/article/details/7939749
一、简介
集合(set)是一组单值对象,它可以是固定的(NSSet)、也可以是可变的(NSMutableSet)。集合可以比较、计算交集、并集,可变集合还可以有查找、添加、删除。
二、常用方法
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];
//集合比较、修改
NSSet *set1=[NSSet setWithObjects:@"s1",@"s2",@"s3",@"s4",nil];
NSMutableSet *set2=[NSMutableSet setWithObjects:@"s1",@"s2",@"s3",@"s4",nil];
NSLog(@"List set1");
for (NSString *element in set1) {
NSLog(@"%@",element);
}
if ([set1 isEqualToSet:set2]) {
NSLog(@"set1 is equal to set set2");
} else {
NSLog(@"set1 is not equal to set set2");
}
if ([set1 containsObject:@"s1"]) {
NSLog(@"set1 contains s1");
} else {
NSLog(@"set1 not contains s1");
}
[set2 addObject:@"s5"];
[set2 removeObject:@"s3"];
NSLog(@"List set2");
for (NSString *element in set2) {
NSLog(@"%@",element);
}
//集合交集、并集
NSMutableSet *set3;
set3=[NSMutableSet setWithObjects:@"s1",@"s3",@"s5",nil];
[set3 intersectSet:set1];
NSLog(@"135 intersectSet 1234");
for (NSString *element in set3) {
NSLog(@"%@",element);
}
set3=[NSMutableSet setWithObjects:@"s1",@"s3",@"s5",nil];
[set3 unionSet:set1];
NSLog(@"135 unionSet 1234");
for (NSString *element in set3) {
NSLog(@"%@",element);
}
[pool drain];
return 0;
}
setWithObjects创建包含给定的对象列表的集合
+ (id)setWithObjects:(id)firstObj ...
isEqualToSet比较两个集合是否相等
- (BOOL)isEqualToSet:(NSSet *)otherSet
containsObject判断给定的对象是否在集合中
- (BOOL)containsObject:(id)anObject
addObject给集合添加一个对象,如果已有这个对象则不会添加
- (void)addObject:(id)object
removeObject删除集合中给定的对象
- (void)removeObject:(id)anObject
intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。
- (void)intersectSet:(NSSet *)otherSet
unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。
- (void)unionSet:(NSSet *)otherSet
三、NSSet的全部方法
Creating a Set
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
+ setWithObjects:count:
+ setWithSet:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
Initializing a Set
– initWithArray:
– initWithObjects:
– initWithObjects:count:
– initWithSet:
– initWithSet:copyItems:
Counting Entries
– count
Accessing Set Members
– allObjects
– anyObject
– containsObject:
– filteredSetUsingPredicate:
– makeObjectsPerformSelector:
– makeObjectsPerformSelector:withObject:
– member:
– objectEnumerator
– enumerateObjectsUsingBlock:
– enumerateObjectsWithOptions:usingBlock:
– objectsPassingTest:
– objectsWithOptions:passingTest:
Comparing Sets
– isSubsetOfSet:
– intersectsSet:
– isEqualToSet:
– valueForKey:
– setValue:forKey:
Creating a Sorted Array
– sortedArrayUsingDescriptors:
Key-Value Observing
– addObserver:forKeyPath:options:context:
– removeObserver:forKeyPath:context:
– removeObserver:forKeyPath:
Describing a Set
– description
– descriptionWithLocale:
四、NSMutableSet的全部方法
Creating a Mutable Set
+ setWithCapacity:
– initWithCapacity:
Adding and Removing Entries
– addObject:
– filterUsingPredicate:
– removeObject:
– removeAllObjects
– addObjectsFromArray:
Combining and Recombining Sets
– unionSet:
– minusSet:
– intersectSet:
– setSet:
Object-C学习之NSSet和NSMutableSet的更多相关文章
- NSSet、NSMutableSet基本用法
NSSet.NSMutableSet基本用法 在Foundation框架中,提供了NSSet类,它是一组单值对象的集合,且NSSet实例中元素是无序,同一个对象只能保存一个. 一.不可变集合NSSet ...
- NSSet和NSMutableSet 确保数据的唯一性--备
NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性.当插入相同的数据时,不会有任何效果.从内部实现来说是hash表,所以可以常数时间内查找一个数据. 1.NSSet的使用 [NSS ...
- Object C学习笔记12-集合
这里讲到的集合是指Set集合,其实Array也是一种类型的集合.在Object C中提供了两个集合类NSSet和NSMutableSet.其实NSSet和NSArray性质一样,都是用于存储对象的. ...
- Object C学习笔记24-关键字总结
学习Object C也有段时间了,学习的过程中涉及到了很多Object C中的关键字,本文总结一下所涉及到的关键字以及基本语法. 1. #import #import <> 从syste ...
- Object C学习笔记22-#define 用法
上一篇讲到了typedef 关键字的使用,可以参考文章 Object C 学习笔记--typedef用法 .而在c中还有另外一个很重要的关键字#define. 一. #define 简介 在C中利用预 ...
- Object C学习笔记21-typedef用法
在上一章的学习过程中遇到了一个关键字typedef,这个关键字是C语言中的关键字,因为Object C是C的扩展同样也是支持typedef的. 一. 基本作用 typedef是C中的关键字,它的主要作 ...
- Object C学习笔记18-SEL,@ selector,Class,@class
本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...
- Object C学习笔记17-动态判断和选择器
当时学习Object C的时被人鄙视了一顿,说使用.NET的思想来学Object C就是狗屎:不过也挺感谢这位仁兄的,这让我学习的时候更加的谨慎.今天的学习笔记主要记录Object C中的动态类型相关 ...
- NSSet、NSMutableSet
NSSet和NSArray功能性质一样,用于存储对象,属于集合:只能添加cocoa对象,基本数据类型需要装箱. NSSet . NSMutableSet是无序的集合,在内存中存储方式是不连续的,而NS ...
随机推荐
- 201871010112-梁丽珍《面向对象程序设计(java)》第八周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- (day46)DOM、BOM、事件
目录 一.BOM (一)定义 (二)window对象 (三)window的子对象 (1)navigator对象 (2)screen对象 (3)history对象 (4)location对象 (5)弹出 ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Linux性能优化实战学习笔记:第十三讲
问题1:性能工具版本太低,导致指标不全 解决方案1: 这是使用 CentOS 的同学普遍碰到的问题.在文章中,我的pidstat 输出里有一个 %wait 指标,代表进程等待 CPU 的时间百分比, ...
- [LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- shell编程题(七)
输出本机创建20000个目录所用的时间 #! /bin/bash time ( ..} ; do mkdir /tmp/nnn$i done ) 运行记得删除 rm -rf /tmp/nnn*
- Npcap环境配置(Winpcap后继者) pcap的一种
Npcap是基于Winpcap和Libpcap的,Winpcap已多年无人维护,其官网也推荐Windows XP之后的用户转移到Npcap上.Npcap基于WINPCAP,Winpcap基于libpc ...
- Mac修改hosts方法
总有各种各样的原因需要修改hosts文件,那么就来简介下怎么修改.terminal中打开hosts: sudo vim /private/etc/hosts 打开文件后I开启插入模式,在最后一行添加你 ...
- numpy 数组增加列,增加行的函数:column_stack,row_stack
def fun_ndarray(): a = [[1,2,7], [-6,-2,-3], [-4,-8,-55] ] b = [3,5,6] a = np.array(a) b = np.array( ...
- 【javascript】判断是否为正整数
function isNormalInteger(str) { var n = Math.floor(Number(str)); return n !== Infinity && St ...