1

当我初始化我的CLBeaconRegion时,我希望能够添加更多信息,如数组或字符串,以便我可以通过didRangeBeacons-方法接收它。 (主要或次要)向区域添加其他信息。 iBeacons

目前,它看起来像这样:

_advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001"]; 

但我真的想初始化像这样或类似:

_advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001" setArrayOrSomething:myArray]; 

而且还我显然应该能够从这个地区得到这样的信息:

[region getArray]; 

当然,它不一定是这样,只是你有一个想法,我需要什么。

我已经试过

  • 我试图设置/通过objc_setAssociatedObject
  • 得到它我试图通过setValue forKey

回答

1

我想将其设置建议您只使用一个单独的NSDictionary实例,该实例的键名与您在构建CLBeaconRegion时使用的标识符相同。

像这样:

// Make this a class variable, or make it part of a singleton object 
NSDictionary *beaconRegionData = [[NSDictionary alloc] init]; 

// Here is the data you want to attach to the region 
NSMutableArray *myArray = [[[NSMutableArray] alloc] init]; 

// and here is your region 
_advertRegion = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"003-002-001"]; 

// attach your data to the NSDictionary instead 
[beaconRegionData setValue:myArray forKey:_advertRegion.identifier]; 

// and you can get it like this  
NSLog(@"Here is my array: %@", [beaconRegionData valueForKey:_advertRegion.identifier]); 
+0

作为一种简化,CLBeacon符合NSCopying协议,因此可直接据我知道作为字典中的关键。 – allprog