Create array of n objects (UILabel) and map them with another array of String











up vote
1
down vote

favorite
1












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?










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    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?










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 13:32









      Flimzy

      36.8k96496




      36.8k96496










      asked Nov 22 at 13:31









      gmoraleda

      392214




      392214
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          1. Array(repeating:count:) really means repeating. The expression for the repeating argument is only evaluated once. Use (0..<count).map instead.


          2. 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
          }





          share|improve this answer





















          • 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




















          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()
          }





          share|improve this answer



















          • 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













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted











          1. Array(repeating:count:) really means repeating. The expression for the repeating argument is only evaluated once. Use (0..<count).map instead.


          2. 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
          }





          share|improve this answer





















          • 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

















          up vote
          2
          down vote



          accepted











          1. Array(repeating:count:) really means repeating. The expression for the repeating argument is only evaluated once. Use (0..<count).map instead.


          2. 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
          }





          share|improve this answer





















          • 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















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted







          1. Array(repeating:count:) really means repeating. The expression for the repeating argument is only evaluated once. Use (0..<count).map instead.


          2. 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
          }





          share|improve this answer













          1. Array(repeating:count:) really means repeating. The expression for the repeating argument is only evaluated once. Use (0..<count).map instead.


          2. 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
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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




















          • 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














          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()
          }





          share|improve this answer



















          • 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

















          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()
          }





          share|improve this answer



















          • 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















          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()
          }





          share|improve this answer














          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()
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo