【问题标题】:Get rid off extra output in playground在操场上摆脱额外的输出
【发布时间】:2017-05-23 21:04:21
【问题描述】:

我目前正在学习 swift,所以我正在使用 xcode 在 swift 操场上工作。

我正在处理课程,但我得到了一些额外的输出,这让我分心。

我不知道我是否修改了 xcode 偏好或我的代码有问题。

//: Playground - noun: a place where people can play

import UIKit

class Person {

    var name = ""

}

class BlogPost {
    var title:String?
    var body = ""
    var author:Person!
    var numberOfComments = 0
}

let post = BlogPost()
if let actualTitle = post.title {

}

我只想摆脱 __lldb_expr_114。

【问题讨论】:

    标签: swift swift-playground


    【解决方案1】:

    使用面向协议的方法:

    import Foundation 
    import Swift
    
    protocol PlaygroundFriendlyClass: CustomStringConvertible
    {
    
    }
    
    extension PlaygroundFriendlyClass
    {
        var description: String
        {
            return String(describing: type(of: self)).components(separatedBy: ".").last!
        }
    }
    
    class Foo: PlaygroundFriendlyClass
    {
        init()
        {
    
        }
    }
    
    class Bar: PlaygroundFriendlyClass
    {
        init()
        {
    
        }
    }
    
    Foo() // "Foo"
    Bar() // "Bar"
    

    【讨论】:

      【解决方案2】:

      添加description 属性:

      var description : String {
         return "BlogPost \(author) - \(title)"
      }
      

      【讨论】:

      • 或者如果你真的想摆脱它(当然是为了测试):var description : String { return "" }。不过,最好使用CustomDebugStringConvertibleCustomPlaygroundQuickLookable 协议。
      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 2015-02-13
      • 1970-01-01
      • 2018-01-01
      • 2015-11-19
      相关资源
      最近更新 更多