Elton's Blog

Tag: NSArray

将Array、Dictionary等集合类的序列化和反序列化

by Elton on 2010年02月15日, under iPhone

Objective-C的集合类序列化到文件中或者从文件中反序列化其实很简单,请看下面的示例代码:

  1.  
  2. NSArray *array = [NSArray arrayWithObjects:
  3.     @"Hefeweizen", @"IPA", @"Pilsner", @"Stout", nil];
  4.  
  5. NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  6.   array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA",
  7.   @"hoppy", nil];
  8.  
  9. // 得到documents directory的路径
  10. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  11.   NSUserDomainMask, YES);
  12. if ([paths count] > 0)
  13. {
  14.   // Array的保存路径
  15.   NSString  *arrayPath = [[paths objectAtIndex:0]
  16.       stringByAppendingPathComponent:@"array.out"];
  17.  
  18.   // dictionary的保存路径
  19.   NSString  *dictPath = [[paths objectAtIndex:0]
  20.       stringByAppendingPathComponent:@"dict.out"];
  21.  
  22.   // 保存array
  23.   [array writeToFile:arrayPath atomically:YES];
  24.  
  25.   // 保存dictionary
  26.   [dictionary writeToFile:dictPath atomically:YES];
  27.  
  28.   // 从文件中读取回来
  29.   NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
  30.   NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath];
  31.  
  32.   for (NSString *element in arrayFromFile)
  33.     NSLog(@"Beer: %@", element);
  34.  
  35.   for (NSString *key in dictFromFile)
  36.     NSLog(@"%@ Style: %@", key, [dictionary valueForKey:key]);
  37. }
  38.  

输出如下:

3 Comments :, , , more...

NSArray 和 KVC

by Elton on 2009年11月4日, under Mac

Theocoacao有篇不错的介绍NSArray和KVC的文章,我给粗略翻译了一下。

NSArray的-valueForKey: 有一个不是很显著的特性. 你可以使用它返回一个由tree对象包装的值对象的数组。这句话可能不是很容易理解,看下面的例子就清楚了。

  1.  
  2. NSMutableArray * tree = [NSMutableArray array];
  3. NSDictionary   * p = nil;   // parent
  4. NSDictionary   * c = nil;   // child
  5. NSNumber       * n = nil;   // value
  6. int i;
  7.  
  8. for ( i = 0; i < 10; i++ )
  9. {
  10. n = [NSNumber numberWithInt: i];
  11. c = [NSDictionary dictionaryWithObject:n forKey: @"someKey"];
  12. p = [NSDictionary dictionaryWithObject: c forKey: @"storage"];
  13. [tree addObject: p];
  14. }
  15.  
  16. NSLog (@"%@", tree);
  17. // here’s the important part!
  18. NSArray * justValues;
  19. justValues = [tree valueForKeyPath: @"storage.someKey"];
  20. NSLog (@"%@", justValues);
  21.  

第一个NSLog输出的内容t:

  1.  
  2. NSLog (@"%@", tree);
  3. (
  4. {storage = {someKey = 0; }; },
  5. {storage = {someKey = 1; }; },
  6. {storage = {someKey = 2; }; },
  7. {storage = {someKey = 3; }; },
  8. {storage = {someKey = 4; }; },
  9. {storage = {someKey = 5; }; },
  10. {storage = {someKey = 6; }; },
  11. {storage = {someKey = 7; }; },
  12. {storage = {someKey = 8; }; },
  13. {storage = {someKey = 9; }; }
  14. )
  15.  

第二个NSLog 返回一个数组的值,这个数组是由封装它的tree对象的 @”storage.someKey”:这个keypath提供的:

  1.  
  2. NSArray * justValues;
  3. justValues = [tree valueForKeyPath: @"storage.someKey"];
  4. NSLog (@"%@", justValues);
  5. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  6.  

我发现它真的非常方便,当你使用Core Data要排序一个managed objects对象里面的一个数组的时候。 你可以用一个attribute就把它里面数组的值列出来。.

Leave a Comment :, more...

Objective-C中切分数组

by Elton on 2009年09月11日, under Mac

在很多脚本语言如ruby,python中都有将字符串切分成数组或者将数组元素以某个间隔字符串间隔形成新的数组。 其实NSArray也提供了这样的功能。

使用-componentsSeparatedByString:来切分NSArray。 如:

  1.  
  2. NSString *string = @"white:black:blue:red";
  3. NSArray *aArray = [string componentsSeparatedByString:@":"];
  4.  

用-componentsJoinedByString:来合并NSArray中的各个元素并创建一个新的字符串,如:

  1.  
  2. string = [aArray componentsJoinedByString:@","];
  3.  

这样,上面的数组就中的各个元素就以”,”分割形成一个字符串。

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...