zoukankan      html  css  js  c++  java
  • NSDictionary等基本类型的使用方法

    2)NSNull

    NSNull大概是Cocoa里最简单的类了,只有一个方法

    + (NSNull *) null;

    可以这样添加到集合中

    [contact setObject: [NSNull null]

    forKey: @"home fax machine"];

    访问时:

    id homefax;

    homefax = [contact objectForKey: @"home fax machine"];

    if (homefax == [NSNull null]) {

    // ... no fax machine. rats.

    }

    //[NSNull null]总是返回一样份数值,所以你可以使用“==”讲该值与其他值进行比较……


    22、NSDictionary和NSMutableDictionary

    A) NSDictionary

    字典是关键字和其定义的集合,也被成为散列表或关联数组,使用的是键查询的优化存储方式

    1)创建方法: 使用dictionaryWithObjectsAndKeys:来创建字典

    + (id) dictionaryWithObjectsAndKeys: (id) firstObject, ...;

    使用方法:

    Tire *t1 = [Tire new];

    Tire *t2 = [Tire new];

    Tire *t3 = [Tire new];

    Tire *t4 = [Tire new];

    NSDictionary *tires;

    tires = [NSDictionary dictionaryWithObjectsAndKeys:

    t1, @"front- left", t2, @"front- right",

    t3, @"back- left", t4, @"back- right", nil];

    2)常用方法

    - (id) objectForKey: (id) aKey;

    使用方法:

    Tire *tire = [tires objectForKey: @"back- right"];   //如果没有则会返回nil值

    B) NSMutableDictionary

    1)创建方法:

    可以向类NSMutableDictionary发送dictionary消息

    也可以使用函数+ (id) dictionaryWithCapacity: (unsigned int) numItems;

    2)常用方法

    可以使用setObject:forKey:方法给字典添加元素:

    - (void) setObject: (id) anObject forKey: (id) aKey;

    - (void) removeObjectForKey: (id) aKey;

    使用方法:

    NSMutableDictionary *tires;

    tires = [NSMutableDictionary dictionary];

    [tires setObject: t1 forKey: @"front- left"];

    [tires setObject: t2 forKey: @"front- right"];

    [tires setObject: t3 forKey: @"back- left"];

    [tires setObject: t4 forKey: @"back- right"];

    //若已经存在,则会用新值替换原有的值

    [tires removeObjectForKey: @"back- left"];

    23、不要创建NSString、NSArray或NSDictionary的子类,因为在Cocoa中,许多类实际上是以类簇的方式实现的,即他们是一群隐藏在通用接口之下的与实现相关的类

    24、Foundation实例 //查找文件

    A)使用枚举遍历

    int main (int argc, const char *argv[])

    {

    NSAutoreleasePool *pool;

    pool = [[NSAutoreleasePool alloc] init]; //自动释放池

    NSFileManager *manager; //Cocoa中有很多类都是单实例构架,即只需要一个实例,你真的只需要一个文件管理器

    manager = [NSFileManager defaultManager]; // defaultManager方法创建一个属于我们的NSFileManager对象

    NSString *home;

    home = [@"~" stringByExpandingTildeInPath]; // stringByExpandingTildeInPath方法可将~替换成当前用户的主目录

    NSDirectoryEnumerator *direnum; //NSEnumerator的子类

    direnum = [manager enumeratorAtPath: home]; //创建一个枚举条件

    NSMutableArray *files;

    files = [NSMutableArray arrayWithCapacity: 42]; //把搜索的结果作为文件存储

    NSString *filename;

    while (filename = [direnum nextObject]) {//调用nextObject时,都会返回该目录中的一个文件的另一个路径,也可搜索子目录

    if ([[filename pathExtension]   // pathExtension输出文件的扩展名(去掉了前面的点.)

    isEqualTo: @"jpg"]) {

    [files addObject: filename];

    }

    }

    NSEnumerator *fileenum;

    fileenum = [files objectEnumerator];

    while (filename = [fileenum nextObject]) {

    NSLog (@"%@", filename);

    }

    [pool drain];

    return (0);

    } // main

    B)使用快速遍历

    int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSFileManager *manager;

    manager = [NSFileManager defaultManager];

    NSString *home;

    home = [@"~" stringByExpandingTildeInPath];

    NSMutableArray *files;

    files = [NSMutableArray arrayWithCapacity: 42];

    for (NSString *filename

    in [manager enumeratorAtPath: home]) {

    if ([[filename pathExtension]

    isEqualTo: @"jpg"]) {

    [files addObject: filename];

    }

    }

    for (NSString *filename in files) {

    NSLog (@"%@", filename);

    }

  • 相关阅读:
    JS调用WebService
    C# FTP FtpWebRequest UsePassive 属性
    vs2010 rdlc .net4.0 卸载 Appdomain 时出错。 (异常来自 HRESULT:0x80131015) 解决办法
    DotNetBar RibbonControl控件office2007风格
    C# WinForm RDLC报表不预览直接连续打印
    C# 调用 WebService 连接ORACLE 11g
    C# WinForm程序打印条码 Code39码1
    RDLC报表 在WinForm里运行出现 未能加载文件或程序集microsoft.reportviewer.winforms
    C# 使用 SAP NCO3.0 调用SAP RFC函数接口
    在ui自动化中,如果有多个case在不同的class 下,要全部执行并且要求只启动一次浏览器页面,怎么处理?
  • 原文地址:https://www.cnblogs.com/wengzilin/p/2397549.html
Copyright © 2011-2022 走看看