代码之家  ›  专栏  ›  技术社区  ›  Kepa Santos

目标C在NSMutableDictionary中访问NSMutabbleDictionary

  •  -3
  • Kepa Santos  · 技术社区  · 10 年前

    我是目标C的新手,在访问方面存在严重问题 NSMutableDictionary s

    我有两个对象( Network Beacon ),我想创建一个 NSMutable字典 具有 NSMutable字典 属于 信标 s在里面。

    Network.h
    
    #import <Foundation/Foundation.h>
    
    @interface Network : NSObject{
        NSString *id_network;
        NSString *major;
        NSString *active;
        NSString *name;
        NSString *status;
        NSMutableDictionary *beaconsDictionary;
    }
    
    @property (nonatomic, strong) NSString *id_network;
    @property (nonatomic, strong) NSString *major;
    @property (nonatomic, strong) NSString *active;
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *status;
    @property (nonatomic, strong) NSMutableDictionary *beaconsDictionary;
    @end
    
    Beacon.h
    
    #import <Foundation/Foundation.h>
    
    @interface Beacon : NSObject{
        NSString *id_beacon;
        NSString *major;
        NSString *minor;
        NSString *active;
        NSString *detected;
    }
    
    @property (nonatomic, strong) NSString *id_beacon;
    @property (nonatomic, strong) NSString *major;
    @property (nonatomic, strong) NSString *minor;
    @property (nonatomic, strong) NSString *active;
    @property (nonatomic, strong) NSString *detected;
    
    @end
    

    我可以创建 NSMutable字典 这样地:

        Beacon *beacon = [[Beacon alloc]init];
            beacon.id_beacon=@"1";
            beacon.major=@"1";
            beacon.minor=@"1";
            beacon.active=@"1";
            beacon.detected=@"0";
        NSMutableDictionary *beaconDic = [[NSMutableDictionary alloc]init];
       [beaconDic setObject:beacon forKey:beacon.id_beacon];
    
        Network *net = [[Network alloc]init];
            net.id_network=@"1";
            net.major=@"1";
            net.active=@"1";
            net.name=@"network 1";
            net.status=@"1";
            net.beaconsDictionary=beaconDic;
    
    
        NSMutableDictionary *networkDic = [[NSMutableDictionary alloc]init]; 
      [networkDic setObject:net forKey:net.id_network];
    

    好的,但现在我如何直接访问“检测到”的信标属性并修改它?

    我知道这是一个非常糟糕的例子,但我不知道该怎么做。

    2 回复  |  直到 10 年前
        1
  •  1
  •   DrGodCarl    10 年前

    看起来你必须有一个网络id和一个信标id才能到达你需要去的地方。它看起来像:

    Network *net = networkDic[netId];
    Beacon *beacon = net.beaconsDictionary[beaconId];
    beacon.detected = newDetectedValue;
    

    这适用于任意网络ID和信标ID。如果需要,可以对值进行硬编码。

    编辑: 在示例代码中值得注意的是,您可以使用更现代的字典赋值。而不是 [dictionary setValue:value forKey:key]; ,你可以 dictionary[key] = value; 。当然,这是个人偏好,但你很可能在最近的事情中看到后者,我发现这一点更清楚。

        2
  •  1
  •   Sergey Kalinichenko    10 年前

    你可以得到你的 Network Beacon 通过提供与字典中的键匹配的键,对象返回:

    NSString *nwKey = @"1";
    Network *n = networkDic[nwKey];
    NSDictionary *bDict = n.beaconsDictionary;
    NSString *bnKey = @"1";
    Beacon *b = bDict[bnKey];
    

    注: 这是新语法。这是旧的:

    NSString *nwKey = @"1";
    Network *n = [networkDic objectForKey:nwKey];
    NSDictionary *bDict = n.beaconsDictionary;
    NSString *bnKey = @"1";
    Beacon *b = [bDict objectForKey:bnKey];