NSSet 内是无序的;
anyObject 官方解释
The object returned is chosen at the hash table’s convenience—the selection is not guaranteed to be random.
大概意思:从hash 表返回一个最近便捷的对象,不保证随机;
1直接打印anyObject返回值
2遍历字典
//
// main.m
// Getstart
//
// Created by 何晓文 on 15/7/20.
// Copyright © 2015年何晓文. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc,constchar * argv[]) {
NSSet *set = [NSSetsetWithObjects:@"1",@"a",@"2",@"b", @"3",@"c", @"4",@"d", @"5",@"e", @"6",@"f" ,nil];
NSLog(@"anyObject :%@",[setanyObject]);
for (id objin set) {
NSLog(@"---->%@", obj);
}
return 0;
}
2015-07-20 20:10:07.448 Getstart[648:35566] anyObject :2
2015-07-20 20:10:07.448 Getstart[648:35566] ---->2
2015-07-20 20:10:07.448 Getstart[648:35566] ---->a
2015-07-20 20:10:07.448 Getstart[648:35566] ---->3
2015-07-20 20:10:07.448 Getstart[648:35566] ---->b
2015-07-20 20:10:07.449 Getstart[648:35566] ---->4
2015-07-20 20:10:07.449 Getstart[648:35566] ---->c
2015-07-20 20:10:07.449 Getstart[648:35566] ---->5
2015-07-20 20:10:07.449 Getstart[648:35566] ---->d
2015-07-20 20:10:07.449 Getstart[648:35566] ---->6
2015-07-20 20:10:07.449 Getstart[648:35566] ---->e
2015-07-20 20:10:07.449 Getstart[648:35566] ---->f
2015-07-20 20:10:07.449 Getstart[648:35566] ---->1
修改元素后依然
博客探讨了NSSet中anyObject的使用,指出其返回的对象是由哈希表的便利性决定,不保证随机性。通过代码示例展示了直接调用anyObject和遍历NSSet的输出,显示anyObject并不总是返回集合的第一个元素。
1013

被折叠的 条评论
为什么被折叠?



