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
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 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 |
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
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 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 at 10:17
add a comment |
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
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 at 10:04
Martin Perry
5,06322962
5,06322962
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 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 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 at 10:17
add a comment |
2
Tip: You should never do this in Swiftif (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
add a comment |
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)
Default should be [m]
– Joakim Danielson
Nov 22 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 at 10:13
Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15
add a comment |
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"]
}
OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16
3
This won't work, sinceDictionary
is astruct
and hence a value type, so optional binding a specific value of from theDictionary
and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34
add a comment |
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)
Default should be [m]
– Joakim Danielson
Nov 22 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 at 10:13
Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15
add a comment |
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)
Default should be [m]
– Joakim Danielson
Nov 22 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 at 10:13
Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15
add a comment |
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)
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 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 indic[key] = [m,m]
instead of the desireddic[key] = [m]
in case there was no value forkey
.
– Dávid Pásztor
Nov 22 at 10:13
Sorry, now I get it. Neat solution btw.
– Joakim Danielson
Nov 22 at 10:15
add a comment |
Default should be [m]
– Joakim Danielson
Nov 22 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 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
add a comment |
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"]
}
OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16
3
This won't work, sinceDictionary
is astruct
and hence a value type, so optional binding a specific value of from theDictionary
and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34
add a comment |
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"]
}
OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16
3
This won't work, sinceDictionary
is astruct
and hence a value type, so optional binding a specific value of from theDictionary
and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34
add a comment |
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"]
}
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"]
}
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, sinceDictionary
is astruct
and hence a value type, so optional binding a specific value of from theDictionary
and modifying that won't modify the original value associated with the key.
– Dávid Pásztor
Nov 22 at 10:34
add a comment |
OP wants that shortened.
– Rakesha Shastri
Nov 22 at 10:16
3
This won't work, sinceDictionary
is astruct
and hence a value type, so optional binding a specific value of from theDictionary
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
add a comment |
2
Tip: You should never do this in Swift
if (dictKeyVal != nil)
.– Rakesha Shastri
Nov 22 at 10:17