Create array of n objects (UILabel) and map them with another array of String
up vote
1
down vote
favorite
I'm facing two problems. I want to create an array of UILabel
.
I tried:
private var weekdayLabels = [DayLabel](repeating: DayLabel(), count: 7)
,
but it uses the same instance of DayLabel()
.
I ended up using this other, much uglier solution:
(0...6).forEach { _ in
weekdayLabels.append(DayLabel())
}
Is there a way I can make [DayLabel](repeating: DayLabel(), count: 7)
work?
The second problem comes while mapping an array of String
with the weekday names to my array of [UILabel]
:
weekdayLabels.enumerated().forEach {
$0.element.text = weekdayNames[$0.offset]
}
I believe there must be a better way to map the String
to the UILabel().text
. Am I right?
ios swift functional-programming
add a comment |
up vote
1
down vote
favorite
I'm facing two problems. I want to create an array of UILabel
.
I tried:
private var weekdayLabels = [DayLabel](repeating: DayLabel(), count: 7)
,
but it uses the same instance of DayLabel()
.
I ended up using this other, much uglier solution:
(0...6).forEach { _ in
weekdayLabels.append(DayLabel())
}
Is there a way I can make [DayLabel](repeating: DayLabel(), count: 7)
work?
The second problem comes while mapping an array of String
with the weekday names to my array of [UILabel]
:
weekdayLabels.enumerated().forEach {
$0.element.text = weekdayNames[$0.offset]
}
I believe there must be a better way to map the String
to the UILabel().text
. Am I right?
ios swift functional-programming
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm facing two problems. I want to create an array of UILabel
.
I tried:
private var weekdayLabels = [DayLabel](repeating: DayLabel(), count: 7)
,
but it uses the same instance of DayLabel()
.
I ended up using this other, much uglier solution:
(0...6).forEach { _ in
weekdayLabels.append(DayLabel())
}
Is there a way I can make [DayLabel](repeating: DayLabel(), count: 7)
work?
The second problem comes while mapping an array of String
with the weekday names to my array of [UILabel]
:
weekdayLabels.enumerated().forEach {
$0.element.text = weekdayNames[$0.offset]
}
I believe there must be a better way to map the String
to the UILabel().text
. Am I right?
ios swift functional-programming
I'm facing two problems. I want to create an array of UILabel
.
I tried:
private var weekdayLabels = [DayLabel](repeating: DayLabel(), count: 7)
,
but it uses the same instance of DayLabel()
.
I ended up using this other, much uglier solution:
(0...6).forEach { _ in
weekdayLabels.append(DayLabel())
}
Is there a way I can make [DayLabel](repeating: DayLabel(), count: 7)
work?
The second problem comes while mapping an array of String
with the weekday names to my array of [UILabel]
:
weekdayLabels.enumerated().forEach {
$0.element.text = weekdayNames[$0.offset]
}
I believe there must be a better way to map the String
to the UILabel().text
. Am I right?
ios swift functional-programming
ios swift functional-programming
edited Nov 22 at 13:32
Flimzy
36.8k96496
36.8k96496
asked Nov 22 at 13:31
gmoraleda
392214
392214
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Array(repeating:count:)
really means repeating. The expression for therepeating
argument is only evaluated once. Use(0..<count).map
instead.Ue
zip
to pair each label to the corresponding string.
The code:
weekDayLabels = (0..<7).map { _ in DayLabel() }
let weekDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
zip(weekDayLabels, weekDayNames).forEach { label, dayName in
label.text = dayName
}
And even more beautiful as a one-liner:zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!
– gmoraleda
Nov 22 at 15:11
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
add a comment |
up vote
0
down vote
Have you tried declaring your weekdayLabels array as a private lazy var
?
I've had problems in the past with declaring arrays of other UIKit objects (assuming because they may or may not have been instantiated yet by the storyboard) and had to resort to the following to access the state of a ToggleButton
(a custom, toggle-able UIButton
implementation) in an optional array of buttons:
lazy var buttonsArray = {
[buttonOne, buttonTwo, buttonThree]
}()
...
if buttonsArray[index]?.isOn == true {
buttonsArray[index]?.buttonPressed()
}
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Array(repeating:count:)
really means repeating. The expression for therepeating
argument is only evaluated once. Use(0..<count).map
instead.Ue
zip
to pair each label to the corresponding string.
The code:
weekDayLabels = (0..<7).map { _ in DayLabel() }
let weekDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
zip(weekDayLabels, weekDayNames).forEach { label, dayName in
label.text = dayName
}
And even more beautiful as a one-liner:zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!
– gmoraleda
Nov 22 at 15:11
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
add a comment |
up vote
2
down vote
accepted
Array(repeating:count:)
really means repeating. The expression for therepeating
argument is only evaluated once. Use(0..<count).map
instead.Ue
zip
to pair each label to the corresponding string.
The code:
weekDayLabels = (0..<7).map { _ in DayLabel() }
let weekDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
zip(weekDayLabels, weekDayNames).forEach { label, dayName in
label.text = dayName
}
And even more beautiful as a one-liner:zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!
– gmoraleda
Nov 22 at 15:11
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Array(repeating:count:)
really means repeating. The expression for therepeating
argument is only evaluated once. Use(0..<count).map
instead.Ue
zip
to pair each label to the corresponding string.
The code:
weekDayLabels = (0..<7).map { _ in DayLabel() }
let weekDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
zip(weekDayLabels, weekDayNames).forEach { label, dayName in
label.text = dayName
}
Array(repeating:count:)
really means repeating. The expression for therepeating
argument is only evaluated once. Use(0..<count).map
instead.Ue
zip
to pair each label to the corresponding string.
The code:
weekDayLabels = (0..<7).map { _ in DayLabel() }
let weekDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
zip(weekDayLabels, weekDayNames).forEach { label, dayName in
label.text = dayName
}
answered Nov 22 at 14:58
Code Different
46k772105
46k772105
And even more beautiful as a one-liner:zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!
– gmoraleda
Nov 22 at 15:11
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
add a comment |
And even more beautiful as a one-liner:zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!
– gmoraleda
Nov 22 at 15:11
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
And even more beautiful as a one-liner:
zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!– gmoraleda
Nov 22 at 15:11
And even more beautiful as a one-liner:
zip(weekdayLabels, weekdayNames).forEach { $0.text = $1 }
. Thanks a lot!– gmoraleda
Nov 22 at 15:11
2
2
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
Yes, that's a shorter way to write it. But the older I get, the more I prefer meaningful variable names. Nothing but personal preference.
– Code Different
Nov 22 at 15:25
add a comment |
up vote
0
down vote
Have you tried declaring your weekdayLabels array as a private lazy var
?
I've had problems in the past with declaring arrays of other UIKit objects (assuming because they may or may not have been instantiated yet by the storyboard) and had to resort to the following to access the state of a ToggleButton
(a custom, toggle-able UIButton
implementation) in an optional array of buttons:
lazy var buttonsArray = {
[buttonOne, buttonTwo, buttonThree]
}()
...
if buttonsArray[index]?.isOn == true {
buttonsArray[index]?.buttonPressed()
}
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
add a comment |
up vote
0
down vote
Have you tried declaring your weekdayLabels array as a private lazy var
?
I've had problems in the past with declaring arrays of other UIKit objects (assuming because they may or may not have been instantiated yet by the storyboard) and had to resort to the following to access the state of a ToggleButton
(a custom, toggle-able UIButton
implementation) in an optional array of buttons:
lazy var buttonsArray = {
[buttonOne, buttonTwo, buttonThree]
}()
...
if buttonsArray[index]?.isOn == true {
buttonsArray[index]?.buttonPressed()
}
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
add a comment |
up vote
0
down vote
up vote
0
down vote
Have you tried declaring your weekdayLabels array as a private lazy var
?
I've had problems in the past with declaring arrays of other UIKit objects (assuming because they may or may not have been instantiated yet by the storyboard) and had to resort to the following to access the state of a ToggleButton
(a custom, toggle-able UIButton
implementation) in an optional array of buttons:
lazy var buttonsArray = {
[buttonOne, buttonTwo, buttonThree]
}()
...
if buttonsArray[index]?.isOn == true {
buttonsArray[index]?.buttonPressed()
}
Have you tried declaring your weekdayLabels array as a private lazy var
?
I've had problems in the past with declaring arrays of other UIKit objects (assuming because they may or may not have been instantiated yet by the storyboard) and had to resort to the following to access the state of a ToggleButton
(a custom, toggle-able UIButton
implementation) in an optional array of buttons:
lazy var buttonsArray = {
[buttonOne, buttonTwo, buttonThree]
}()
...
if buttonsArray[index]?.isOn == true {
buttonsArray[index]?.buttonPressed()
}
edited Nov 22 at 15:04
rmaddy
237k27308374
237k27308374
answered Nov 22 at 13:47
zb1995
239
239
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
add a comment |
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
1
1
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
I guess this works if you have already initialised buttonOne, buttonTwo, etc. In my case I want to initialise them "on the go".
– gmoraleda
Nov 22 at 15:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432127%2fcreate-array-of-n-objects-uilabel-and-map-them-with-another-array-of-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown