简体   繁体   English

Xcode Playground 只能部分运行

[英]Xcode Playground can only run partially

As shown above, in the Xcode playground, I am trying to extend the protocol Exercise to have two more computed properties, caloriesBurnedPerMinute and description .如上所示,在 Xcode Playground 中,我试图扩展协议Exercise以增加两个计算属性, calsalBurnedPerMinutedescription When I want to run this part of code in the playground, the "play button" at the extreme bottom left corner appears as grey, indicating that it cannot run the code until line 20. However, if I just want to run the code until line 14, as shown below, the "play button" appears as blue, indicating that it can run the code until line 14.当我想在操场上运行这部分代码时,最左下角的“播放按钮”显示为灰色,表示它不能运行代码直到第 20 行。但是,如果我只想运行代码直到第14行,如下图,“播放按钮”显示为蓝色,表示可以运行代码直到第14行。

May I know if there is anything wrong with the second extension of protocol Exercise ?请问第二次扩展练习有什么问题吗?

edited: the following is the code.编辑:以下是代码。

import Cocoa

protocol Exercise: CustomStringConvertible {
   var name: String { get }
   var caloriesBurned: Double { get }
   var minutes: Double { get }
}

extension Exercise {
   var caloriesBurnedPerMinute: Double {
       return caloriesBurned / minutes
   }
}

extension Exercise {
   var description: String {
      return "Exercise(\(name), burned \(caloriesBurned) calories in \(minutes) minutes)"
   }
}

There is nothing wrong with your code, That is just Xcode 10's new feature.您的代码没有任何问题,这只是 Xcode 10 的新功能。 See here .这里

It's a way of running your code line by line, but there are some limitations.这是一种逐行运行代码的方式,但有一些限制。 If you hover over the line number "20", you will see a grey line:如果将鼠标悬停在行号“20”上,您将看到一条灰线:

在此处输入图片说明

That means Xcode can't only run that part of the code, presumably because how Xcode handles extension declarations.这意味着 Xcode 不能只运行那部分代码,大概是因为 Xcode 处理扩展声明的方式。

Just add some code that actually runs, as opposed to just declarations, and press the play button above the bottom panel.只需添加一些实际运行的代码,而不仅仅是声明,然后按下底部面板上方的播放按钮。 Your code will run just fine.你的代码会运行得很好。

You need to wrap the Double variables in a String cast like String(caloriesBurned) :你需要用的Double在一个变量String铸像String(caloriesBurned)

extension Exercise {
   var description: String {
      return "Exercise(\(name), burned \(String(caloriesBurned)) calories in \(String(minutes)) minutes)"
   }
}

I don't know why, but it helps我不知道为什么,但它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM