Это мой код, я могу загружать данные из API, и данные также хранятся в dataArray, но если к этому массиву обращаются из другого контроллера представления или в пределах этого класса, это только ошибка.import Foundation import Alamofire
class DashboardData {
struct User {
var StateName: String
var schemes: Int
var trainingCenter: Int
var DDSubmitted: Int
var ddApproved:Int
var trainingBatches: Int
var ongoingBatches : Int
var closedBatches: Int
var trainingCandidates: Int
var underTraining: Int
var droppedOut: Int
var trained: Int
var placements: Int
var totalJobs: Int
var consideredPlaced:Int
var averageSalary:Int
init(_ dictionary: [String: Any]) {
self.StateName = dictionary["StateName"] as? String ?? ""
self.schemes = dictionary["No_of_scheme"] as? Int ?? 0
self.trainingCenter = dictionary["Training_Centres_Proposed_by_OPS"] as? Int ?? 0
self.DDSubmitted = dictionary["DD_done"] as? Int ?? 0
self.ddApproved = dictionary["DD_approved"] as? Int ?? 0
self.trainingBatches = dictionary["Total_Batches_till_Date"] as? Int ?? 0
self.ongoingBatches = dictionary["Batches_in_Progress"] as? Int ?? 0
self.closedBatches = dictionary["Closed_Batches"] as? Int ?? 0
self.trainingCandidates = dictionary["Candidates_Enrolled"] as? Int ?? 0
self.underTraining = dictionary["Candidates_Under_Training"] as? Int ?? 0
self.droppedOut = dictionary["Candidates_Dropped_out"]as? Int ?? 0
self.trained = dictionary["Candidates_Trained"]as? Int ?? 0
self.placements = dictionary["Total_interviews"] as? Int ?? 0
self.totalJobs = dictionary["Total_offered_jobs"] as? Int ?? 0
self.consideredPlaced = dictionary["Consider_placed"] as? Int ?? 0
self.averageSalary = dictionary["Average_salary"]as? Int ?? 0
}
}
var dataArray = [User]()
func downloadData(){
let defaultValues = UserDefaults.standard
let userID = defaultValues.string(forKey: "userid")
let url = URL(string: "http://192.168.1.241:9011/api/\(userID!)")
//`print(url!)
// credentials
let user = "***"
let password = "***"
let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(url!,
method: .get,
parameters: nil,
encoding: URLEncoding.default,
headers:headers)
.validate()
.responseJSON { response in
if let result = response.result.value{
print(result)
let jsonArray = result as! [Dictionary<String, AnyObject>]
for dic in jsonArray{
// model.append(User(dic))// adding now value in Model array
self.dataArray.append(User(dic))// adding now value in dataArray
}
print(self.dataArray[0].StateName)
}else{
print("Error")
}
}
если я это сделаю // print (self.dataArray [0] .StateName) здесь xcode показывает ошибку} может ли кто-нибудь объяснить мне, где я отстаю?