support need for old swift Vs new swift

I 'm. struggling with the following code "import SwiftUI

struct ContentView: View {

var pictures = [String]()

var body: some View {
    
    let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)
    
   for item in items {
        if let item = item.hasPrefix("nssl") {
            pictures.append(item)
            
        }
    }
    print(pictures)
    
}

}

while before it was :

class ViewController: UIViewController { var pictures = [String] "()

override func viewDidLoad() {
    super.viewDidLoad()

let fm = FileManager.default let path = Bundle.main.resourcePath! let items = try! fm.contentsOfDirectory(atPath: path)

and it was working

Answered by Claude31 in 778643022

Much clearer.

You should do the assignment in init or in onAppear, like this:

struct ContentView: View {
  
    @State var pictures = [String]() // need @State
    
    var body: some View {
        
        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)
        
        Text("Hello")
            .onAppear() {
                for item in items {
                     if item.hasPrefix("nssl") {
                         pictures.append(item)
                     }
                 }
                print(pictures)
            }
        
    }
}

and it was working

That tells nothing. What do you get now ? Compiler error ? Execution error ? Unexpected result ?

I guess there is a compilation error on line

            pictures.append(item)

If so, it is because pictures is a var, unmutable. Replace by

  @State var pictures = [String] ()

If that works, don't forget to close the thread by marking this answer as correct. Otherwise explain precisely the error you get.

Note: You should use code formatter tool to make your code more readable:

import SwiftUI

struct ContentView: View {
  var pictures = [String]()

  var body: some View {
    
    let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)
    
   for item in items {
        if let item = item.hasPrefix("nssl") {
            pictures.append(item)
            
        }
    }
    print(pictures)
    
  }
}

and

class ViewController: UIViewController {
   var pictures = [String] "()

   override func viewDidLoad() {
    super.viewDidLoad()
    let fm = FileManager.default 
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)

Note: there is an error (extra quote):

var pictures = [String] "()

Your first example uses SwiftUI. Every SwiftUI view requires a body.

var body: some View {
  // Code inside the body removed.
}

The body property must return a SwiftUI view. The code in your body property does not have a SwiftUI view in it so you are going to get an error when you build the project.

You should move all the code you have in body into a separate function.

I also recommend going through a book or course to learn SwiftUI. The site Hacking with Swift has a free 100 day course that teaches SwiftUI.

https://www.hackingwithswift.com/100/swiftui

Sorry guys first trime trying to post my own thread

here is the error from compiler "Closure containing control flow statement cannot be used with result builder 'ViewBuilder" appear on line 21 " For item in items {"

//
//  ContentView.swift
//  Project1
//
//  Created by Sebastien BENAVIDES on 2/2/24.
//

import SwiftUI

struct ContentView: View {
    
  
    var pictures = [String]()
    
    var body: some View {
        
        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)
        
       for item in items {
            if let item = item.hasPrefix("nssl") {
                pictures.append(item)
                
            }
        }
        print(pictures)
        
    }
}

#Preview {
    ContentView()
}

I'm trying to follow STORMVIEWS tutorial which was made with an old version of swift were viewed load was still available...

Accepted Answer

Much clearer.

You should do the assignment in init or in onAppear, like this:

struct ContentView: View {
  
    @State var pictures = [String]() // need @State
    
    var body: some View {
        
        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)
        
        Text("Hello")
            .onAppear() {
                for item in items {
                     if item.hasPrefix("nssl") {
                         pictures.append(item)
                     }
                 }
                print(pictures)
            }
        
    }
}

I'm trying to follow STORMVIEWS tutorial which was made with an old version of swift were viewed load was still available

The viewDidLoad method is still available. Your problem is that you are using SwiftUI to follow a tutorial that uses UIKit.

Start over by creating a UIKit project. Take the following steps to create a UIKit project in Xcode:

  1. In Xcode choose File > New > Project.
  2. Select iOS from the platforms at the top of the New Project Assistant.
  3. Select the App template from the list of iOS app templates.
  4. Click the Next button.
  5. Choose Storyboard from the Interface menu.

Read the following article for more details and a screenshot:

https://www.swiftdevjournal.com/xcode-11-missing-view-controllers/

support need for old swift Vs new swift
 
 
Q