1

I am quite new to swift, but I have worked a bit in php, html, css, sql and so on. In php I used to do these loops with html/css like below and filling out with data from my database. I know how to do loops, but do not know how to use them like I did in php

<?php for ($i=0; $i < sizeof($array1); $i++) { ?>
  <div class="article">
     <h1 class="title"><?php echo $array1[$i] ?></h1>
     <p><?php echo $array2[$i] ?></p>
  </div>
<?php } ?>

Are there anyway to do something like this in swift/xcode? Maybe I just need an alternative to the "div" in swift.

This might be a stupid question, but I still hope you can helt me :)

Chris
  • 85
  • 1
  • 4
  • 16
  • 1
    "I just need an alternative to the "div" in swift." That would ba an `NSView`/`UIView`. Although it really won't help to think of it that way, because the analogy really don't hold well. HTML is a declarative format for describing the structure of a web page. `UIKit`/`AppKit` don't work like that (although `SwiftUI` does). What you're probably looking for is a `UITableView` – Alexander Mar 16 '20 at 21:15
  • 1
    Does this answer your question? [Swift for loop: for index, element in array?](https://stackoverflow.com/questions/24028421/swift-for-loop-for-index-element-in-array) – alokraop Mar 16 '20 at 22:44
  • @Alexander-ReinstateMonica You were right about the tableViews. Thanks for your comment :) – Chris Mar 18 '20 at 11:29

1 Answers1

0

Try by using enumerated. The c-style for loop has been removed as from swift 3. Working sample code

    for (index, _) in array1.enumerated() {
        print(array1[index])
    }

More details here https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

kerim.ba
  • 276
  • 1
  • 8
  • Thanks for your answer. My question might be a little confusing, but I already know how to do loops, what I do not know is how to use them to make a html-like structure in my app, like above. Hope that makes sense – Chris Mar 16 '20 at 21:30