0

Please an help to iterate literal dictionary on cappuccino environment.Thanks

var userDict = @{@"name": @"Jack",@"secondName": @"Buck",@"name":  @"Jacob",@"secondName": @"Smith"};

for (var righe in userDict){

console.log(righe.name + righe.secondName);
}
output NaN
sundsx
  • 588
  • 9
  • 27

1 Answers1

1

I'd probably do something like this:

for (var key in [userDict allKeys])
{
    console.log(key, userDict[key]);
}

But your dictionary looks wrong; this:

@{
    @"name":         @"Jack",
    @"secondName":   @"Buck",
    @"name":         @"Jacob",
    @"secondName":   @"Smith"
};

Will overwrite the name and secondName indices and result in:

@{
    @"name":         @"Jacob",
    @"secondName":   @"Smith"
};

You probably wanted a CPArray of CPDictionary:

var users = [
    @{
        @"name":         @"Jacob",
        @"secondName":   @"Smith"
    },
    @{
        @"name":         @"Jacob",
        @"secondName":   @"Smith"
    }
];

Then if you loop over users; you get one user dictionary for each step in the loop, and you can address its ' indices (properties). Since both CPArray and CPDictionary are tollfree-bridged to their native javascript counterparts, you can still do this:

for (var ix = 0; ix < users.length; ix ++)
{
   var user = users[ix];
   console.log(user.name, user.secondName);
}

Hope this helps.

Kris
  • 40,604
  • 9
  • 72
  • 101
  • This looking good! It's incredibly charming to look objective-c and js mixed. As default I would write user.count ; ) I'm a little confused, but I think that soon I can bring my web projects. thank you. Last question: there is an autocompletation ide for Cappuccino ? thank you agin – sundsx Dec 06 '16 at 17:01
  • @sundsx: There are some text editor extensions out there that provide limited autocompletion but I'm not aware of any full "intellisense" like IDE at present. Personally I mostly use textmate (http://www.macromates.com) and atom (http://atom.io/) they both have limited support. – Kris Dec 07 '16 at 10:03