zoukankan      html  css  js  c++  java
  • 可选链

    //: Playground - noun: a place where people can play
    
    import Foundation
    
    /*
    可选链:
    1.可选链访问方式:将强制解析的感叹号换成?, 在隐式解析的后面也添加?
    2.可选链会自动判断程序访问的关联实例是否为nil, 即使为nil, 也不会报异常
    */
    
    class Person
    {
        var name:String?
        var age: Int?
        
        init(name:String)
        {
            self.name = name
        }
        
        func info()
        {
            print("我的名字是:(self.name!)")
        }
    }
    
    class Company
    {
        var employees = [
            Person(name: "Rinpe"),
            Person(name: "Lili"),
            Person(name: "Lolo")
        ]
        
        func isEmployee(name:String) -> Person?
        {
            for employee in employees {
                if name == employee.name
                {
                    return employee
                }
            }
            return nil
        }
    }
    
    var e = Person(name: "Lala")
    
    let company = Company()
    company.isEmployee(e.name!)?.age = 10
    
  • 相关阅读:
    try
    mysql 遇到的问题
    java POI(二)
    Spring/SpringBoot整合QuartZ
    Spring整合QuartZ
    Idea使用指南--实用版
    QuartZ
    Spring Task
    Spring01-模块划分
    国际化、文件上传下载
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5183138.html
Copyright © 2011-2022 走看看