【问题标题】:What libraries do we need to include to use CGPoint and arc4random()?我们需要包含哪些库才能使用 CGPoint 和 arc4random()?
【发布时间】:2021-07-29 10:26:46
【问题描述】:

这是我在 Swift 中的第一个程序。 该函数返回两个随机位置 (CGPoints),它们是半径 = 变量(由调用程序传入)的圆的周长上的点。 两个 CGPoints 也必须在圆的周长周围大于度数。 我写了以下代码:

 func returnPerimeterPoints (radius: Int, degreesApart: Int) -> (CGPoint, CGPoint) {
    var perimeterPoint1 = CGPoint(x:Int(arc4random()%radius),y:Int(arc4random()%radius))
    var perimeterPoint2 = CGPoint(x:Int(arc4random()%radius),y:Int(arc4random()%radius))
return (perimeterPoint1, perimeterPoint2)
    }
print(returnPerimeterPoints(20, 24))

我收到以下错误:

cannot find 'CGPoint' in scope
cannot find 'arc4random' in scope

我需要包含哪些库以及如何获得第一段中提到的 CGPoints?

【问题讨论】:

  • 不要将arc4random 与模数一起使用,选择Int.random(in:),因为它不易受到模数偏差的影响。

标签: ios swift cgpoint


【解决方案1】:

CGPointCoreGraphics 中定义,但在导入UIKitSwiftUI 等UI 库时也会默认导入。

arc4randomDarwin(内核)的一部分,但同样,当您导入依赖于它的 Apple 框架时,它会自动导入,例如 Foundation 甚至 CoreGraphics

import CoreGraphics

// ... your code here

【讨论】:

    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 2017-11-04
    • 2011-02-22
    • 2021-07-08
    • 1970-01-01
    • 2012-06-24
    • 2021-04-29
    • 2014-09-17
    相关资源
    最近更新 更多