Swift可选链接,具有非零值

时间:2018-06-03 02:29:25

标签: swift

我有以下程序可以正常工作 -

@FXML
void openAnotherWindow(ActionEvent actionEvent) {
    try {
        OpenWindow.openWindowMenuItem(someLabel, "views/some.fxml", "Title", 600, 400,
                false, "resources/pictures/some_icon.png");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

打印:

  

这个人的宠物没有喜欢的玩具

如何使用上面修改的代码进行打印"这个人的宠物喜欢泰迪熊。"?

我知道我将无法使用public class OpenWindow { public static void openWindowMenuItem(Node label, String recource, String title, int width, int height, boolean resizeable, String icon) throws IOException { Parent root = FXMLLoader.load(Objects.requireNonNull(OpenWindow.class.getClassLoader().getResource(recource))); Stage stage = new Stage(); Scene scene = new Scene(root, width, height); if (icon != null) { stage.getIcons().add(new Image(icon)); } stage.setTitle(title); stage.setResizable(resizeable); stage.setScene(scene); stage.show(); // close current window label.getScene().getWindow().hide(); // this is key point } } ,因为没有任何东西需要打开。所以我必须写这样的东西而不是class Person { var pet: Pet? } class Pet { var name: String var favoriteToy: Toy? init(name: String) { self.name = name } } class Toy { var name: String init(name: String) { self.name = name } } let q = Person() // Pet(name:"goofy") // Toy(name:"teddy") if let someToy = q.pet?.favoriteToy?.name { print("This person's pet likes \(someToy).") } else { print("This person's pet does not have a favorite toy") }

if let

应该打印"这个人的宠物喜欢泰迪熊。"

我也知道我必须把非零值这样的东西:

if let

我仍然遇到初始化问题。怎么去呢?

2 个答案:

答案 0 :(得分:0)

这可以解决您的迫切需求......

let man = Person()
let goofy = Pet(name: "goofy")
let squeaky = Toy(name: "squeaky toy")

goofy.favoriteToy = squeaky
man.pet = goofy

但是如果一个人通常会用宠物和玩具进行初始化,并且这两个类都是用字符串初始化的,那么你可能想要定义一个方便的初始化器:

class Pet {
  var name: String
  var favoriteToy: Toy?
  init(name: String) {
    self.name = name
  }
}

class Toy {
  var name: String
  init(name: String) {
    self.name = name
  }
}

class Person {
  var pet: Pet?
  convenience init(petName: String, toyName: String) {
    self.init()
    let toy = Toy(name: toyName)
    let pet = Pet(name: petName)
    pet.favoriteToy = toy
    self.pet = pet
  }
}

func test() {
  let bill = Person(petName: "goofy", toyName: "squeaky ball")
  guard let toyName = bill.pet?.favoriteToy?.name else { return }
  print(toyName)
}

test()

还有一件事:optional chaining可以与guard statement结合使用,以便在Swift中产生很好的效果。

答案 1 :(得分:0)

在初始值设定项中使用? = nil传入一些可选参数。

class Person {
    var pet: Pet?

    init(pet: Pet? = nil) {
        self.pet = pet
    }
}

class Pet {
    var name: String

    var favoriteToy: Toy?

    init(name: String, toy: Toy? = nil) {
        self.name = name
        self.favoriteToy = toy
    }
}

class Toy {
    var name: String

    init(name: String) {
        self.name = name
    }
}

let q = Person(pet: Pet(name: "goofy", toy: Toy(name: "teddy")))
// Pet(name:"goofy")
// Toy(name:"teddy")

if let someToy = q.pet?.favoriteToy?.name {
    print("This person's pet likes \(someToy).")
} else {
    print("This person's pet does not have a favorite toy")
}