Hello,
Thanks for your answer.
I have tried to implement the on Appear function but I failed.
Could you please rewrite the code with the on Appear function because I don't manage to do so.
thanks a lot.
Post
Replies
Boosts
Views
Activity
<
import SwiftUI
struct ContentView: View {
@State var username: String = "."
@State var password: String = "."
var body: some View {
ZStack {
VStack {
.onAppear(perform: {
if 1==1 {
let myUrl = URL(string: ".");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
let firstNameValue = parseJSON["Name"] as? String
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue?.components(separatedBy: ",")
print(dateien)
}
} catch {
print(error)
}
}
task.resume()
}
}
)
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
} >
Hello Sir,
It doesn't work with the <> ones.
Could you me even if I don't have them?
Thanks
Nikias
Okay, I`m sorry,
Are you able to send the corrected code anyways?
Nikias
import SwiftUI
struct ContentView: View {
@State var username: String = "."
@State var password: String = "."
var body: some View {
ZStack {
VStack {
.onAppear(perform: {
if 1==1 {
let myUrl = URL(string: "http://192.168.1.180/int.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
let firstNameValue = parseJSON["Name"] as? String
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue?.components(separatedBy: ",")
print(dateien)
}
} catch {
print(error)
}
}
task.resume()
}
}
)
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hello,
the function an the request work perfectly fine but now I´like to view the array "Dateien" that I get off the HTTP-Request with a For- loop: There should be an Image an the filename for each string in the array "Dateien", which works fine with the example array.
import SwiftUI
struct ContentView4: View {
@State var showingDetail = false
@State var username: String = "."
@State var password: String = "."
let dateien = Optional(["word.png", "vi.png", "text.png", "pp.png", "pdf.png", "ordner.png", "ex.png", "datei.png", "bild.png"])
var body: some View {
ScrollView(.vertical) {
ZStack{
VStack {
ForEach(0 ..< dateien!.count) {
Button(action: {
print("button pressed")
self.showingDetail.toggle()
}) {
Image("datei")
}
.scaledToFit()
.padding(0)
Text(self.dateien![$0])
}
}
}
.sheet(isPresented: $showingDetail) {
ts()
}
.onAppear { //This `onAppear` is added to `ZStack{...}`
doHttpRequest()
}
}
}
But I don't get how to use the Array" Dateien" from the Http-Request instead of the example one.
Heeres my code for the request:
func doHttpRequest() {
let myUrl = URL(string: "http://192.168.1.180/int.php")! //Trailing semicolon is not needed
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
//Use if-let when you want to use the unwrapped value
if let error = error {
print("error=\(error)")
return
}
//Use guard-let when nil has no meaning and want to exit on nil
guard let response = response else {
print("Unexpected nil response")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
//Use guard-let when nil has no meaning and want to exit on nil
guard let data = data else {
print("Unexpected nil data")
return
}
//#1 `mutableContainer` has no meaning in Swift
//#2 Use Swift Dictionary type instead of `NSDictionary`
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
if let parseJSON = json {
// Now we can access value of First Name by its key
//Use if-let when you want to use the unwrapped value
if let firstNameValue = parseJSON["Name"] as? String {
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue.components(separatedBy: ",")
print(dateien)
let greeting = dateien
}
}
} catch {
print(error)
}
}
task.resume()
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView4()
}
}
struct ts: View {
var body: some View {
NavigationView {
VStack {
Text("Datei.pdf")
.font(.system(size: 60))
.foregroundColor(.black)
.padding(50)
Text("+")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.yellow)
.cornerRadius(35.0)
Text("Datei herunterladen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.blue)
.cornerRadius(35.0)
Text("Datei umbenennen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.green)
.cornerRadius(35.0)
Text("Datei löschen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.red)
.cornerRadius(35.0)
}
}
}
}
I would be very glad I you help me out for another time.
Thanks
Nikias
Hello,
Thanks a lot for your quick reply.
i have now got this, but It says
"Cannot use mutating member on immutable value: 'self' is immutable."
on Line 43:
import SwiftUI
struct ContentView4: View {
@State var showingDetail = false
@State var username: String = "."
@State var password: String = "."
var dateien = Optional(["word.png", "vi.png", "text.png", "pp.png", "pdf.png", "ordner.png", "ex.png", "datei.png", "bild.png"])
var body: some View {
ScrollView(.vertical) {
ZStack{
VStack {
ForEach(0 ..< dateien!.count) {
Button(action: {
print("button pressed")
self.showingDetail.toggle()
}) {
Image("datei")
}
.scaledToFit()
.padding(0)
Text(self.dateien![$0])
}
}
}
.sheet(isPresented: $showingDetail) {
ts()
}
.onAppear { //This `onAppear` is added to `ZStack{...}`
doHttpRequest()
}
}
}
mutating func doHttpRequest() {
let myUrl = URL(string: "http://192.168.1.180/int.php")! //Trailing semicolon is not needed
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
//Use if-let when you want to use the unwrapped value
if let error = error {
print("error=\(error)")
return
}
//Use guard-let when nil has no meaning and want to exit on nil
guard let response = response else {
print("Unexpected nil response")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
//Use guard-let when nil has no meaning and want to exit on nil
guard let data = data else {
print("Unexpected nil data")
return
}
//#1 `mutableContainer` has no meaning in Swift
//#2 Use Swift Dictionary type instead of `NSDictionary`
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
if let parseJSON = json {
// Now we can access value of First Name by its key
//Use if-let when you want to use the unwrapped value
if let firstNameValue = parseJSON["Name"] as? String {
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue.components(separatedBy: ",")
print(dateien)
self.dateien = dateien
}
}
} catch {
print(error)
}
}
task.resume()
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView4()
}
}
struct ts: View {
var body: some View {
NavigationView {
VStack {
Text("Datei.pdf")
.font(.system(size: 60))
.foregroundColor(.black)
.padding(50)
Text("+")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.yellow)
.cornerRadius(35.0)
Text("Datei herunterladen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.blue)
.cornerRadius(35.0)
Text("Datei umbenennen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.green)
.cornerRadius(35.0)
Text("Datei löschen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.red)
.cornerRadius(35.0)
}
}
}
}
Hello,
I have now changed some things, but it's still not working.
import SwiftUI
struct ContentView4: View {
@State var showingDetail = false
@State var username: String = "."
@State var password: String = "."
@State var dateien = ["word.png", "vi.png", "text.png", "pp.png", "pdf.png", "ordner.png", "ex.png", "datei.png", "bild.png"]
var body: some View {
ScrollView(.vertical) {
ZStack{
VStack {
ForEach(0 ..< dateien.count) {
Button(action: {
print("button pressed")
self.showingDetail.toggle()
}) {
Image("datei")
}
.scaledToFit()
.padding(0)
Text(self.dateien[$0])
}
}
}
.sheet(isPresented: $showingDetail) {
ts()
}
.onAppear { //This `onAppear` is added to `ZStack{...}`
doHttpRequest()
}
}
}
mutating func doHttpRequest() {
let myUrl = URL(string: "http://192.168.1.180/int.php")! //Trailing semicolon is not needed
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
//Use if-let when you want to use the unwrapped value
if let error = error {
print("error=\(error)")
return
}
//Use guard-let when nil has no meaning and want to exit on nil
guard let response = response else {
print("Unexpected nil response")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
//Use guard-let when nil has no meaning and want to exit on nil
guard let data = data else {
print("Unexpected nil data")
return
}
//#1 `mutableContainer` has no meaning in Swift
//#2 Use Swift Dictionary type instead of `NSDictionary`
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
if let parseJSON = json {
// Now we can access value of First Name by its key
//Use if-let when you want to use the unwrapped value
if let firstNameValue = parseJSON["Name"] as? String {
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue.components(separatedBy: ",")
print(dateien)
self.dateien = dateien
}
}
} catch {
print(error)
}
}
task.resume()
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView4()
}
}
struct ts: View {
var body: some View {
NavigationView {
VStack {
Text("Datei.pdf")
.font(.system(size: 60))
.foregroundColor(.black)
.padding(50)
Text("+")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.yellow)
.cornerRadius(35.0)
Text("Datei herunterladen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.blue)
.cornerRadius(35.0)
Text("Datei umbenennen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.green)
.cornerRadius(35.0)
Text("Datei löschen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.red)
.cornerRadius(35.0)
}
}
}
}
Hello,
Sorry for my bad explaination.
I have now stopped mutating, the app starts fine but as far as the for- loop starts, there is this issue:
firstNameValue: pp.png,bild.png,.DS_Store,..,.
["pp.png", "bild.png", ".DS_Store", "..", "."]
ForEach<Range<Int>, Int, TupleView<(ModifiedContent<ModifiedContent<Button<Image>, _AspectRatioLayout>, _PaddingLayout>, Text)>> count (5) != its initial count (9). `ForEach(_:content:)` should only be used for *constant* data. Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!
Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444