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