-1

My code gives an error saying

No visible interface for 'NSMutableDictionary' declares the selector 'addObjects:forKey:'

.h

@interface ViewController : UIViewController{
    NSMutableDictionary *credtialsDictionary;
}
-(IBAction)registerlogin;
@end

.m

@implementation ViewController

-(IBAction)registerlogin{

    [credtialsDictionary addObjects:passwordFieldregister.text forKey:usernameFieldregister];
}
@end

I can't figure out why this IBAction isn't letting me add objects to the NSMutableDictionary

jscs
  • 63,694
  • 13
  • 151
  • 195
Danstett14
  • 31
  • 1
  • 6
  • 2
    Have you checked the spec for NSMutableArray to see if `addObjects:forKey:` is defined??? – Hot Licks May 07 '14 at 21:33
  • 1
    @HotLicks, for all the good it would do, seeing as the OP is trying to add objects to a dictionary... – Jonathan. May 07 '14 at 21:40
  • Why would you call a method called `addObject` *`s`* `:` but pass only one object anyways? – jscs May 07 '14 at 21:40
  • @Jonathan. -- Oops!! ;) – Hot Licks May 07 '14 at 21:45
  • 1
    User, two very critical programming skills are learning to find and use reference documentation and learning to actually *read* error messages (and not just interpret them as meaning "Oooh, there's something mysteriously wrong"). You basically flunked this test on both counts. Please try to do better next time. – Hot Licks May 07 '14 at 21:50

3 Answers3

0

Because there is no instance method addObjects:forKey: in an NSMutableDictionary.

The method you are looking for is setObject:forKey:. (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html)

If you say why you thought the method should be addObjects:forKey: I can help you in a more generic way. But in general you want to look in Apple's documentation (like in the link above, generally just google the class name), or in look in the headers, in Xcode you can press Cmd-Shit-O and then type the name of a class and you can open the header from there.

A clue that you've got the wrong method in this case is that addObjects is plural (and forKey isn't) and you're only trying to add one object.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • No it doesn't. No need to be so aggressive towards the OP. It's their first question here and they're 15. Let's try not put people off stackoverflow and programming? – Jonathan. May 07 '14 at 21:46
  • this is going to help me a lot thank you i have never seen this before i would vote it up but my reputation isn't high enough – Danstett14 May 07 '14 at 23:22
  • Danstett14, do you know what an interface, and selector are? If you read through the introduction pages in the Apple Documentation, it will tell what they are and you will be able to understand the error messages, yourself. Asking here wasn't the wrong thing to do, just next time you get an error you can work it out for yourself which is much quicker :) – Jonathan. May 08 '14 at 01:06
0

Perhaps because the method name is setObject:forKey:

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • 1
    It does answer the question, from the context of the question, the method he's looking for isn't `addObjects:forKey:` (which doesn't exist) it's `setObject:forKey:`. – David Berry May 07 '14 at 22:37
  • even if i put set object:forKey: it gives me the same error – Danstett14 May 07 '14 at 23:03
  • No visible interface for 'NSMutableDictionary' declares the selector 'addObject:forKey:' – Danstett14 May 07 '14 at 23:04
  • Change `addObject:forKey:` to `setObject:forKey:` which appears to be the method you're actually looking for. – David Berry May 07 '14 at 23:06
  • thankyou i do not get the error anymore but when i test it i can register a username and password but it won't let me login with the new username and password – Danstett14 May 07 '14 at 23:21
0

There is no such method addObjects:forKey:, and that wouldn't make sense as it's not meaningful to have multiple objects correspond to one key. But if you want to add multiple objects for keys in one call, it's easy enough to add something via a category if you're so inclined. An example:

#import <Foundation/Foundation.h>

@interface NSMutableDictionary (CBVAdditions)

- (void)cbvSetObjects:(NSArray *)objects forKeys:(NSArray *)keys;

@end

@implementation NSMutableDictionary (CBVAdditions)

- (void)cbvSetObjects:(NSArray *)objects forKeys:(NSArray *)keys
{
    NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    [self addEntriesFromDictionary:tempDictionary];
}

@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSMutableDictionary *md = [NSMutableDictionary dictionary];
        [md cbvSetObjects:@[@"obj1", @"obj2"] forKeys:@[@"key1", @"key2"]];
        NSLog(@"md = %@", md);
    }
}

Output:

2014-05-07 15:51:03.056 Untitled[38754:507] md = { key1 = obj1; key2 = obj2; }

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81