Swift - dictionary with array - get reference to array [duplicate]












0
















This question already has an answer here:




  • Swiftier Swift for 'add to array, or create if not there…'

    1 answer




I currently have this code:



var dic = [String: [String]]() 

if (dic.index(forKey: key) != nil){
dic[key]?.append(m)
}
else {
dic[key] = [m]
}


However, in dic.index(forKey: key) and dic[key]?.append(m) I calculate the key twice.



Is there a possibility to do something like this?:



var dictKeyVal = &dic[key]
if (dictKeyVal != nil) {
dictKeyVal?.append(m)
}
else {
dic[key] = [m]
}


where I get the reference to array at key or nil if there is no key










share|improve this question













marked as duplicate by Martin R arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 10:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2





    Tip: You should never do this in Swift if (dictKeyVal != nil).

    – Rakesha Shastri
    Nov 22 '18 at 10:17


















0
















This question already has an answer here:




  • Swiftier Swift for 'add to array, or create if not there…'

    1 answer




I currently have this code:



var dic = [String: [String]]() 

if (dic.index(forKey: key) != nil){
dic[key]?.append(m)
}
else {
dic[key] = [m]
}


However, in dic.index(forKey: key) and dic[key]?.append(m) I calculate the key twice.



Is there a possibility to do something like this?:



var dictKeyVal = &dic[key]
if (dictKeyVal != nil) {
dictKeyVal?.append(m)
}
else {
dic[key] = [m]
}


where I get the reference to array at key or nil if there is no key










share|improve this question













marked as duplicate by Martin R arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 10:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2





    Tip: You should never do this in Swift if (dictKeyVal != nil).

    – Rakesha Shastri
    Nov 22 '18 at 10:17
















0












0








0









This question already has an answer here:




  • Swiftier Swift for 'add to array, or create if not there…'

    1 answer




I currently have this code:



var dic = [String: [String]]() 

if (dic.index(forKey: key) != nil){
dic[key]?.append(m)
}
else {
dic[key] = [m]
}


However, in dic.index(forKey: key) and dic[key]?.append(m) I calculate the key twice.



Is there a possibility to do something like this?:



var dictKeyVal = &dic[key]
if (dictKeyVal != nil) {
dictKeyVal?.append(m)
}
else {
dic[key] = [m]
}


where I get the reference to array at key or nil if there is no key










share|improve this question















This question already has an answer here:




  • Swiftier Swift for 'add to array, or create if not there…'

    1 answer




I currently have this code:



var dic = [String: [String]]() 

if (dic.index(forKey: key) != nil){
dic[key]?.append(m)
}
else {
dic[key] = [m]
}


However, in dic.index(forKey: key) and dic[key]?.append(m) I calculate the key twice.



Is there a possibility to do something like this?:



var dictKeyVal = &dic[key]
if (dictKeyVal != nil) {
dictKeyVal?.append(m)
}
else {
dic[key] = [m]
}


where I get the reference to array at key or nil if there is no key





This question already has an answer here:




  • Swiftier Swift for 'add to array, or create if not there…'

    1 answer








arrays swift dictionary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 10:04









Martin PerryMartin Perry

5,08523064




5,08523064




marked as duplicate by Martin R arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 10:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Martin R arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 10:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Tip: You should never do this in Swift if (dictKeyVal != nil).

    – Rakesha Shastri
    Nov 22 '18 at 10:17
















  • 2





    Tip: You should never do this in Swift if (dictKeyVal != nil).

    – Rakesha Shastri
    Nov 22 '18 at 10:17










2




2





Tip: You should never do this in Swift if (dictKeyVal != nil).

– Rakesha Shastri
Nov 22 '18 at 10:17







Tip: You should never do this in Swift if (dictKeyVal != nil).

– Rakesha Shastri
Nov 22 '18 at 10:17














1 Answer
1






active

oldest

votes


















4














You can simply use a default value for the subscript and your whole code will be simplified to this:



var dic = [String: [String]]()
dic[key, default: ].append(m)





share|improve this answer
























  • Default should be [m]

    – Joakim Danielson
    Nov 22 '18 at 10:11











  • @JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

    – Dávid Pásztor
    Nov 22 '18 at 10:13











  • Sorry, now I get it. Neat solution btw.

    – Joakim Danielson
    Nov 22 '18 at 10:15


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You can simply use a default value for the subscript and your whole code will be simplified to this:



var dic = [String: [String]]()
dic[key, default: ].append(m)





share|improve this answer
























  • Default should be [m]

    – Joakim Danielson
    Nov 22 '18 at 10:11











  • @JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

    – Dávid Pásztor
    Nov 22 '18 at 10:13











  • Sorry, now I get it. Neat solution btw.

    – Joakim Danielson
    Nov 22 '18 at 10:15
















4














You can simply use a default value for the subscript and your whole code will be simplified to this:



var dic = [String: [String]]()
dic[key, default: ].append(m)





share|improve this answer
























  • Default should be [m]

    – Joakim Danielson
    Nov 22 '18 at 10:11











  • @JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

    – Dávid Pásztor
    Nov 22 '18 at 10:13











  • Sorry, now I get it. Neat solution btw.

    – Joakim Danielson
    Nov 22 '18 at 10:15














4












4








4







You can simply use a default value for the subscript and your whole code will be simplified to this:



var dic = [String: [String]]()
dic[key, default: ].append(m)





share|improve this answer













You can simply use a default value for the subscript and your whole code will be simplified to this:



var dic = [String: [String]]()
dic[key, default: ].append(m)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 10:10









Dávid PásztorDávid Pásztor

21.4k82749




21.4k82749













  • Default should be [m]

    – Joakim Danielson
    Nov 22 '18 at 10:11











  • @JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

    – Dávid Pásztor
    Nov 22 '18 at 10:13











  • Sorry, now I get it. Neat solution btw.

    – Joakim Danielson
    Nov 22 '18 at 10:15



















  • Default should be [m]

    – Joakim Danielson
    Nov 22 '18 at 10:11











  • @JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

    – Dávid Pásztor
    Nov 22 '18 at 10:13











  • Sorry, now I get it. Neat solution btw.

    – Joakim Danielson
    Nov 22 '18 at 10:15

















Default should be [m]

– Joakim Danielson
Nov 22 '18 at 10:11





Default should be [m]

– Joakim Danielson
Nov 22 '18 at 10:11













@JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

– Dávid Pásztor
Nov 22 '18 at 10:13





@JoakimDanielson nope, that would result in dic[key] = [m,m] instead of the desired dic[key] = [m] in case there was no value for key.

– Dávid Pásztor
Nov 22 '18 at 10:13













Sorry, now I get it. Neat solution btw.

– Joakim Danielson
Nov 22 '18 at 10:15





Sorry, now I get it. Neat solution btw.

– Joakim Danielson
Nov 22 '18 at 10:15



Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen