Swift - dictionary with array - get reference to array [duplicate]
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
arrays swift dictionary
marked as duplicate by Martin R
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.
add a comment |
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
arrays swift dictionary
marked as duplicate by Martin R
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 Swiftif (dictKeyVal != nil)
.
– Rakesha Shastri
Nov 22 '18 at 10:17
add a comment |
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
arrays swift dictionary
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
arrays swift dictionary
asked Nov 22 '18 at 10:04
Martin PerryMartin Perry
5,08523064
5,08523064
marked as duplicate by Martin R
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
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 Swiftif (dictKeyVal != nil)
.
– Rakesha Shastri
Nov 22 '18 at 10:17
add a comment |
2
Tip: You should never do this in Swiftif (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
add a comment |
1 Answer
1
active
oldest
votes
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)
Default should be [m]
– Joakim Danielson
Nov 22 '18 at 10:11
@JoakimDanielson nope, that would result indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– 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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Default should be [m]
– Joakim Danielson
Nov 22 '18 at 10:11
@JoakimDanielson nope, that would result indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– 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
add a comment |
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)
Default should be [m]
– Joakim Danielson
Nov 22 '18 at 10:11
@JoakimDanielson nope, that would result indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– 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
add a comment |
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)
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)
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 indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– 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
add a comment |
Default should be [m]
– Joakim Danielson
Nov 22 '18 at 10:11
@JoakimDanielson nope, that would result indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– 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
add a comment |
2
Tip: You should never do this in Swift
if (dictKeyVal != nil)
.– Rakesha Shastri
Nov 22 '18 at 10:17