'Confusion about accessing data between files
I have problems about adding to data that is existing in another file. I've copied someone else's code for NSKeyedArchiver, and it works fine. The original adds to the archiver by creating a custom array of Books with a let statement:
let books = [Book(title: "Nineteen Eighty-Four: A Novel", author: "George Orwell", published: 1949)]
But I want to add additional books to this array & then archive it. I can list my archived file just fine. (ContentView). So i created another view with a save button, and that's where I get confused. I tried debugging before myFile.myArchiver(), but the fruits array is empty. I tried binding the bookChoices to what was in ContentView, but then it just required it as an additional argument to the append. So how do I connect up my new addition to this array so i can archive it with the existing array?
(Also please ignore the 1975, as i was passing an Int and having problems so i just stuck that in there for now)
And yes, I have also watched CS193P lesson 3 a few times, feel confident, then stumble when I try to modify my own code.
struct ContentView: View {
// MARK: - PROPERTIES
@Environment(\.presentationMode) var presentationMode
@State private var showNewBooks: Bool = false
@State var bookChoices = [Book]() // book is a class
let deleteFile = DeleteFile() // struct
let saveFile = SaveFile() // class
let getFile = GetFile() // class
// MARK: - BODY
var body: some View {
VStack (spacing: 5) {
Button(" < Archive > ", action: {(saveFile.myArchiver()) })
Button(" < UnArchive >", action: {(getFile.myUnArchiver()) })
Button(" < Delete >", action: {(deleteFile.deleteArchivedUser()) })
}
NavigationView {
List(bookChoices) { item in
Text("\(item.title)")
Text("\t\(item.author)")
}
.onAppear(perform: {
bookChoices = getFile.myUnArchiver() //gets the file & decodes it
})
.navigationBarTitle("Books", displayMode: .inline)
.toolbar {
Button("Add") {
showNewBooks = true
}
.sheet(isPresented: $showNewBooks, content: {
NewBooks()
})
}
} //: NAV
.navigationViewStyle(StackNavigationViewStyle())
} //: BODY
}
save button:
struct SaveButton: View {
// MARK: - PROPERTIES
@AppStorage("isOnboarding") var isOnboarding: Bool = true
@Binding var bookTitle: String
@Binding var bookAuthor: String
//@Binding var bookChoices: [Book]
@State var bookChoices = [Book]()
let myFile = SaveFile()
// MARK: - BODY
var body: some View {
Button(action: {
bookChoices.append(Book(title:bookTitle, author: bookAuthor, published: 1975))
(myFile.myArchiver())
isOnboarding = false
}) {
HStack(spacing: 8) {
Text("Save")
.font(.caption)
Image(systemName: "icloud.and.arrow.up")
.imageScale(.medium)
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.shadow(color: Color(red: 0, green: 0, blue: 0,
opacity: 0.15), radius: 2, x: 2, y: 2)
.background(
Capsule().strokeBorder(Color.white, lineWidth: 1.25)
)
} //: BUTTON
.accentColor(Color.white)
} //: BODY
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
