私はSwiftの初心者です。私は今年初めからそれをやってきました...私は外部のJSONファイルからデータを取り出し、それをUILabelsに入力する単純なアプリケーションを作ろうとしています。私は引っ張って、配列に追加するデータを得ました。ここからは、スコープを終了し、他の場所では使用できないようです...私はそれに応じてデータを保持するための構造体を作成しました。ご覧のとおり、視覚的に何が起こっているかを見るためのプリントマーカーを追加しました。JSON Parse - Swiftからのデータの返却
struct GlobalTestimonialData {
var testimonialsText: [String]
var customerNames: [String]
var companyNames: [String]
}
var TestimonialData = GlobalTestimonialData(testimonialsText: [""], customerNames: [""], companyNames: [""])
func getData() {
let requestURL: URL = URL(string: "https://szadydesigns.com/test/mobileapp/testimonials.php")!
let urlRequest = URLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("File has been downloaded!")
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
print("JSON Serialized")
if let JSONfile = json as? [String: AnyObject] {
print("JSON Reading")
if let testimonial = JSONfile["testimonial"] as? [String] {
print("Testimonials Read")
TestimonialData.testimonialsText.append(contentsOf: testimonial)
print(TestimonialData.testimonialsText)
print("Inside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
if let name = JSONfile["name"] as? [String] {
print("Names Read")
TestimonialData.customerNames.append(contentsOf: name)
print(TestimonialData.customerNames)
print("Inside of loop Customers Number: \(TestimonialData.customerNames.count)")
}
if let company = JSONfile["company"] as? [String] {
print("Companies Read")
TestimonialData.companyNames.append(contentsOf: company)
print(TestimonialData.companyNames)
}
print("Companies: \(TestimonialData.companyNames)")
}
print("COMPANIES: \(TestimonialData.companyNames)")
}
print("Companies AGIAN: \(TestimonialData.companyNames)")
}catch {
print("Error with Json: \(error)")
}
print("Companies AGIAN AGAIN : \(TestimonialData.companyNames)")
}
print("Companies AGIAN AGAIN AGAIN: \(TestimonialData.companyNames)")
}
//Loses Scope
print("Companies AGIAN TIMES : \(TestimonialData.companyNames)")
task.resume()
print("Outside of loop Customers Number: \(TestimonialData.customerNames.count)")
print("Outside of loop Testimonial Text Number: \(TestimonialData.testimonialsText.count)")
print(TestimonialData.companyNames)
}
私は私は本当に簡単な何かが欠けてる知っている...しかし、私は途方に暮れてよ...すべてのヘルプ/情報感謝です!
まず:
ストーリーボードを使用していますか?あなたのコードは、ストーリーボードにリンクするカスタムクラス内にはありません。そのため、データをどのように表示するのか混乱します。 – Bawpotter
'dataTask'が非同期に動作するので、' task.resume() 'の後の行はコールバックを呼び出すために欠けています。あなたのコードにもう一つの大きなデザインミスがあります:カスタム構造体は、複数の配列を保持するのではなく、それぞれ**の**証言のための** one **インスタンスを作成することになっています。 Swift 3ではJSON辞書のためにネイティブ 'URL'、' URLRequest'、 '[String:Any]'を使います。 – vadian
はい、ストーリーボードを使用しています。上記のコードは、独自の.Swiftファイルで、ViewControllerをクリーンに保ちます。 – szady