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











up vote
0
down vote

favorite













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 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 at 10:17

















up vote
0
down vote

favorite













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 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 at 10:17















up vote
0
down vote

favorite









up vote
0
down vote

favorite












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 at 10:04









Martin Perry

5,06322962




5,06322962




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 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 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 at 10:17
















  • 2




    Tip: You should never do this in Swift if (dictKeyVal != nil).
    – Rakesha Shastri
    Nov 22 at 10:17










2




2




Tip: You should never do this in Swift if (dictKeyVal != nil).
– Rakesha Shastri
Nov 22 at 10:17






Tip: You should never do this in Swift if (dictKeyVal != nil).
– Rakesha Shastri
Nov 22 at 10:17














2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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 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 at 10:13










  • Sorry, now I get it. Neat solution btw.
    – Joakim Danielson
    Nov 22 at 10:15


















up vote
0
down vote













You can use the following code snippet to achieve this:



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

if var arr = dic[key]{
arr.append("m")
}else{
dic[key] = ["m"]
}





share|improve this answer





















  • OP wants that shortened.
    – Rakesha Shastri
    Nov 22 at 10:16






  • 3




    This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
    – Dávid Pásztor
    Nov 22 at 10:34


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










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 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 at 10:13










  • Sorry, now I get it. Neat solution btw.
    – Joakim Danielson
    Nov 22 at 10:15















up vote
4
down vote



accepted










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 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 at 10:13










  • Sorry, now I get it. Neat solution btw.
    – Joakim Danielson
    Nov 22 at 10:15













up vote
4
down vote



accepted







up vote
4
down vote



accepted






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 at 10:10









Dávid Pásztor

19.7k72547




19.7k72547












  • Default should be [m]
    – Joakim Danielson
    Nov 22 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 at 10:13










  • Sorry, now I get it. Neat solution btw.
    – Joakim Danielson
    Nov 22 at 10:15


















  • Default should be [m]
    – Joakim Danielson
    Nov 22 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 at 10:13










  • Sorry, now I get it. Neat solution btw.
    – Joakim Danielson
    Nov 22 at 10:15
















Default should be [m]
– Joakim Danielson
Nov 22 at 10:11




Default should be [m]
– Joakim Danielson
Nov 22 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 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 at 10:13












Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15




Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15












up vote
0
down vote













You can use the following code snippet to achieve this:



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

if var arr = dic[key]{
arr.append("m")
}else{
dic[key] = ["m"]
}





share|improve this answer





















  • OP wants that shortened.
    – Rakesha Shastri
    Nov 22 at 10:16






  • 3




    This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
    – Dávid Pásztor
    Nov 22 at 10:34















up vote
0
down vote













You can use the following code snippet to achieve this:



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

if var arr = dic[key]{
arr.append("m")
}else{
dic[key] = ["m"]
}





share|improve this answer





















  • OP wants that shortened.
    – Rakesha Shastri
    Nov 22 at 10:16






  • 3




    This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
    – Dávid Pásztor
    Nov 22 at 10:34













up vote
0
down vote










up vote
0
down vote









You can use the following code snippet to achieve this:



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

if var arr = dic[key]{
arr.append("m")
}else{
dic[key] = ["m"]
}





share|improve this answer












You can use the following code snippet to achieve this:



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

if var arr = dic[key]{
arr.append("m")
}else{
dic[key] = ["m"]
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 10:16









nikksindia

936




936












  • OP wants that shortened.
    – Rakesha Shastri
    Nov 22 at 10:16






  • 3




    This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
    – Dávid Pásztor
    Nov 22 at 10:34


















  • OP wants that shortened.
    – Rakesha Shastri
    Nov 22 at 10:16






  • 3




    This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
    – Dávid Pásztor
    Nov 22 at 10:34
















OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16




OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16




3




3




This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34




This won't work, since Dictionary is a struct and hence a value type, so optional binding a specific value of from the Dictionary and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34



Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)