I tried using the # char as a key in a NSDictionary and my application simply crashes. I searched for "invalid" key names, could not find the #"#" anywhere. If I use something else than #"#" it all works fine.
I have a list of comanies, I get the first letter of each company and then I am creating a NSMutableDictionary entry containing the first letter as the key and a NSMutableArray as the value.
NSMutableDictionary indexDictionary = [[NSMutableDictionary alloc] init];
// here we have a loop on companyName
{
NSString *fristLetter = [[companyName substringToIndex:1] uppercaseString];
NSMutableArray *arrayOfIndexedCompanies = [indexDictionary valueForKey:firstLetter];
if (arrayOfIndexedCompanies) {
[arrayOfIndexedCompanies addObject:companyName]
}
else {
NSMutableArray *newArray = [NSMutableArray array];
[indexDictionary setObject:newArray forKey:firstLetter];
[newArray addObject:companyName];
}
}
I enabled breakpoint break on throw and it stops at the [indexDictionary valueForKey:firstLetter]... only when firstLetter is a #"#".
I had an if saying:
if ([firstLetter isEqualToString:#"#"]) {
firstLetter = #"A";
}
and this works fine, it places the # starting companies in the A section correctly. If I am letting the firstLetter unchanged (leaving it as #"#") the application will crash.
Also, this is not really my code, I am just trying to fix it, I am not quite familiar with ObjC and Foundation so please be gentle.
Read the documentation :)
Specifically, this bit :
If key does not start with “#”, invokes objectForKey:. If key does start with “#”, strips the “#” and invokes [super valueForKey:] with the rest of the key.
I guess that super valueForKey: isn't happy being called :)
To fix it, just call objectForKey: instead of valueForKey:
Using the string #"#" as a dictionary key works just fine:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"Test 1", #"#", #"Test 2", #"Another #", nil];
NSString *result = [dict objectForKey:#"#"];
NSLog(#"%#", result);
// --> Test 1
You could give a try with #"\u0040"
Related
I have this snipped of code that results in an array with a whole bunch of "<null>" throughout and I need to figure out how to remove them. Obviously after smashing my head against the keyboard I'm asking for some help.
In my .h I have declared:
NSArray *sortedContacts;
NSArray *rawContacts;
And then in .m:
-(void) buildContacts {
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
NSArray *contacts = [addressBook people];
rawContacts=contacts;
NSArray *firstNames = [rawContacts valueForKey:#"First"];
NSArray *lastNames = [rawContacts valueForKey:#"Last"];
NSArray *organization = [rawContacts valueForKey:#"Organization"];
NSMutableArray *fullNames = [NSMutableArray array];
for(int i = 0; i < [firstNames count]; i++)
{
NSString *fullName = [NSString stringWithFormat:#"%# %# %#",
[firstNames objectAtIndex:i],
[lastNames objectAtIndex:i],
[organization objectAtIndex:i]];
[fullNames addObject:fullName];
}
NSMutableArray *fullList = [[NSMutableArray alloc]initWithArray:fullNames];
[fullList removeObjectIdenticalTo: #"<null>"];
sortedContacts = [fullList sortedArrayUsingSelector:#selector(compare:)];
NSLog(#"%#",sortedContacts);
}
I've tried so many things that I just can't see the forest for the trees anymore.
The text <null> is how the singleton instance of NSNull describes itself. That is, it's what -[NSNull description] returns.
In turn, these NSNull objects are getting into your firstNames, lastNames, and organization arrays because that's what Key-Value Coding does when you call -valueForKey: on an array and some of the elements return nil when that message is forwarded on to them with the same key. That is, calling [rawContacts valueForKey:#"First"] causes NSArray to call [element valueForKey:#"First"] for each element in rawContacts and to put the result in the array it builds. But, since an array can't contain nil, if one of those elements returns nil from [element valueForKey:#"First"], an NSNull object is added in its place.
Then, you are formatting the string fullName from the corresponding elements of firstNames, lastNames, and organization. You need to check if any of those elements are NSNull using if ([value isKindOfClass:[NSNull class]]) and handling that. For instance, you might just skip that record. Or you might combine the available fields and leave out any unavailable ones.
In any case, none of the elements of fullList will be #"<null>" because formatting values into #"%# %# %#" can never result in that string. (It might be #"<null> <null> <null>" or something like that, but never just #"<null>".)
A quick look at your code suggests you cannot get any empty strings added to your array, (a) you add elements using:
[fullNames addObject:fullName];
and fullName is created using:
[NSString stringWithFormat:#"%# %# %#" ...
so even if the %#'s get replaced by nothing you'll still have 2 spaces...
Maybe this is why all the things you've tried fail, if you're looking for empty strings you won't find them.
(Addendum: Question now says you're looking for #"<null>", you won't get that either for the same reason - there is at least two spaces in your string.)
The simple answer to removing invalid entries in fullNames is not to add them in the first place. You are adding elements in a loop (for), and conditional logic (e.g. if) inside the loop to determine whether you have something valid to add - however you define "something valid" - and only add an item to fullNames if so.
HTH
I'm not really familiar with the AddressBook framework, however this might be what's causing the confusion:
The values you collect in your arrays firstNames, lastNames and organization can be of type NSString or NSNull. You have to do any null-checking within the for-loop, before the fullName-string is constructed.
Remove this useless line:
[fullList removeObjectIdenticalTo: #"<null>"];
And replace the contents of your for-loop with the following code:
for(int i = 0; i < [firstNames count]; i++)
{
NSString *firstName = [firstNames objectAtIndex:i];
NSString *lastName = [lastNames objectAtIndex:i];
NSString *org = [organization objectAtIndex:i];
NSMutableArray *namesArray = [NSMutableArray array];
if ([firstName isKindOfClass:[NSString class]])
[namesArray addObject:firstName];
if ([lastName isKindOfClass:[NSString class]])
[namesArray addObject:lastName];
if ([org isKindOfClass:[NSString class]])
[namesArray addObject:org];
if (namesArray.count > 0)
[fullNames addObject:[namesArray componentsJoinedByString:#" "]];
}
I get an array from a JSON and I parse it into an NSMutableArray (this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:#"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:#"label"];
NSLog(#"arrayLabel = %#", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(#"string = %#", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:#"%#", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:#"%#", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex: to an NSString. In your case, arrayLabel isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary. Likewise for arrays and NSArray and strings and NSString.
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.
As someone who has some programming experience it pains me to be asking this question. I just started playing around with objective-c a few days ago and I am trying to simply add NSNumber objects to an NSDictionary. The problem is, when I add an NSNumber object with a negative value it seems as if it is being added as a string not an NSNumber.
Here is how I am initializing the dictionary:
testDict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithDouble:-3],#"x",
[NSNumber numberWithDouble:7, #"a",
nil];
I guess I really have two questions, 1.) Is this not how you create an NSNumber object that has a negative value?
2.) When I print out the dictionary I get the following:
NSLog(#"dictionary = %#", self.testDict);
a = 7;
x = "-3";
Why the double quotes around the -3?
You're correct, and everything's fine. That's just the dictionary -description being misleading.
To verify, break on the NSLog() and try (warning: typed on iPhone):
p [testDict objectForKey:#"x"];
It should reveal it to be an NSNumber instance.
#Conrad Shultz is right, it's just an artifact of how the the description method for the NSDictionary prints the dictionary contents (which is what is happening when you pass the dictionary to NSLog)
Another way to verify that everything is really working as expected is to iterate through the dictionary members and print the descriptions of the indivdual objects. Then you can see your negative number description looks like a number rather than a string.
NSDictionary* testDict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithDouble:-3],#"x", [NSNumber numberWithDouble:7], #"a", nil];
NSArray *keys = [testDict allKeys];
for (NSString *key in keys) {
NSLog(#"%# => %#", key, [testDict objectForKey:key]);
}
Console output is:
2012-02-29 12:38:39.544 test10[1055:f803] x => -3
2012-02-29 12:38:39.546 test10[1055:f803] a => 7
I have a fairly lengthy if statement. The if statement examines a string "type" to determine what type of object should be instantiated. Here's a sample...
if ( [type rangeOfString:#"coin-large"].location != NSNotFound )
{
... create large coin ...
mgr = gameLayer.coinLargeMgr;
}
else if ( [type rangeOfString:#"coin-small"].location != NSNotFound )
{
mgr = gameLayer.coinLargeMgr;
}
... more else statements ...
myObject = [mgr getNewObject];
The "else-if" statements continue for other object types which stand at about 20 right now and that number is likely to increase. This works quite well but in terms of maintenance and efficiency I think it could be improved. My leading candidate right now is to create an NSDictionary keyed on the object type string (coin-small, coin-large, etc.) and with the value of the manager object that should be tied to that type. The idea being that this would be a quick look for the type of object I need to create. Not sure this is the best approach, continuing to look at other options but am curious what folks here might have done for a similar problem. Any help/feedback is greatly appreciated.
You can use an NSDictionary filled with ObjC 'Blocks' to do a switch-like statement which executes the desired code. So make a dictionary with your string keys mapped to a block of code to execute when each is found:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
^{ NSLog(#"found key1"); }, #"key1",
^{ NSLog(#"found key2"); }, #"key2",
nil];
You'll probably prepare this dictionary only once at some early stage like in a constructor or a static initializer so that it is ready when your later code executes.
Then instead of your if/else block, slice out the string key from whatever intput you are receiving (or maybe you won't need to slice it, whatever):
NSString *input = ...
NSRange range = ...
NSString *key = [input substringWithRange:range];
And do the (fast) dictionary lookup for the code to execute. Then execute:
void (^myBlock)(void) = [dict objectForKey:key];
myBlock();
The dictionary approach would be easily doable. Assuming the various managers have been boiled down to specific instances when you create the dictionary, it'd be just the same as almost any object-oriented language:
NSDictionary *stringsToManagers =
[NSDictionary dictionaryWithObjectsAndKeys:
#"coin-large", gameLayer.coinLargeMgr,
#"coin-small", gameLayer.coinSmallMgr,
nil];
// this is assuming that type may contain multiple types; otherwise
// just use [stringsToManagers objectForKey:string]
for(NSString *string in [stringsToManagers allKeys])
{
if([type rangeOfString:string].location != NSNotFound)
{
[[stringsToManagers objectForKey:string] addNewObject];
// or get it and store it wherever it should go
}
}
If all the managers do is vend appropriate objects, the more object-oriented approach might be:
NSDictionary *stringsToClasses =
[NSDictionary dictionaryWithObjectsAndKeys:
#"coin-large", [LargeCoin class],
#"coin-small", [SmallCoin class],
nil];
// this is assuming that type may contain multiple types; otherwise
// just use [stringsToManagers objectForKey:string]
for(NSString *string in [stringsToManagers allKeys])
{
if([type rangeOfString:string].location != NSNotFound)
{
id class = [stringsToManagers objectForKey:string];
id newObject = [[class alloc] init];
// this is exactly the same as if, for example, you'd
// called [[LargeCoin alloc] init] after detecting coin-large
// within the input string; you should obviously do something
// with newObject now
}
}
That could save you having to write any managers if your program structure otherwise fits.
What is the difference between objectForKey and valueForKey?
I looked both up in the documentation and they seemed the same to me.
objectForKey: is an NSDictionary method. An NSDictionary is a collection class similar to an NSArray, except instead of using indexes, it uses keys to differentiate between items. A key is an arbitrary string you provide. No two objects can have the same key (just as no two objects in an NSArray can have the same index).
valueForKey: is a KVC method. It works with ANY class. valueForKey: allows you to access a property using a string for its name. So for instance, if I have an Account class with a property accountNumber, I can do the following:
NSNumber *anAccountNumber = [NSNumber numberWithInt:12345];
Account *newAccount = [[Account alloc] init];
[newAccount setAccountNumber:anAccountNUmber];
NSNumber *anotherAccountNumber = [newAccount accountNumber];
Using KVC, I can access the property dynamically:
NSNumber *anAccountNumber = [NSNumber numberWithInt:12345];
Account *newAccount = [[Account alloc] init];
[newAccount setValue:anAccountNumber forKey:#"accountNumber"];
NSNumber *anotherAccountNumber = [newAccount valueForKey:#"accountNumber"];
Those are equivalent sets of statements.
I know you're thinking: wow, but sarcastically. KVC doesn't look all that useful. In fact, it looks "wordy". But when you want to change things at runtime, you can do lots of cool things that are much more difficult in other languages (but this is beyond the scope of your question).
If you want to learn more about KVC, there are many tutorials if you Google especially at Scott Stevenson's blog. You can also check out the NSKeyValueCoding Protocol Reference.
When you do valueForKey: you need to give it an NSString, whereas objectForKey: can take any NSObject subclass as a key. This is because for Key-Value Coding, the keys are always strings.
In fact, the documentation states that even when you give valueForKey: an NSString, it will invoke objectForKey: anyway unless the string starts with an #, in which case it invokes [super valueForKey:], which may call valueForUndefinedKey: which may raise an exception.
Here's a great reason to use objectForKey: wherever possible instead of valueForKey: - valueForKey: with an unknown key will throw NSUnknownKeyException saying "this class is not key value coding-compliant for the key ".
As said, the objectForKey: datatype is :(id)aKey whereas the valueForKey: datatype is :(NSString *)key.
For example:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObject:#"123"],[NSNumber numberWithInteger:5], nil];
NSLog(#"objectForKey : --- %#",[dict objectForKey:[NSNumber numberWithInteger:5]]);
//This will work fine and prints ( 123 )
NSLog(#"valueForKey : --- %#",[dict valueForKey:[NSNumber numberWithInteger:5]]);
//it gives warning "Incompatible pointer types sending 'NSNumber *' to parameter of type 'NSString *'" ---- This will crash on runtime.
So, valueForKey: will take only a string value and is a KVC method, whereas objectForKey: will take any type of object.
The value in objectForKey will be accessed by the same kind of object.
This table represents four differences between objectForKey and valueForKey.
objectForKey
valueForKey
Works on ...
NSDictionary
NSDictionary / KVC
Throws exception
No
Yes (on KVC)
Feed
NSObject's subclass
NSString
Usage on KVC
cannot
can
I'll try to provide a comprehensive answer here. Much of the points appear in other answers, but I found each answer incomplete, and some incorrect.
First and foremost, objectForKey: is an NSDictionary method, while valueForKey: is a KVC protocol method required of any KVC complaint class - including NSDictionary.
Furthermore, as #dreamlax wrote, documentation hints that NSDictionary implements its valueForKey: method USING its objectForKey: implementation. In other words - [NSDictionary valueForKey:] calls on [NSDictionary objectForKey:].
This implies, that valueForKey: can never be faster than objectForKey: (on the same input key) although thorough testing I've done imply about 5% to 15% difference, over billions of random access to a huge NSDictionary. In normal situations - the difference is negligible.
Next: KVC protocol only works with NSString * keys, hence valueForKey: will only accept an NSString * (or subclass) as key, whilst NSDictionary can work with other kinds of objects as keys - so that the "lower level" objectForKey: accepts any copy-able (NSCopying protocol compliant) object as key.
Last, NSDictionary's implementation of valueForKey: deviates from the standard behavior defined in KVC's documentation, and will NOT emit a NSUnknownKeyException for a key it can't find - unless this is a "special" key - one that begins with '#' - which usually means an "aggregation" function key (e.g. #"#sum, #"#avg"). Instead, it will simply return a nil when a key is not found in the NSDictionary - behaving the same as objectForKey:
Following is some test code to demonstrate and prove my notes.
- (void) dictionaryAccess {
NSLog(#"Value for Z:%#", [#{#"X":#(10), #"Y":#(20)} valueForKey:#"Z"]); // prints "Value for Z:(null)"
uint32_t testItemsCount = 1000000;
// create huge dictionary of numbers
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:testItemsCount];
for (long i=0; i<testItemsCount; ++i) {
// make new random key value pair:
NSString *key = [NSString stringWithFormat:#"K_%u",arc4random_uniform(testItemsCount)];
NSNumber *value = #(arc4random_uniform(testItemsCount));
[d setObject:value forKey:key];
}
// create huge set of random keys for testing.
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:testItemsCount];
for (long i=0; i<testItemsCount; ++i) {
NSString *key = [NSString stringWithFormat:#"K_%u",arc4random_uniform(testItemsCount)];
[keys addObject:key];
}
NSDictionary *dict = [d copy];
NSTimeInterval vtotal = 0.0, ototal = 0.0;
NSDate *start;
NSTimeInterval elapsed;
for (int i = 0; i<10; i++) {
start = [NSDate date];
for (NSString *key in keys) {
id value = [dict valueForKey:key];
}
elapsed = [[NSDate date] timeIntervalSinceDate:start];
vtotal+=elapsed;
NSLog (#"reading %lu values off dictionary via valueForKey took: %10.4f seconds", keys.count, elapsed);
start = [NSDate date];
for (NSString *key in keys) {
id obj = [dict objectForKey:key];
}
elapsed = [[NSDate date] timeIntervalSinceDate:start];
ototal+=elapsed;
NSLog (#"reading %lu objects off dictionary via objectForKey took: %10.4f seconds", keys.count, elapsed);
}
NSString *slower = (vtotal > ototal) ? #"valueForKey" : #"objectForKey";
NSString *faster = (vtotal > ototal) ? #"objectForKey" : #"valueForKey";
NSLog (#"%# takes %3.1f percent longer then %#", slower, 100.0 * ABS(vtotal-ototal) / MAX(ototal,vtotal), faster);
}