swift - 我应该如何决定我的函数是否应该返回可选或隐式解包可选?

标签 swift option-type

如果我需要返回一些可能为零的东西,我应该如何决定我的 func 应该返回可选的还是隐式解包的可选?我见过这两个版本的 Swift 方法,我很好奇是什么插入了从函数/方法返回可选与隐式解包可选的决定,因为无论哪种方式,您仍然需要在使用它之前检查非零.

例如SpriteKit的SKNode大量使用隐式解包(部分版本如下所示)

class SKNode : UIResponder, NSCopying, NSCoding {

    ...

    /**
     The parent of the node.

     If this is nil the node has not been added to another group and is thus the root node of its own graph.
     */
    var parent: SKNode! { get }

    /**
     The children of this node.

     */
    var children: AnyObject[]! { get }

    /**
     The client assignable name.

     In general, this should be unique among peers in the scene graph.
     */
    var name: String!

    /**
     The scene that the node is currently in.
     */
    var scene: SKScene! { get }

    /**
     Physics body attached to the node, with synchronized scale, rotation, and position
     */
    var physicsBody: SKPhysicsBody!

    /**
     An optional dictionary that can be used to hold user data pretaining to the node. Defaults to nil. 
     */
    var userData: NSMutableDictionary!

    /**
     Kinematic constraints, used in IK solving
     */
    var reachConstraints: SKReachConstraints!

    /**
     Optional array of SKConstraints
     Constraints are evaluated each frame after actions and physics.
     The node's transform will be changed to staisfy the constarint.
     */
    var constraints: AnyObject[]!

    /**
     Sets both the x & y scale

     @param scale the uniform scale to set.
     */
    func setScale(scale: CGFloat)

    /**
     Adds a node as a child node of this node

     The added node must not have a parent.

     @param node the child node to add.
     */
    func addChild(node: SKNode!)

    func insertChild(node: SKNode!, atIndex index: Int)

    func removeChildrenInArray(nodes: AnyObject[]!)
    func removeAllChildren()

    func removeFromParent()

    func childNodeWithName(name: String!) -> SKNode!
    func enumerateChildNodesWithName(name: String!, usingBlock block: ((SKNode!, CMutablePointer<ObjCBool>) -> Void)!)

    /* Returns true if the specified parent is in this node's chain of parents */

    func inParentHierarchy(parent: SKNode!) -> Bool

    func runAction(action: SKAction!)
    func runAction(action: SKAction!, completion block: (() -> Void)!)
    func runAction(action: SKAction!, withKey key: String!)

    func hasActions() -> Bool
    func actionForKey(key: String!) -> SKAction!

    func removeActionForKey(key: String!)
    func removeAllActions()

    func containsPoint(p: CGPoint) -> Bool
    func nodeAtPoint(p: CGPoint) -> SKNode!
    func nodesAtPoint(p: CGPoint) -> AnyObject[]!

    func convertPoint(point: CGPoint, fromNode node: SKNode!) -> CGPoint
    func convertPoint(point: CGPoint, toNode node: SKNode!) -> CGPoint

    /* Returns true if the bounds of this node intersects with the transformed bounds of the other node, otherwise false */

    func intersectsNode(node: SKNode!) -> Bool
}

最佳答案

现在对 swift 制定严格的最佳实践可能还为时过早,但这是我的观点。一般来说,隐式解包选项应该用于少数情况,例如导出、初始化之前为 nil 的属性以及 Objective-C 函数的返回值,否则应该使用常规选项。

如果您要创建一个返回值或 nil 的函数,请与您的代码交互的其他程序员帮忙并将返回值设置为可选。

关于swift - 我应该如何决定我的函数是否应该返回可选或隐式解包可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24481931/

相关文章:

ios - 从 Github 添加 mongoSwift 驱动程序后尝试运行项目时,会显示 libbson-1.0.0.dylib : mach-o, 但不是为 iOS 模拟器构建的错误

ios - 如何将 double 类型转换为 CGAffineTransform?

ios - 理解 ?和 ! swift - 具体情况

f# - 检查几种选项类型,然后转换为类型

swift - 需要帮助添加多个保护语句来检查字符数

泛型函数中的 Swift 可选绑定(bind)

json - 将 Swift Codable 类迁移到 Realm

swift - NSDateComponentsFormatter 做错了什么?

swift - 计算 UITextField 的字符因 NSException 崩溃

swift - “线程 1 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, 消退 = 0x0)