Lines read the file but

let url = Bundle.main.url(forResource: “TextFile”, withExtension: “txt”)!
let text = try! String(contentsOf: url)
let lines = text.split(separator: “\n”)

all the lines from textFile are loaded into the lines array !!! yeah

I can’t do anything with them . I get self is immutable or can’ t copy

We need more of an idea what you’re trying to do. As it is, there’s not really a question here that can be answered. Please share some code and ask something specific.

The 3 let lines read the file and store it line by line in the array lines.

I have an @State var n4 = “”

if I try to n4 = lines[26] for example I get alarms
the lines array is only accessible in the Button area where it was created the rest of the app doesn’t see it.

Please post a code example so we can see what you are doing. Just describing your task and including a couple of variable assignments is not sufficient. At the very least, post the particular View where you are having issues.

import SwiftUI

struct ContentView: View {

//var c = Recipe()
@State var timerOn = true
@State var text = ""

@State var counter = 4
@State var barCounter = 1

@State var n1 = ""
@State var n2 = ""
@State var n3 = ""
@State var n4 = ""
@State var fret = 4
@State var fileName = ""
@State var bpm:Double = 71
@State var bpb = 4
@State var count = 0
@State var timerSpeed:Double = 1

@State var lines = [""]

@State var notes = [""]




var body: some View {
    
    
    
    
    
    
    
    VStack {
        
        HStack{
            Text(String("Beats Per MInute")).font(.title)
                //.background(Color.green)
            TextField("BPM:", value: $bpm, format: .number).background(Color.white).font(.system(size: 40, weight: .heavy, design: .default))
                .onChange(of: bpm) { newValue in
                    timerSpeed = 60 / bpm
                }
            
           
            
            
            
            
            
            Text("Beats Per Bar   ").font(.title)
                //.background(Color.green)
            
            TextField("Beats Per Bar", value: $bpb, format: .number).background(Color.white).font(.system(size: 40, weight: .heavy, design: .default))
            
        }
        HStack{
            Button("Start Timer",action:  {
                timerSpeed = 60 / bpm
               
                Timer.scheduledTimer(withTimeInterval: timerSpeed, repeats: true) { timer in
                     
                  if timerOn == true {
                      barCounter += 1
                      if barCounter > 4 {
                          barCounter = 1
                      }
                      if barCounter == 1 {
                          
                          counter += 2
                          if counter >= notes.count - 3 {
                              counter = 1
                              
                          }
                      }
                     
                      n3 = notes[counter]
                      n4 = notes[counter + 1]
                      
                  }
                }
                
            }).foregroundColor(Color.black).fontWeight(.none)
            
            
            Button("Stop Timer") {
                timerOn = false
            }.foregroundColor(Color.black).fontWeight(.none)
                .background(Color.cyan)
                
            Button("Resume") {
                timerOn = true
            }.foregroundColor(Color.black).fontWeight(.none)
            
            Button("Read File") {
              
                let url = Bundle.main.url(forResource: "TextFile", withExtension: "txt")!
                let text = try! String(contentsOf: url)
               @State var lines = text.split(separator: "\n")
                
                for number in lines {
                    notes[number].description = lines[number].description
                }
               
                   
                            
                
               
              
                
                
               
                
            }.foregroundColor(Color.black).fontWeight(.none)
                .background(Color.cyan)
            
            
           
        }.font(.largeTitle)
            .fontWeight(.semibold)
            //.padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            .background(Color.green)
        
        Text(n1)
            
        Spacer()
       
        
        VStack{
            Spacer()
            Spacer()
            Spacer()
            HStack{
                Text("Count")
                    .font(.title)
                    .background(Color.green)
               
                
                Text(String(barCounter))
                    .font(.largeTitle)
                    .fontWeight(.black)
                    .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                    .background(Color.green)
                Text("Bars")
                    .font(.title)
                    .background(Color.green)
                  Text(String(counter / 2)).font(.largeTitle)
                    .fontWeight(.black)
                    .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                    .background(Color.green)
                
                Button("Save Change") {
                   // notes[counter] = n3
                    
                }.foregroundColor(Color.black).fontWeight(.none)
                    .background(Color.cyan)
            }
           
            
           
            TextField("      ", text: $n3)
            
                .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                .font(.system(size: 40, weight: .heavy, design: .default))
                .foregroundColor(.black)
                .background(Color.cyan)
                
            
            
            
            
           
            TextField("      ", text: $n4)
            
                .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                .font(.system(size: 40, weight: .heavy, design: .default))
                .foregroundColor(.black)
                .background(Color.cyan)
            
            Spacer()
            
            HStack{
                Button(action: {counter += 2
                    n3 = String(notes.count)
                   
                }, label: {
                    Image(systemName: "arrow.up.square.fill")}).font(.system(size: 50))
                
                Button(action: {counter -= 2
                   // n3 = lines[counter].description
                   // n4 = lines[counter + 1].description
                }, label: {
                    Image(systemName: "arrow.down.square.fill")}).font(.system(size: 50))
               
            }
            Spacer()
        }
        
       
        
        Spacer()
    }
    Spacer()
    
    
   // bpm = c.bpm
   // bpb = c.bpb
   
    
    
   
    
   
    
    
   
}

}

func getStr(str: String) → String{
return str
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

@guyzer1954

What does each line of data look like in the TextFile that you are processing?

Paste the contents of the TextFile.txt in a reply. When you do, select that block of data and then tap the </> button in the message compose toolbar.

First things first. When posting code to these forums, place three backticks ``` on a line by themselves and then three more backticks ``` on a line by themselves and paste your code in between those two lines so that it will be formatted properly.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Now, on to the code…

@State var lines = [""]

You don’t need this, as the only place you are using lines is as a temporary variable in the action handler of a Button.

@State var notes = [""]

If notes is empty to start with, use either of these forms instead:

@State var notes: [String] = []

//or

@State var notes = [String]()

Now, to the meat of your "Read File" button.

let url = Bundle.main.url(forResource: "TextFile", withExtension: "txt")!
let text = try! String(contentsOf: url)
@State var lines = text.split(separator: "\n")

for number in lines {
    notes[number].description = lines[number].description
}

notes is an array of Strings (i.e., you previously defined it as @State var notes = [""]). String has no settable description property, so I’m not sure what you are trying to achieve here.

When you do a for X in Y loop as you have here, the part designated as X is one example of the collection of things called Y, so number is one line in the lines array. As such, you wouldn’t use number as an index into your lines array.

But, those two things aside, the loop is entirely unnecessary here anyway. All you are doing is assigning each line in lines to the notes array. Instead, just assign the lines directly to notes:

let url = Bundle.main.url(forResource: "TextFile", withExtension: "txt")!
let text = try! String(contentsOf: url)
notes = text.split(separator: "\n")

Finally, I’m not sure what this function is for:

func getStr(str: String) → String{
return str
}

It does absolutely nothing. You take a String and return the exact same String. It’s pointless.

Make those changes, see what happens. You’ll probably (definitely) still have errors but they’ll be different ones.

And provide the sample data that Chris requested.

Then let us know how it goes.

I think you need this one Patrick.

notes = text.components(separatedBy: "\n")

text.split(separator: "\n") creates a [String.Subsequence] which makes the complier have a whammy.

Thank you guys so much
notes = text.components(separatedBy: “\n”)
I was getting frustrated