Need help adding arrays to dictionary - cocoa

I am following this tutorial: https://www.youtube.com/watch?v=4_0Ao11zrtM

I am trying to make a login box similar to the one in the video, but for a cocoa app on OS X. I am fairly new at objective-c, and this is my first app.


Here is the current code for my LoginController.m file:


#import "LoginController.h"
@implementation LoginController
- (void)awakeFromNib {
    [prodigyLabel setTextColor:[NSColor orangeColor]];
    [studioLabel setTextColor:[NSColor darkGrayColor]];
    loginDictionary = [[NSDictionary alloc] initWithObjects:@"password" forKeys:@"username"];
}
- (IBAction)loginButtonPressed:(id)sender {

}
@end

The loginDictionary part was modified because I looked online and thought it might help when I found it on a different site, hence it says NSDictionary alloc.

It originally said:

loginDictionary = [[NSArray alloc] initWithObjects:@"password", nil];

But there is no option for "forKeys" as seen in the tutorial.


I keep getting error:

Incompatible pointer types sending 'NSString *' to parameter of type 'NSArray *'

Please help me. I am trying to make the dictionary and add the usernames and passwords to it like in the tutorial. What am I doing wrong??

Answered by jewelz in 17835022

If you want to add the usernames and passwords to your dictionary, the dictionary shoud be mutable , you can do like this:

loginDictionary = [NSMutableDictionary dictionary];


[loginDictionary setObject:@"your username" forKey:@"username"];

[loginDictionary setObject:@"your password" forKey:@"password"];

the loginDictionary must be a NSMutableDictionary.

Accepted Answer

If you want to add the usernames and passwords to your dictionary, the dictionary shoud be mutable , you can do like this:

loginDictionary = [NSMutableDictionary dictionary];


[loginDictionary setObject:@"your username" forKey:@"username"];

[loginDictionary setObject:@"your password" forKey:@"password"];

the loginDictionary must be a NSMutableDictionary.

loginDictionary = [[NSDictionary alloc] initWithObjects:@"password" forKeys:@"username"];


The line above is wrong, look at the type of the parameters, use Xcode > Help > Documentation and API reference (search on NSDictionary), they should be NSArray not NString, as your error message already tells you.


This is one valid way to write the same line:


loginDictionary = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObject: @"password"]

forKeys: [NSArray arrayWithObject: @"username"]];


Using object literals makes this much more compact (also note the more natural key-value order):


loginDictionary = @{

@"username" : @"password"

};

Need help adding arrays to dictionary - cocoa
 
 
Q