NSDictionary initWithObjectsAndKeys

Hi,

What is the difference between initWithObjectsAndKeys and dictionaryWithObjectsAndKeys? I’ve tried dictionaryWithObjectsAndKeys, but don’t know what the other one does.

Thanks,
kel

Hi,

it does the same thing, the latter form

NSDictionary's dictionary()

is a convenience replacement for

NSDictionary's alloc()'s init()'s autorelease()

The autorelease message is only for Objective-C and with ARC unnecessary anyway

Hi Stefan,

When I use this:

on applicationDidFinishLaunching_(aNotification)
 -- main code
 set theDict to current application's NSDictionary's initWithObjectsAndKeys_("value1", "key1", "value2", "key2", missing value)
 set theText to theDict's valueForKey_("key2")
 display dialog (theText as string)
 return
end applicationDidFinishLaunching_

I get an error:

When I change ‘init’ to dictionary it works. I don’t understand what the difference is.

Thanks,
kel

you forget the alloc()

In Objective-C where the syntax comes from

[NSDictionary dictionary];
is more convenient and easier to read than
[[[NSDictionary alloc] init] autorelease];

I am thinking I need to add the alloc()'s after NSDictionary’s?

Edited: ok, got it!

Thanks a lot,
kel

init. needs always alloc

PS: here is a short description

http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/ClassFactoryMethods/ClassFactoryMethods.html

Hi Stefan,

It works!

    on applicationDidFinishLaunching_(aNotification)
        -- main code
        set theDict to current application's NSDictionary's alloc()'s initWithObjectsAndKeys_("value1", "key1", "value2", "key2", missing value)
        set theText to theDict's valueForKey_("key2")
        display dialog (theText as string)
        return
    end applicationDidFinishLaunching_

So, ‘dictionary’ is a shortcut for initialization.

Thanks for the link also.
kel

Hello kel.

This video course is really for iPhone, but it is basically the same. IMHO the best course you can find: Here.

Hi McUsr,

Interesting lecture. I felt like I was back in school on the first day at the start. But, the professor showed a good overview of what needs to be learned by the end. When I was watching the lecture, I was thinking about how peoples learning styles evolve as you grow older. Not actually the learning styles, but how we adapt ourselves to get the maximum out of things. Interesting.

Don’t mind me I’m about to go to bed. I think.

Thanks for the link,
kel