我是Xcode的新Swift程序员,我不明白为什么会出现这种错误。
错误:
主题1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)
它发生在使用
的for-in循环中的XMLParser.swift中Ss.append(Scene(title: e["name"].text!, imageStr: e["image"].text!, info: e["info"].text!, isEnabled: Bool(e["isEnabled"].text!)!, action: e["action"].text!, id: e.attributes["id"]!)!)
我的文件
XMLParser.swift
//
// XMLParser.swift
// Home
//
// Created by Valentin Dusollier on 24/07/2017.
// Copyright © 2017 Valentin Dusollier. All rights reserved.
//
import UIKit
import SwiftyXMLParser
class XMLParser {
let data: String
init?(data: String) {
self.data = data
let xml = try! XML.parse(data);
//mainRoom
var Ss: [Scene] = []
for e in xml["home", "scenes", "scene"] {
Ss.append(Scene(title: e["name"].text!, imageStr: e["image"].text!, info: e["info"].text!, isEnabled: Bool(e["isEnabled"].text!)!, action: e["action"].text!, id: e.attributes["id"]!)!)
}
//Home.mainRoom = Room(title: xml["home", "title"].text!, description: xml["home", "description"].text!, titleOfSection: "Scènes", backgroundStr: xml["home", "background"].text!, accessories: Ss);
}
}
struct Home {
static var mainRoom: Room = Room(title: "Connexion en cours", description: "", titleOfSection: "", backgroundStr: "", accessories: nil)!;
static var rooms: [Room]? = nil;
}
Scene.swift
//
// Scene.swift
// Home
//
// Created by Valentin Dusollier on 23/07/2017.
// Copyright © 2017 Valentin Dusollier. All rights reserved.
//
import UIKit
class Scene {
var title: String
var image: UIImage
var info: String
var isEnabled: Bool
var action: String
var id: String
init?(title: String, imageStr: String, info: String, isEnabled: Bool, action: String, id: String) {
self.title = title
self.image = UIImage(named: imageStr)!
self.info = info
self.isEnabled = isEnabled
self.action = action
self.id = id
}
}
Room.swift
//
// Room.swift
// Home
//
// Created by Valentin Dusollier on 26/07/2017.
// Copyright © 2017 Valentin Dusollier. All rights reserved.
//
import UIKit
class Room {
var title: String
var description: String
var titleOfSection: String
var accessories: Scene
var background: UIImage
init?(title: String, description: String, titleOfSection: String, backgroundStr: String, accessories: Scene?) {
self.title = title
self.description = description
self.titleOfSection = titleOfSection
self.accessories = accessories!
self.background = UIImage(named: "black-iphone")!;
if backgroundStr != "" {
let url = URL(string: backgroundStr)
let data = try? Data(contentsOf: url!)
if data != nil {
self.background = UIImage(data: data!)!
}
}
}
}
提前感谢您的回答! :d