-2
CLBeaconRegion *region;
NSMutableDictionary *beacons;
....

beacons[region] = beacons;

Is the code above setting beacons key as region to the value of beacons? I thought the key has to be a string in a NSDictionary/NSMutableDictionary?

user1107173
  • 10,334
  • 16
  • 72
  • 117

2 Answers2

3

When you say:

beacons[region] = beacons;

That's the same as:

[beacons setObject:beacons forKey:region];

The first is just syntactical shortcut for second. Either way, it's probably not what you want, since it doesn't usually make sense to set a dictionary key/value pair that points back to the dictionary itself. What are you actually trying to do?

The key in a dictionary doesn't have to be a string, but it does have to conform to the NSCopying protocol, see the definition:

- (void)setObject:(id)anObject
           forKey:(id<NSCopying>)aKey
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • I'm trying to follow the Airlocate project for iBeacons and was kind of confused: https://developer.apple.com/library/ios/samplecode/AirLocate/Introduction/Intro.html Thank you for clarifying. – user1107173 Apr 13 '15 at 23:46
  • `[beacons setObject:beacons forKey:region];` does not make sense. – zaph Apr 13 '15 at 23:46
  • 2
    It technically calls `-setObject:forKeyedSubscript:`, not `-setObject:forKey:` when using the literal syntax, but the signature is the same. – Tim Johnsen Apr 14 '15 at 02:41
0

beacons[region] = beacons; seems to be incorrect as you are adding the beacons dictionary to itself under the key region.

Note that when using key-value coding the key must be a string see Key-Value Coding Fundamentals.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks. I took this code from Apple sample program: https://developer.apple.com/library/ios/samplecode/AirLocate/Introduction/Intro.html – user1107173 Apr 14 '15 at 00:42
  • 1
    No, that statement is not in the linked app. File and line number please? The closest I can find is: `self.rangedRegions[region] = beacons;`. – zaph Apr 14 '15 at 01:22