Skip to content

Instantly share code, notes, and snippets.

@iT-Boyer
Last active January 23, 2017 08:39
Show Gist options
  • Save iT-Boyer/ecba275d5e4404678354 to your computer and use it in GitHub Desktop.
Save iT-Boyer/ecba275d5e4404678354 to your computer and use it in GitHub Desktop.
NSAttributedString *attrString; // from previous code
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
法一
据说是开发文档中的方法,未考证。
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
/* code that uses the returned key */
}
法二
@未解提供,也是最方便用的最多的一种方法,一般用这种就可以。
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
for (NSString *key in dict) {
NSLog(@"key: %@ value: %@", key, dict[key]);
}
法三
这个方法是最笨拙的一个,一般遍历NSDictionary也不会这么用,姑且记在这里,怎么也算是一种方法嘛~
- (void)describeDictionary:(NSDictionary *dict)
{
NSArray *keys;
int i, count;
id key, value;
//获取字典中所有的Key
keys = [dict allKeys];
count = [keys count];
for (i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [dict objectForKey: key];
NSLog (@"Key: %@ for value: %@", key, value);
}
}
//方法4:
//得到词典中所有KEY值
NSEnumerator * enumeratorKey = [dictionary keyEnumerator];
//快速枚举遍历所有KEY的值
for (NSObject *object in enumeratorKey) {
NSLog(@"遍历KEY的值: %@",object);
}
//得到词典中所有Value值
NSEnumerator * enumeratorValue = [dictionary objectEnumerator];
//快速枚举遍历所有Value的值
for (NSObject *object in enumeratorValue) {
NSLog(@"遍历Value的值: %@",object);
}
方法5:
[dictionary enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
}];
NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表,所以可以常数时间内查找一个数据。
NSAttributedString *attrString; // from previous code
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length])
documentAttributes:options
error:nil];
NSString *htmlString = [[NSString alloc] initWithData:htmlData
encoding:NSUTF8StringEncoding];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment